what is the event in magento 2 when store configuration save?
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).
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 events.xml
app/code/Vendor/Module/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_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;
}
}