what is the event that is triggered after the save config action is performed in magento 2 Stores - Configuration, and how to write a simple observer method after the save is done.

up vote 17 down vote accepted

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();
    }
}

Note:

'admin_system_config_changed_section_general' this event dispatch with 2 argument (website and store). So If you need more data like configuration fields values then you should go with 2nd solution(plugin).

  • I have tagged the question as magento 2, the answer is for magento 1 isn't? – Dev Apr 4 '16 at 2:13
  • You should add 'Magento2' keyword in your question, tag is not enough. – Sohel Rana Apr 4 '16 at 6:35
  • Ok, I have edited that, and thought of "Stores -> Configuration" would make sense instead of "System -> Configuration". – Dev Apr 4 '16 at 6:42
  • answer edited for Magento2 – Sohel Rana Apr 4 '16 at 7:11
  • 1
    'admin_system_config_changed_section_general' this event dispatch with 2 argument (website and store). So If you need more data then you should go with 2nd solution(plugin) – Sohel Rana Apr 6 '16 at 10:00

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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