Quantcast
Channel: Baobaz Blog - Magento modules
Viewing all articles
Browse latest Browse all 10

Magento Events

$
0
0

When it comes to extending Magento core functionality you have two options - override core classes or use event-driven architecture. The major disadvantage of first is that you can override class only once, so if you want to override it in multiple modules you are soon going to find yourself in dependency hell. Event-driven architecture allows you to keep loose coupling without losing the flexibility of extending Magento modules.

When you want to use Magento event-driven architecture you must know basically two things - how to dispatch an event and how to catch it.

Dispatching events

Within Magento you can dispatch an event as simple as by calling Mage::dispatchEvent(...) method, for example:

 Mage::dispatchEvent('custom_event',array('object'=>$this));

This methods accepts two parameters - event unique identifier and associative array of data that is set as Varien_Event_Observer object data, so in fact passed to event observers.

Catching events

Catching events is a little bit more complex than dispatching. You have to use existing custom module or create a new one. In the minimal case the module file tree should look like this:

  

Within config.xml you have to add a definition of event observer. Which of the main config.xml file sections (frontend, adminhtml) should contain this definition depends on the scope you want your observer to work on. Here is the example of definition:

<events>
      <custom_event><!-- identifier of the event we want to catch -->
        <observers>
          <custom_event_handler><!-- identifier of the event handler -->
            <type>model</type><!-- class method call type; valid are model, object and singleton -->
            <class>baobazacustommodule/observer</class><!-- observers class alias -->
            <method>customObserverAction</method> <!-- observer's method to be called -->
            <args></args><!-- additional arguments passed to observer -->
          </custom_event_handler>
        </observers>
      </custom_event>
</events>

Xml above should be self-explanatory. I will just explain that for type model and object are equal in behavior and mean that object of a class will be instantiated using Mage::getModel(...) method, and singleton means it will be instantiated using Mage::getSingleton(...) method.

Observer.php file should contain relevant observer class. There is no interface nor need to extend any class for observer classes. The method though should accept one parameter which is the object of Varien_Event_Observer class. This object is the link between dispatcher and event handler. It inherits from Varien_Object so has all required getters handled magically. For example:

class Baobaz_ACustomModule_Model_Observer
{
  publicfunction customObserverAction(Varien_Event_Observer $observer)
  {
    $object=$observer->getEvent()->getObject();// we are taking the item with 'object' key from array passed to dispatcher
    $object->doSomething();

    return$this;
}

Default events

Magento implements lot of events. You can find list of them here. What you may miss reading this list, and what was spotted on MageDev blog, Mage_Core_Model_Abstract by default dispatch some special events. Those are:
 

event identifierevent parameters
model_save_before'object'=>$this
{_eventPrefix}_save_before{_eventObject}=>$this
model_save_after'object'=>$this
{_eventPrefix}_save_after{_eventObject}=>$this
model_delete_before'object'=>$this
{_eventPrefix}_delete_before{_eventObject}=>$this
model_delete_after'object'=>$this
{_eventPrefix}_delete_after{_eventObject}=>$this
model_load_after'object'=>$this
{_eventPrefix}_load_after{_eventObject}=>$this

 

{_eventPrefix} means the value of $_eventPrefix variable and {_eventObject} means the value of $_eventObject variable. All classes inheriting from Mage_Core_Model_Abstract should override these variables to create specific events being dispatched. For example for catalog cagetory these variables take following values: $_eventPrefix = 'catalog_category';  $_eventObject = 'category';


Viewing all articles
Browse latest Browse all 10

Trending Articles