How to disable Magento event

Featured Image

There are some cases where you will wish to disable Magento event. For example, in one our projects we decided to disable all “Mage_Log” events in order to decrease number of sql queries to the database. We will describe the process of doing it in this post, but I’m sure similar approach can be used in many other scenarios.

How are Magento events defined? For example, go to the config.xml file of magento module “Mage_Log” and you will see next content in tag “events”.
“Events” tag contains children events, you can see chunk of config.xml file.

<frontend>
	<events>
            <controller_action_predispatch>
                <observers>
                    <log>
                        <class>log/visitor</class>
                        <method>initByRequest</method>
                    </log>
                </observers>
            </controller_action_predispatch>
            .... other events
        </events>
</frontend>

For example, if you want to disable event “controller_action_predispatch” you need to create your own module and put it in your config.xml file next xml content:

	<frontend>
		<events>
			<controller_action_predispatch>
				<observers><log><type>disabled</type></log></observers>
			</controller_action_predispatch>
		</events>
	</frontend>

You will notice that we added tag “type” with value “disable“, explanation is below.
Go to Magento class “Mage_Core_Model_App” and in method “dispatchEvent” you will see next code:

            foreach ($events[$eventName]['observers'] as $obsName=>$obs) {
                $observer->setData(array('event'=>$event));
                Varien_Profiler::start('OBSERVER: '.$obsName);
                switch ($obs['type']) {
                    case 'disabled': // if we set disabled type, event will not be executed.
                        break;
                    case 'object': case 'model':
                        $method = $obs['method'];
                        $observer->addData($args);
                        $object = Mage::getModel($obs['model']);
                        $this->_callObserverMethod($object, $method, $observer);
                        break;
                    default:
                        $method = $obs['method'];
                        $observer->addData($args);
                        $object = Mage::getSingleton($obs['model']);
                        $this->_callObserverMethod($object, $method, $observer);
                        break;
                }
                Varien_Profiler::stop('OBSERVER: '.$obsName);
            }

If you set event type to “disabled“, event will not be executed.

that’s it 🙂


About Domagoj Potkoc

Backend Developer

Domagoj is Magento Certified Developer who enjoys playing tennis after long hours in front of computer screen.

Read more posts by Domagoj / Visit Domagoj's profile

2 comments

  1. I’ve used this method with 1.3.2.4 and this actually breaks the compare products functionality.. I also had controller_action_postdispatch set to disabled..

    could have been the one to break it but I’m not sure..

    any thoughts?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.