1

In my plugin I'm trying to create an event observer that should fire when I display my plugin system configuration page.

At first I created a new db table f24_staging by \Vendor\MyApp\Setup\UpgradeSchema.php I launched php bin/magento setup:upgrade from the terminal and it's ok;

This is the complete path of system.xml: \Vendor\MyApp\etc\adminhtml\system.xml

I already have got events.xml in the parent folder, that is \Vendor\MyApp\etc\events.xml

for example, here I catch the customer registration process

<event name="customer_register_success">
        <observer name="sMSHandleCustomerSaveAfter" instance="Vendor\MyApp\Observer\CustomerRegisterObserver" />
    </event>

I tried to create a new event in the same events.xml file, like this:

<event name="admin_system_config_changed_section">
        <observer
            name="templates_list"
            instance="Vendor\MyApp\Observer\F24ApiOptionsObserver"
        /> 
    </event> 

then I created my F24ApiOptionsObserver under Vendor\MyApp\Observer, like this:

class F24ApiOptionsObserver implements \Magento\Framework\Event\ObserverInterface
{
 public function __construct(
        \Vendor\MyApp\Helper\Data $helper,
        \Psr\Log\LoggerInterface $logger
     )
     {
         $this->helper = $helper;
         $this->_logger = $logger;
     }
public function execute(\Magento\Framework\Event\Observer $observer)
{
    $event = $observer->getEvent();
    $this->_logger('event :', $event);
    $objectManager =   \Magento\Framework\App\ObjectManager::getInstance();
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection('\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION'); 
    $f24_staging = $resource->getTableName('f24_staging');
    $storeId = (int) $this->getRequest()->getParam('store', 0);
    $apiKey = $this->helper->getConfig('myapp/generali/api_key', $storeId);
    $templates_res = $this->helper->apiCall('https://www.app.myapp.com/api/v0.3/GetTemplate', $apiKey);
    $templates_xml = simplexml_load_string(utf8_encode($templates_res));
    $json_templates = json_encode($templates_xml);
    $this->helper->trace('elenco templates :', $json_templates);
    $insert = "insert into $f24_staging set id_api_call='1',f24_options_value='$json_templates'";
    $connection->query($insert);
}

}

The observer should add data to my custom db table when I display system admin config page, but the event is not firing. Anyone could help me?

0

At last I found a solution.

This is my events.xml file

<event name="layout_generate_blocks_before">
        <observer name="f24apioptions" instance="Vendor\MyApp\Observer\F24ApiOptions" />
    </event>  

Then my Observer, path => \Vendor\MyApp\Observer\F24ApiOptions

<?php

namespace Vendor\MyApp\Observer;

class F24ApiOptions implements \Magento\Framework\Event\ObserverInterface
{
    public function __construct(
        \Vendor\MyApp\Helper\Data $helper
    ) {
        $this->helper = $helper;
    }

    

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $event = $observer->getEvent();
        if ($event['full_action_name'] == 'adminhtml_system_config_edit') {
           
            $this->helper->saveTemplateList();
            $this->helper->saveNumeratorList();
            $this->helper->savePdcList();
        }
        
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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