Magento – add an observer when save in system->configuration

corecore-config-dataevent-observer

I need to add an observer in my module's config.xml file, the event is when the user press the button Save Config in System->Configuration->MyModule section of the Admin-panel.
I've this in config.xml:

<global>
...
        <events>
            <admin_system_config_changed_section_sectionname>
                <observers>
                    <module_name>
                        <class>module_name/observer</class>
                        <method>handle_adminSystemConfigChangedSection</method>
                    </module_name>
                </observers>
            </admin_system_config_changed_section_sectionname>
        </events>

And this is what i have in Model/Observer.php :

<?php
class Modulename_Model_Observer
{
    public function handle_adminSystemConfigChangedSection(Varien_Event_Observer $observer)
    {
        Mage::log('Test: oberver is working!');
    }
}

When i check var/log/system.log there's nothing there. So the observer is not working. What's wrong?.

EDIT (SOLVED):

I managed to add the observer correctly.

config.xml file:

<admin_system_config_changed_section_sectionaname>
    <observers>
        <modulename>
            <type>singleton</type>
            <class>modulename/observer</class>
            <method>observersection</method>
        </modulename>
    </observers>
</admin_system_config_changed_section_sectionaname>

Observer.php:

<?php
class Modulename_Model_Observer extends Mage_Core_Model_Session_Abstract
{

    public function observersection(Varien_Event_Observer $observer)
    {
        die("");
    }
}

Best Answer

The classname should be [Namespace]_[Module]_Model_Observer, to make sure the right class is used in your config.xml you can specify the whole classname in the <class> tag.

<class>[Namespace]_[Module]_Model_Observer</class>

It might be helpful to also post the system.xml contents to see if the admin_system_config_changed_section_[sectionname] is correct.

Small suggestion. Keep your method names small and consistent. Although technically there's nothing wrong with the name using underscore and camelcase, but it may not be best practice to use underscores and camelcases.

Related Topic