Skip to content

February 19, 2011

1

Disabling a Magento Observer in config.xml

In some rare cases there is functionality that clients need me to develop that requires disabling some built-in observers due to them conflicting with the desired custom behavior. There are also some that aren’t needed by everyone that you can gain a performance boost from by disabling. I have previously disabled a few observers by rewriting the observer model and returning NULL inside the observer method. Brought to light by Colin M. on the Magento Developer Group, it turns out there is a better technique and method of disabling observers that is both less intrusive and only requires a small bit of configuration XML in your custom modules config.xml file.

I┬ádecided to test it myself and find out if it worked and then determine if it was merely a side effect of something not being handled in the code or if the event dispatcher was intentionally coded to skip calling observers that have the type set to ‘disabled’. The one I chose for my test case was the customer_login event, which is called by an observer in the Mage_Catalog module; I added a call to Mage::log in my sandbox to easily see when it was being called.

1
2
3
4
5
    <customer_login>
        <observers>
            <catalog><type>disabled</type></catalog>
        </observers>
    </customer_login>

In a typical observer declaration you would have a type of ‘model’ or ‘object’, but if you specify a type of ‘disabled’ Magento will specifically skip calling the observer per the code found in Mage_Core_Model_App::dispatchEvent, which means not only is this less intrusive and only requiring a bit of XML to disable any observer, it is also does not rely on side-effects of being an invalid observer type.

1
2
3
4
5
6
7
8
9
10
11
switch ($obs['type']) {
 
    case 'disabled':
        break;
    case 'object': case 'model':
        ...
        break;
    default:
        ...
        break;
}
Read more from Magento
1 Comment Post a comment
  1. Oct 19 2012

    Hi! Thanks, it’s very useful information. We have an article on the similar topic posted here http://www.atwix.com/magento/observers-reality-or-fiction/ you’re welcome to check it out and leave your feedback.
    It is nice to share our achievements with others

    Reply

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments