How to Trigger an Event on Magento 2 Save Store Configuration

Steps to Trigger an Event on Magento 2 Save Store Configuration

<section id="mytab" 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="name" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enter Name</label>
</field>
</group>
</section>

The section id is mytab which has to be added in the event name.

Create event.xml

Directory 

app/code/VendorName/ModuleName/etc/adminhtml/events.xml

Content of 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_mytab">
<observer name="custom_admin_system_config_changed_section_mytab" instance="VendorName\ModuleName\Observer\ConfigChange"/>
</event>
</config>

Create ConfigChange.php

Directory

app/code/VendorName/ModuleName/Observer/ConfigChange.php

Content of ConfigChange.php

<?php
namespace VendorName\ModuleName\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
{
private $request;
private $configWriter;
public function __construct(RequestInterface $request, WriterInterface $configWriter)
{
$this->request = $request;
$this->configWriter = $configWriter;
}
public function execute(EventObserver $observer)
{
$meetParams = $this->request->getParam('groups');
$name = $meetParams['general']['fields']['name']['value'];
//to print thatn value in system.log
\Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info($name);
return $this;
}
}
?>
 If you are looking for Magento Developers, visit Magento Developer Agency.
Related Products