Considering the number of eCommerce frameworks available in the market, Magento is one of the most popular choices for medium-to-large-scale web shops. Both its feature set and its architecture help set it apart from many of the other options that are available today.
Although there are a lot of third-party extensions for Magento available, it's also easy to develop your own. The purpose of this tutorial is not to walk through how to create a custom extension; however, it does assume that you're familiar with the process as we'll be using a design pattern to implement our own custom module.
Specifically, we'll be looking at the Event-Observer pattern. For those who are unfamiliar, observers register themselves to wait for an event so that they can execute code whenever an event is fired.
As mentioned, this tutorial assumes that you're familiar with how to develop custom modules within Magento, because we're going to create a custom module that employs that design pattern.
Setting Up the Observer
First, we need to declare the observer for our custom event. To do this, enter the following code into your module's config.xml file just inside the global element.
We'll explain the code momentarily.
<global> .. .. <events> <my_custom_event> <observers> <namespace_modulename_my_custom_event_observer> <type>singleton</type> <class>modulename/observer</class> <method>my_custom_method</method> </namespace_modulename_my_custom_event_observer> </observers> </my_custom_event> </events> .. .. </global>
In the code above, we've just declared the observer for the event named my_custom_event
. This simply means that whenever my_custom_event
is fired, it will see that the observer for this event is registered in your module and then execute the code associated with event.
In our case, it'll call the method my_custom_method
defined in the class Namespace_Modulename_Model_Observer
.
Now, let's take this a step further. Notice that in the <events>
tag, you can define the events for which you would like to register the observers. In our case, we've registered the observer for the event <my_custom_event>
. You can also define multiple observers for the same event. In that case, declare all your observers inside the <observers>
tag.
Next, each observer is wrapped with a unique identifier. In our example, it's <namespace_modulename_my_custom_event_observer>
. Furthermore, you need to specify the class name and the method name which will be executed. The <class>
tag specifies the location of the class as per the Magento convention.
In our example, it will be Observer.php
under the model
directory of modulename
module. The method name is declared by the <method>
tag. The value of the <type>
tag is "singleton" which means that it will use the same instance of the observer object for each event call.
Now, after declaring our observer, let's create the observer class and related method code. Create a file Observer.php
under the Model
directory of your module. Copy and paste the following code in that file.
<?php class Namespace_Modulename_Model_Observer { public function my_custom_method($observer) { $event = $observer->getEvent(); // getter method to fetch cid value passed from the dispatcher $cid = $event->getCid(); echo $cid; exit; } } ?>
Here, we've just created the observer class. In the my_custom_method
method, you'll have access to the observer object and the data passed along with it from the dispatcher so that you can use it to do something meaningful.
In this example, we have just fetched the value of the cid
variable passed from the dispatcher code which we'll see in a moment.
Of course, you need to modify the modulename
and namespace
according to your custom module in the class file and XML declaration.
Dispatch Our Custom Event
As we've declared the observer in the custom module, the only thing remaining is to dispatch our custom event.
Let's see how you can use Magento's event dispatcher from your custom module. Write the following code in your controller's method from where you would like to dispatch the event. Having said that, you can also fire the event from other places apart from the controller.
<?php .. $event_data_array = array('cid' => '123'); Mage::dispatchEvent('my_custom_event', $event_data_array); .. ?>
As you can see it's fairly straightforward to dispatch the event from almost anywhere in Magento. The first argument of the dispatchEvent
method is the name of the event and the second argument is used to pass the data to observers.
As you may have noticed earlier in the observer class, we have used the getter method to access the value of variable cid
.
In most cases the event data passed to the observers is in read only mode. More specifically, observers can't change the value of the variables passed by the event dispatcher method.
Granting the Write Access
Assume that you have dispatched a custom event in your module, and you want the observers to be able to modify the original event data passed. Let's revisit the dispatcher code to accomplish this.
<?php .. $event_data_array = array('cid' => '123'); $varien_object = new Varien_Object($event_data_array); Mage::dispatchEvent('my_custom_event', array('varien_obj'=>$varien_object)); // should output '456' as we'll change the value in observer echo $varien_object->getCid(); .. .. ?>
We've just used the special object from Magento Varien_Object
and passed our data using that to make sure any modification to the data is preserved.
You'll also need to change the observer class code accordingly. Here's the modified observer class.
<?php class Namespace_Modulename_Model_Observer { public function my_custom_method($observer) { $event = $observer->getEvent(); // getter method to fetch varien object passed from the dispatcher $varien_object = $event->getVarienObj(); $varien_object->setCid('456'); } } ?>
The only major change in the code is fetching the varien
object and using that to change the value of the array variable cid
.
Summary
Though this is a relatively simple tutorial, it shows how we can easily implement the Event-Observer Pattern in the context of a Magento custom module. At this point, you should be familiar with creating your own custom modules using the Event-Observer pattern.
Please don't hesitate to leave questions and/or comments in the form below!
Comments