Overwriting and deactivating Observers in Magento

Citește postarea în română

Share on:

Sometimes we need to overwrite an observer. The first way that usually comes in mind is overwriting the model. Usually is named Observer.php, because this is the “best practice”.

And NO, you don’t have to overwrite the model. Observer.php doesn’t extend anything anyway and usually contains all of the module’s observers, so you can’t overwrite the same observer in more than one module.

How it works?
In magento when a new observer is added, it must have an unique identifier. This identifier is the key!

Actually, there is another element: “area”. When Mage::dispatchEvent(…) is performed, events will be dispatched using “area” and “identifier”.

For example, the admin notification system, which is observing “controller_action_predispatch”, will run:

1=> "global"(area)
2=> "controller_action_predispatch"(event)
3=> "adminnotification"(identifier)

then:

1=> "adminhtml"(area)
2=> "controller_action_predispatch"(event)
3=> "adminnotification"(identifier)

If the event was in the frontend area, it would be: “global” then “frontend”.

Overwriting
Overwriting is in fact a observer defined in the same config area as the original event (global, frontend or adminhtml), attached to the same event and with the same identifier as the original observer (e.g. adminnotification).

Let’s say we have to overwrite “adminnotification”. This observer is in Mage/AdminNotification. The unique identifier is defined in etc/config.xml:

 1...
 2  <adminhtml>
 3...
 4    <events>
 5      <controller_action_predispatch>
 6        <observers>
 7          <adminnotification>
 8            <class>adminnotification/observer</class>
 9            <method>preDispatch</method>
10          </adminnotification>
11        </observers>
12      </controller_action_predispatch>
13    </events>
14...
15  </adminhtml>
16...

From the example above we can see:
– area: adminhtml
– event: controller_action_predispatch
– identifier: adminnotification

The module activation file will be: app/etc/modules/CP_AdminNotification.xml

 1<?xml version="1.0"?>
 2<config>
 3  <modules>
 4    <CP_AdminNotification>
 5      <active>true</active>
 6      <codePool>local</codePool>
 7      <depends>
 8        <Mage_AdminNotification/>
 9      </depends>
10    </CP_AdminNotification>
11  </modules>
12</config>

I’ve added dependencies because without the original module, this module will be useless.

There’s a “best practice” to name a module that is overwritten with the same name as the original module.

The configuration file for this module, will contain practically everything you’ll need for the overwriting: area, event and identifier. The file is located in app/code/local/CP/AdminNotification/etc/config.xml:

 1<?xml version="1.0"?>
 2<config>
 3  <modules>
 4    <CP_AdminNotification>
 5      <version>0.0.1</version>
 6    </CP_AdminNotification>
 7  </modules>
 8  <global>
 9    <models>
10      <cp_adminnotification>
11        <class>CP_AdminNotification_Model</class>
12      </cp_adminnotification>
13    </models>
14  </global>
15  <adminhtml>
16    <events>
17      <controller_action_predispatch>
18        <observers>
19          <adminnotification>
20            <class>cp_adminnotification/observer</class>
21            <method>overwrittenPreDispatch</method>
22          </adminnotification>
23        </observers>
24      </controller_action_predispatch>
25    </events>
26  </adminhtml>
27</config>

The observer should contain all the new logic. The file is in app/code/local/CP/AdminNotification/Model/Observer.php, just like you would probably expect from the structure above.

1<?php
2
3class CP_AdminNotification_Model_Observer {
4
5  public function overwrittenPreDispatch(Varien_Event_Observer $observer) {
6    // noua logica din observer
7  }
8}

Disabling
Disabling is preaty similar to overwriting, the difference is in the config and the fact that an observer file is not needed anymore, because there isn’t a new logic.

The new config.xml file is:

 1<?xml version="1.0"?>
 2<config>
 3...
 4  <adminhtml>
 5    <events>
 6      <controller_action_predispatch>
 7        <observers>
 8          <adminnotification>
 9            <type>disabled</type>
10          </adminnotification>
11        </observers>
12      </controller_action_predispatch>
13    </events>
14  </adminhtml>
15</config>