I am trying to create a custom event and get it to fire in my custom module. How can I do this?
Magento 1.9 – How to Create Custom Events
event-observermagento-1.9module
event-observermagento-1.9module
I am trying to create a custom event and get it to fire in my custom module. How can I do this?
Best Answer
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.
In the code above, we've just declared the observer for the event named
my_custom_event
. This simply means that whenevermy_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 ofmodulename
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 theModel
directory of your module. Copy and paste the following code in that file.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
andnamespace
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.
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.
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.
The only major change in the code is fetching the
varien
object and using that to change the value of the array variablecid
.For more details visit tutsplus.