Magento 2 autoclean config cache observer

In Magento, the event admin_system_config_changed_section_ is an event that is triggered when a configuration section is changed in the admin panel. Observers are used in Magento to listen for specific events and perform custom actions when those events occur.

Here’s how you can set up an observer for the admin_system_config_changed_section_ event in Magento:

  1. Create a custom module or use an existing one. If you don’t have a custom module, you can create one in the app/code directory of your Magento installation.

  2. Inside your module, create a etc directory if it doesn’t already exist, and within that directory, create a events.xml file. This file will define your observer and the event it listens to.

  3. In the events.xml file, add the following XML code to configure your observer:


<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_[your_section_id]">
        <observer name="[your_observer_name]" instance="[Vendor]\[Module]\Observer\[YourObserverClass]" />
    event>
config>

Replace the placeholders with your actual values:

  • [your_section_id]: The ID of the configuration section you want to listen for changes to.
  • [your_observer_name]: A unique name for your observer.
  • [Vendor]\[Module]\Observer\[YourObserverClass]: The class of your observer.
  1. Create the observer class specified in the XML configuration. This class should implement the Magento\Framework\Event\ObserverInterface interface and contain the logic you want to execute when the configuration section is changed. Here’s an example of what your observer class might look like:
namespace [Vendor]\[Module]\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class [YourObserverClass] implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        // Your custom logic here
    }
}
  1. Don’t forget to replace [Vendor]\[Module] and [YourObserverClass] with your actual module and observer class names.

Now, your observer is set up to listen for changes to the specified configuration section in the Magento admin panel. When the section is changed, the execute method of your observer class will be called, allowing you to perform custom actions based on the changes made to the configuration section.



Copyright © 2013-present Magesystem.net All rights reserved.