10

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.

2 Answers 2

30

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).

16
  • I have tagged the question as magento 2, the answer is for magento 1 isn't?
    – Dev
    Apr 4, 2016 at 2:13
  • You should add 'Magento2' keyword in your question, tag is not enough.
    – Sohel Rana
    Apr 4, 2016 at 6:35
  • Ok, I have edited that, and thought of "Stores -> Configuration" would make sense instead of "System -> Configuration".
    – Dev
    Apr 4, 2016 at 6:42
  • answer edited for Magento2
    – Sohel Rana
    Apr 4, 2016 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, 2016 at 10:00
4

If you want to change or filter current configuration value, use below way with event admin_system_config_changed_section_{$section}

For example, my system.xml like and I want to filter faq_url value at the time of saving the config.

<section id="faqtab" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
    <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>General Configuration</label>
        <field id="faq_url" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>FAQ Url</label>
        </field>
    </group>
</section>

Create adminhtml/events.xml

app/code/Vendor/Module/etc/adminhtml/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_faqtab">
        <observer name="custom_admin_system_config_changed_section_faqtab" instance="Vendor\Module\Observer\ConfigChange"/>
    </event>
</config>

Now create ConfigChange.php

app/code/Vendor/Module/Observer/ConfigChange.php

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;

class ConfigChange implements ObserverInterface
{
    const XML_PATH_FAQ_URL = 'faqtab/general/faq_url';

    /**
     * @var RequestInterface
     */
    private $request;

    /**
     * ConfigChange constructor.
     * @param RequestInterface $request
     * @param WriterInterface $configWriter
     */
    public function __construct(
        RequestInterface $request,
        WriterInterface $configWriter
    ) {
        $this->request = $request;
        $this->configWriter = $configWriter;

    }

    public function execute(EventObserver $observer)
    {
        $faqParams = $this->request->getParam('groups');
        $urlKey = $faqParams['general']['fields']['faq_url']['value']; //Current faq_url value, Here you can filter current value
        $this->configWriter->save('faqtab/general/page_title', $urlKey);
        return $this;
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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