Events are commonly used in applications to handle external actions or input when, for example, a user clicks a mouse. Each action is interpreted as an event. Events are part of the Event-Observer pattern. This design pattern is characterized by objects (subjects) and their list of dependents (observers). Events trigger objects to notify their observers of any state changes, usually by calling one of their methods. It is a very common programming concept that works well to decouple the observed code from the observers.
Magento 1 shares the same concept, although the execution is different. If you needed to trigger an event at a certain place in your code in Magento 1, it would be fired by calling Mage::dispatchEvent(), while in Magento 2 there is a special event manager class – Magento\Framework\Event\Manager that fires events. This class can be obtained through dependency injection by defining the dependency in your constructor.
Look for the mentioned notes in the code Magento1:
Mage::dispatchEvent(‘event_name’, $event_arguments);
This note is an “observer” that allows executing methods attached to it, transferring $event_arguments data array into methods.
Look for the mentioned notes in the code Magento2:
$this->eventManager->dispatch(‘event_name’,[‘myEventData’=>$event_arguments]);
Event Observers in Magento1 can be configured and described in config.xml:
1 2 3 4 5 6 7 8 9 10 11 12 |
<frontend><!-- this is depending on the event area (frontend, adminhtml or global)--> <events> <my_module_event> <observers> <observer_name> <class>moduleName/observer</class> <method>methodName</method> </observer_name> </observers> </my_module_event> </events> </frontend> |
An observer class should have the method name declared in config.xml
1 2 3 4 5 6 7 |
class Namespace_Modulename_Model_Observer { public function methodName(Varien_Event_Observer $observer) { …. } } |
Event Observers in Magento2 can be configured in a separate file event.xml. It should be created in <module-root>/etc directory, if observer is associated with globally events, or in <module-root>/etc/frontend and <module-root>/etc/adminhtml if observer is to only watch for events in frontend or adminhtml areas.
1 2 3 4 5 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="my_module_event"> <observer name="observer_name" instance="Namespace\Modulename\Observer\MyObserver" /> </event> </config> |
In instance attribute we declare observer class name. This class has to implement Magento\Framework\Event\ObserverInterface::execute(Observer $observer) method. The $observer object has an $event object (available through $observer->getEvent()), which contains the event’s parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
namespace Namespace\Modulename\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Event\Observer; class MyObserver implements ObserverInterface { public function __construct() { //You can use dependency injection } public function execute(Observer $observer) { ... } } |
In Magento1 we can use automatically available events. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
protected function _beforeSave() { ... Mage::dispatchEvent($this->_eventPrefix.'_save_before', $this->_getEventData()); ... } protected function _getEventData() { return array( 'data_object' => $this, $this->_eventObject => $this, ); } |
This event triggered before saving object, if it extends Mage_Core_Model_Abstract class. When we create a new class, which extends Mage_Core_Model_Abstract, we can declare $eventPrefix = “namespace_modulename” and use new event namespace_module_save_before.
There’s the same ability in Magento2. In class Magento\Framework\Model\AbstractModel:
1 2 3 4 5 6 |
public function beforeSave() { ... $this->_eventManager->dispatch($this->_eventPrefix . '_save_before', $this->_getEventData()); …. } |
In Magento1 and Magento2 we have the same automatically available events.
Models:
[$eventPrefix]_load_before
[$eventPrefix]_load_after
[$eventPrefix]_save_before
[$eventPrefix]_save_after
[$eventPrefix]_save_commit_after
[$eventPrefix]_delete_before
[$eventPrefix]_delete_after
[$eventPrefix]_delete_commit_after
Controllers:
controller_action_predispatch_[ROUTE_NAME]
controller_action_predispatch_[FULL_ACTION_NAME]
controller_action_postdispatch_[ROUTE_NAME]
controller_action_postdispatch_[FULL_ACTION_NAME]
controller_action_layout_render_before_[FULL_ACTION_NAME]
Using observers in Magento is a good practice, but in process of development in Magento1 often this tool was not enough and we had rewrite classes. So in Magento2 a new tool “plugin“ was added.