1

save data from a module in database if save config button is click in admin configuration. Basically I want to disabled a module if disabled from admin. Actually disable functionality is not working. So I trying disabled module pro grammatically I have created a controller with update query but this works when I hit the controller in front end. Basically My query disable module from general but my query will disable from advance/advance.

Path of controller - Vendor/Module/Controller/index/index.php

function in controller that I trying to update :-

public function execute()
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
        $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        $tableName = $resource->getTableName('core_config_data'); 
   echo $isEnabled = $this->helperData->isEnable();

   if ($isEnabled==1) {
        //gives table name with prefix

        //Select Data from table
        echo $sql = "Update " . $tableName . " Set value = 0 where path ='advanced/modules_disable_output/Vendor_Module'";
        $connection->query($sql); 

        }

        //return $result->setData($methods);

    else {
        echo $sql = "Update " . $tableName . " Set value = 1 where path ='advanced/modules_disable_output/Vendor_Module'";
        $connection->query($sql); 

        }
}
1

You can achieve what you want to do by either using observer or plugin as follows ;

Following event are fire after System->configuration Save.

admin_system_config_changed_section_{$section} -> here $section is 'general' etc.

Here is an example for 'admin_system_config_changed_section_general' event.

etc/events.xml


<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="admin_system_config_changed_section_general">
        <observer name="custom_admin_system_config_changed_section_general" instance="[Vendor]\[Module]\Observer\ConfigObserver"/>
    </event>
</config>

and observer class is "[Vendor][Module]\Observer\ConfigObserver"


namespace [Vendor]\[Module]\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Psr\Log\LoggerInterface as Logger;

class ConfigObserver implements ObserverInterface
{
    /**
     * @var Logger
     */
    protected $logger;

    /**
     * @param Logger $logger
     */
    public function __construct(
        Logger $logger
    ) {
        $this->logger = $logger;
    }

    public function execute(EventObserver $observer)
    {
        //$this->logger->info($observer->getWebsite());
        //$this->logger->info($observer->getStore());
    }
}

You can do this same thing using plugin. Create di.xml "etc/di.xml"


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Config\Model\Config">
        <plugin name="admin_system_config_save_plugin" type="[Vendor]\[Module]\Plugin\ConfigPlugin" sortOrder="1"/>
    </type>
</config>

And Plugin class '[Vendor][Module]/Plugin/ConfigPlugin.php'


namespace [Vendor]\[Module]\Plugin;


class ConfigPlugin
{
    public function aroundSave(
        \Magento\Config\Model\Config $subject,
        \Closure $proceed
    ) {
        // your custom logic
        return $proceed();
    }
}

Reference : what is the event in magento 2 when store configuration save?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.