سؤال

How can I call an action of my controller in a custom module after "Save Config" was clicked in System --> Configuration --> Catalog? (image)

Settings of my module are inside that section, and I need to fire my action just after configuration was saved. I need to call the action only in that section of the admin panel - if "Save Config" was clicked in a different section of the admin, the action don't need to be called.

EDIT:

Action has to check if saved settings of the module are correct, and do some other calculations every time the configuration is saved.

EDIT 2:

I tried solution posted by Fabian Blechschmidt. Observer is working after "Save Config" was clicked (I see text in log file):

config.xml:

<config>
...
    <global>
        <events>
            <admin_system_config_changed_section_mysection>
                <observers>
                    <mymodule>
                        <type>singleton</type>
                        <class>mymodule/observer</class>
                        <method>handle_adminSystemConfigChangedSection</method>
                    </mymodule>
                </observers>
            </admin_system_config_changed_section_mysection>
        </events>
    </global>
...
</config>

Model/Observer.php:

class My_Module_Model_Observer
{
    public function handle_adminSystemConfigChangedSection()
    {
        Mage::log('Test: oberver is working!');

        //I tried this but it doesn't actually trigger the action:
        $url = Mage::getUrl('myrouter/adminhtml_test/validate');
        Mage::app()->getResponse()->setRedirect($url);
    }
}

But now I'm stuck.

I don't know how to trigger action method inside that observer. Controller looks like this and it works fine:

config.xml:

<admin>
    <routers>
        <mymodule>
            <use>admin</use>
            <args>
                <module>My_Module</module>
                <frontName>myrouter</frontName>
            </args>
        </mymodule>
    </routers>
</admin>

controllers/Adminhtml/TestController.php:

class My_Module_Adminhtml_TestController extends Mage_Adminhtml_Controller_Action
{   
    public function validateAction()
    {
        //validate config settings here
    }
}

EDIT 3:

Event-observer solution works fine, so I created separate question about redirecting to actions: Redirect to module/controller/action

هل كانت مفيدة؟

المحلول

What exactly is your problem? Your path:

  1. Register an observer, listen to admin_system_config_changed_section_{$section}, /app/code/core/Mage/Adminhtml/controllers/System/ConfigController.php:177
  2. do whatever you want

نصائح أخرى

You can set a backend model to one of your config settings. In this model you can add methods like _beforeSave and _afterSave and you also have access to the POST vars. This way it doesn't mater where you add your settings, the model methods will be triggered.
To see an example check how Magento does it in app/code/code/Mage/Core/etc/syste.xml, path trans_email/ident_custom1/email.
It has the backend model <backend_model>adminhtml/system_config_backend_email_address</backend_model>
And in Mage_Adminhtml_Model_System_Config_Backend_Email_Address the _beforeSave method validates the email address entered.

It works for me if I use the full class name rather than the namespace/observer syntax in config.xml. Try this.

config.xml

<config>
...
    <global>
        <events>
            <admin_system_config_changed_section_mysection>
                <observers>
                    <mymodule>
                        <type>singleton</type>
                        <class>Namespace_Mymodule_Model_Observer</class>
                        <method>handle_adminSystemConfigChangedSection</method>
                    </mymodule>
                </observers>
            </admin_system_config_changed_section_mysection>
        </events>
    </global>
...
</config>

Model/Observer.php

class Namespace_Mymodule_Model_Observer
{
    public function handle_adminSystemConfigChangedSection()
    {
        die('I have called the admin config changed observer');
        // Web browse to admin config section, change something, then save it
        // Program should die with above message
        // Then replace these lines with whatever you want it to do, eg

        // If option 1 is switched to false, then switch option 2 to false
        $switch1 = Mage::getStoreConfig('mysection/mytab/myoption1', Mage::app()->getStore());
        if (!$switch1) {
            Mage::getConfig()->saveConfig('mysection/mytab/myoption2', false);
            Mage::getConfig()->reinit();
            Mage::app()->reinitStores();
        }
    }
}

I used the controller_action_postdispatch_adminhtml_catalog_product_save event, after using some temporary logging in Mage.php event dispatch. It was the only event that truly was fired in admin, but not on all product saves (for example, it doesn't fire when api product save is used).

I was able to use var_dump in my observer function to dig out the data I wanted.

In your observer, you can do things like this..... There are ways to dig down into the objects and find almost any data you are looking for, once you learn how to look.

public function syncProducts($observer) {
   $event = $observer->getEvent();

   // After var_dump($event) i learned that params were available in the controller
   $controllerAction = $event->getControllerAction();

   // Then I saw that the original request was available
   $request = $controllerAction->getRequest()

   // Then i noticed that the params were available from the save event
   $params = $request->getParams();

   // From there, it was just a simple array
   $product = $params["product"];
   $sku = $product["sku"];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top