7

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

10

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
| improve this answer | |
  • Thank you. 1) Does <events> section has to be inside <global> or somewhere else in config.xml? 2) I don't know how to call my action inside observer, can you point me in the right direction? @Fabian – zitix Apr 15 '13 at 23:35
  • There are three options to add events: global, frontend and adminhtml. Global is called everytime, frontend only in frontend, adminhtml only in backend. So adminhtml should be good – Fabian Blechschmidt Apr 16 '13 at 9:56
  • And how to redirect to controller/action in observer method? – zitix Apr 16 '13 at 10:51
  • Mage::app()->getRequest()->setRedirect('checkout/index') – Fabian Blechschmidt Apr 16 '13 at 11:59
6

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.

| improve this answer | |
1

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();
        }
    }
}
| improve this answer | |
0

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"];
| 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.