Pages

Monday 23 June 2014

Magento Developper’s Guide (Lesson 11) – Events and Observers in magento

This tutorial is the 11th of many tutorials , you will now learn how to create your own controller in Magento. If you have not read the first articles yet, I strongly advise you to do so.

Today we will see a new way to modify a base behavior of magento thanks to the Events and the Observers. This kind of programming method is a web development standard I recommend you to master if you want to work with Magento.

A bit of theory

This programming method is called a « design pattern », what that is?

First, we will start by defining two terms:

Event:

 We can consider an event as a kind of flag that rises when a specific situation happens for example when the user presses the « pay » button of your website, it is an event. Your order has been registered? Here it is an event too. etc …

Observer:

 An Observer is also called « Listener » he will listen to your program to detect events. When there is an event taking place, it will perform an action.

A small example: The neighbor and you

Let us talk a bout a situation , you’re at home and you host a party ! you start the night party and everything goes well, then one time, one of your friend who drank a little too much (Ok he’s completely drunk  !) begins to sing loudly in the garden

Your neighbor is a « no life » and has never had a friend, he’s at home all the time (worst !! maybe it is a magento developer !) And it has nothing to do best of his life waiting you make too much noise to be able to call the police (for disturbing the peace) … that’s how he like to spent his time (we need all kinds of people to make a world).

In short, we all know that kind of situation. Now in this story, a friend makes too much noise, « making too much noise » here is the event .

 The observer is your neighbor, and he watches what? If you make too much noise (if an event is raised). And there will be action when the event is raised: call the police!

With this great example (in which I’m not talking about real facts … ), you get the principle i hope! Now let’s go for practice and we will move to our favorite e-commerce CMS Magento …!

Create an Observer

An Observer in magento is in the directory Model of your plugin and extends the class Varien_Event_Observer (class which I think is clear …), go in /app/code/local/Easylife/Test/Model/ folder and create your Observer.php

<?php
class Easylife_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’ve created an Observer with a function saveCmsPageObserve , this function is the method that will be executed when a page CMS will be saved in the backend of Magento.

In our example we stops the program to display a message but instead of a true die(), you will create a process to record additional information on your page in another table (for exemple).

I think I do not need you to explain this short code, I get the event using the method getEvent on the Observer we passed as parameter of the method (the parameter is automatically sent to the method). Then starting from the event, from the method getPage(), I get my cmspage and I display its content with print_r and the getData as we are used to.

Ensures that the Observer is declared in Magento

Now we have created our Observer , we need to declare it in Magento, how it’s done? … 11th tutorial, if you have not an idea I urge you to restart this series of tutorials from the beginning because I think you have not understood the Magento development principles.

SO…if you have understood, we will edit the config.xml our plugin. So open the file:/app/code/local/Easylife/Test/etc/config.xml
And in , after or after add:


<strong><events>
   <cms_page_prepare_save>
    <observers>
       <Easylife_Exemple_Model_Observer>
           <type>singleton</type>
           <class>exemple/observer</class>
           <method> saveCmsPageObserve</method>
           </ Easylife_Exemple_Model_Observer>
       </observers>
  </cms_page_prepare_save>
</events>
</strong>
 
Some small

explanations are necessary here:

Tag:

<strong> <cms_page_prepare_save> </strong>
 
Represent the event is monitored in our example your neighbor was waiting you to do a lot of noise in order to launch its action (call the cops). Here we expect the action cms_page_prepare_save to launch our action saveCmsPageObserve ).
The rest seems logical:


<strong>    <observers>
      <Easylife_Exemple_Model_Observer>
     <type>singleton</type>
     <class>exemple/observer</class>
     <method> saveCmsPageObserve</method>
         </ Easylife_Exemple_Model_Observer>
    </observers>
</strong>
 
We define the class observer that will be used when this method will be called with the type, class and method to use.

Ok I understand but how do we know that it is the event cms_page_prepare_save  wich is used?
It is not complicated, in fact we will see in the core file, it opens the file:

\app\code\core\Mage\Adminhtml\controllers\Cms\PageController.php look at the method saveAction() , at the following line:


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

It is by dispatchEvent that generates the event in our program.

With this line we know that the event is called cms_page_prepare_save and that he passes a table with a page that contains the model object and an request object, they may be recovered in your observer method thanks to the features getPage() and getRequest() .

I think we went around. Now you can have fun with the events and Observers in Magento.

Leave a comment or ask a question and if you liked this article help me by sharing a link to this article on your blog, Facebook page or twitter.


 

Find the summery of this tutorial


No comments:

Post a Comment