The Events and Observers in Magento

Today we will see a new way to edit a Magento basic behaviors using the Events and Observers . This a standard design patern in software development that you must master.

1- A little theory on the events and the observers

This programming method is called a "design patern", it is one of the big bases in development and you will find regularly in magento. What is it about ?
First, let's define two terms:

The Event:
An event can be considered as a kind of flag that rises when a specific situation happens for example when the user presses the button "pay" of your website, it is an event. Your order is saved? Again it is an event. Etc ...

An Observer:
An Observer, also called "listener", will listen to your program to detect events. When there is an event taking place, it will perform an action defined by you.

A small example: The neighbor and you

! Let us in situation, you are at home and you are throwing a party ... you start the evening and everything goes well, then at one point, one of your friend who is too drunk starts singing loudly in the garden. Your neighbor has never had a friend of her life, she remains all the time in her house watching the others having fun (...maybe she's just a developer xD !! )! She has nothing to do better in his life than waiting for you to do a little too much noise to be able to call the police ... that's how she likes to have fun, that's her kind of fun...that allows it to pass the time ( it takes all kinds to make a world :) ).

Now in this, one of your friends makes too much noise, "make too much noise," here is the Event . The Observer is your neighbor, and what does she observe ? you making too much noise (the event). And when the event fires: she call the police!

With this great example (in which I'm not talking about actual fact, neither alludes to one of my last nights when i wrote this article as a 20years old men... especially not my old neighbor ), you get the idea I hope ! Now let a little practice and let's switch on our favorite e-commerce ... Magento CMS!

2- Create observeur

A observer under magento is located in the directory Model of your module and extends the Varien_Event_Observer (the class name, is very clear...), so go in /app/code/local/Pfay/Films/Model/ folder and create a file Observer.php as follow :

class Pfay_Test_Model_Observer extends Varien_Event_Observer
{
   public function __construct()
   {
 
   }
   
   public function saveCmsPageObserve($observer)
   {
         $event = $observer->getEvent(); 	
         $model = $event->getPage();
   	     print_r($model->getData());
         die('test');
   }
}

Here we create an Observer with a function saveCmsPageObserve , this function is the method that will be executed when a CMS page will be saved in the administration of Magento (save the page is our event). In our example we stop the program to display a the content of the page object but in the reality in place of the die(), you will do what you want.

I think I did not need to do you any explanation on this little code, I get the event using the getEvent() method of the observer Object (which you will find as a parameter of the method, this object is automatically sent to the method). Then from the event, from the getPage() method I get my cms page and I then displays its content with the print_r php method and getData as of habit.

3- Declare the Observer in Magento

Now that we have created our Observer Magento must take it into account, how do we do? ... 11th tutorial, if you do not have an idea I urge you to hang yourself muahahaha ... restart this tutorial serie from the very beginning because obviously you have not understood the Magento development bascics principles to be confortable enought with the following lessons. Sure you understood we will edit the file config.xml of our module to declare our Observer. So open your file: /app/code/local/Pfay/Films/etc/config.xml In global add:

<events>
   <cms_page_prepare_save>
	<observers>
	   <Pfay_Films_Model_Observer>
	       <type>singleton</type>
	       <class>pfay_films/observer</class>
	       <method> saveCmsPageObserve</method>
           </ Pfay_Films_Model_Observer>
       </observers>
  </cms_page_prepare_save>
</events>

Some little explanation is necessary here:

The tag :

<cms_page_prepare_save>

Represents the event. In our example your neighbor expecting you to do too much noise to be able to launch its action (call the police). Here we expect action cms_page_prepare_save to be triggered to launch our method saveCmsPageObserve ). The result seems logical:

<observers>
      <Pfay_Films_Model_Observer>
        	 <type>singleton</type>
        	 <class>pfay_films/observer</class>
        	 <method> saveCmsPageObserve</method>
     </Pfay_Films_Model_Observer>
</observers>

It is defined above, observe the class that will be used by specifying: - The event on which this method will be called - The type (here Singleton) - The path to the observer class (in the Model folder). - The method used in this Observer. Ok Ok I understand but how do we know it is the event cms_page_prepare_save which is used? It's not complicated, in fact we will see in the core file, you open the file: \app\code\core\Mage\Adminhtml\controllers\Cms\PageController.php and seen in the saveAction() the following lines:

Mage::dispatchEvent('cms_page_prepare_save', array('page' => $model, 'request' => $this->getRequest()));

It is through dispatchEvent that you can trigger the event in our program. With this line we know that the event is called cms_page_prepare_save and that gives it an array with the page object and the request object, they can be retrieved in your method of you observer through functions getPage() and getRequest() .

That's it for today, I think we went around a lot of new concepts. Now you can have fun with the events and Observers Magento.

Feel free to ask questions and to download the code to compare it with your if necessary.
Available documents for this article :
Questions about this lesson
No questions for this lesson. Be the first !

You must be logged in to ask for help on a lesson.