Magento is the most widely used eCommerce platform among the top 1000 Retailers in North America including big brands like Walmart, Adidas etc. Let’t get started with Magento Services for your business as well.
Events & Observers are mainly used to extend the original behavior of Magento 2 framework. Using events & observers, business logic can be changed and you can run your custom code in response to a specific Magento event or even a custom event. It enables loose coupling between the modules.
The events and observers implementation in Magento 2 are based on the publish-subscribe pattern. In software architecture, publish–subscribe is a messaging pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers, but instead characterize published messages into classes without knowledge of which subscribers, if any, there may be. Similarly, subscribers express interest in one or more classes and only receive messages that are of interest, without knowledge of which publishers, if any, there are.
In Magento 2, events are dispatched to various places especially when certain actions are triggered. Observers are classes which will be executed when the respectively mapped event is dispatched. Magento can execute any number of observers for a dispatched event.
Events
Events will be dispatched with the help of MagentoFrameworkEventManagerInterface. This Interface is responsible for dispatching the events.
Magento/Checkout/Model/Cart.php
In this example, we are dispatching events ‘checkout_cart_save_before’ and ‘checkout_cart_save_after’. So now we are free to perform any actions before / after the cart has been saved. This highly avoids rewriting the entire class.
Magento allows us to pass values to the observers(that we can make use of) as you see in above example.
Creating Custom Events
We can also dispatch custom events in our modules by simply creating an object for MagentoFrameworkEventManagerInterface and passing the unique custom event name to the object.
$this->_eventManager->dispatch(‘custom_action_after_save’, [‘param’ => $this]);
Parameters are optional.
Mapping Events and Observers
Events.xml files is responsible for configuring the observers to certain events.
<observer> node has following properties
- name – Observer Name. Must be Unique per event definition.
- instance – Magento class that needs to be executed when the event is dispatched.
- disabled – Whether the observer is active or not.
- shared – Determine whether the instance to be created is shared or not. Default is false.
Observers
Observers are Magento classes, used to extend the Magento functionality. Observers are executed when the mapped event is dispatched. Event Manager is responsible for dispatching events and executing the respective observers.
The above structure is a basic observer class. Note that Observer class must implement MagentoFrameworkEventObserverInterface.
Following the observer pattern is the preferred method of extending the Magento functionality. Using Observers for @api classes is the most preferred way. We hope that this blog has helped you to understand the ways observers and events are used in Magento 2.