Magento Module Development Series – Part11 – Events

In this blog we are going to learn about Magento Event Handling API
Events are very powerful tool in magento for customization.

I my experience, events should be used more often that overriding of classes. The reason being, if there are two or more module, overriding same class then there will be conflict, i.e only 1 module will work. But, if you’re using events, multiple modules can subscribe to a single event easily.
Magento Events follow Observer Design Pattern. So here is how it works: In the core code of magento, at many places magento fires events. Each event has a unique name and some additional data parameters associated with it. In our module, we can subscribe to these events, meanings, as soon as magneto fires an event, a function in our custom module class is executed where we can do some data manipulation and other thing.
Here is how we do it, Magneto uses a function called Mage::dispatchEvent to fire an event. You will find this any many places inside magento code based.
For example in class Mage_Checkout_Model_Type_Onepage in saveOrder function.

Mage::dispatchEvent('checkout_type_onepage_save_order_after', array('order'=>$order, 'quote'=>$this->getQuote()));

Each event has a name and data, in the above case its checkout_type_onepage_save_order_after and data i.e array(‘order’=>$order, ‘quote’=>$this->getQuote()) (which is an array passed)
In our module to subscribe/listen to this event we need to add the following in config.xml file.
Inside the tag, will put this

<events>
     <checkout_type_onepage_save_order_after> <!-- Name of Event -->
      	 <observers>
                   <save_after> <!-- Any Unique Identifier -->
                    <type>singleton</type>
                    <class>Excellence_Test_Model_Observer</class> <!-- Over Model Class -->
                    <method>saveOrderAfter</method> <!-- name of function -->
                  </save_after>
        </observers>
      </checkout_type_onepage_save_order_after>     
</events>

Then we will create a class in Model folder of our module called Observer.php and declare a function saveOrderAfter() there.

<?php
class Excellence_Test_Model_Observer {
	public function saveOrderAfter($evt){
		$order = $evt->getOrder(); //this is how we access data passed in event
		$quote = $evt->getQuote(); //this is how we access data passed in event
		....
		do something here
		send email
		etc etc 
		....
	} 
}

Few important events that are fired in magento.
When every a Model is saved an event is fired called
Mage::dispatchEvent(‘model_save_before’, array(‘object’=>$this));
Mage::dispatchEvent(‘model_save_after’, array(‘object’=>$this));

Exercise:
Create a small user tracking system. As soon as a user login in magneto, make a database entry of date time and username of the user. And as soon, as user logs out, make another database entry to record when user logged out.
  • roman

    Hello Manish!
    Can I add multiple observers to a single event? I want smth like this
                                                     singleton                    observer1                     fisrtMethod                                    singleton                    observer2                     secondMethod                             

    • Manish Prakash

      Yes its easy here is an example

      singleton

      Excellence_Facebook_Model_Observer
      beforeLogout

      singleton

      Excellence_Facebook_Model_Observer

      preDispatch

  • Frederik

    First, I want to say thank you for a great blog. Then I would like to know, why in some modules, I have seen this structure for events:
     
       
         
            ns_module/observer
            addLayoutHandles
         
       
     

    Why are yours written with full class name and a type singleton? What difference does it make?

    • Manish Prakash

      The way your saying could be another way of doing events. Singleton just means it won’t create another instance of an object, i guess the syntax you have shown is a non singleton way.