Events and observers
Working with events and observers is one of the main ways to extend Magento functionality. The events and observers implementation in Magento 2 is based on the publish-subscribe pattern. Using events and observers, you can run your custom code in response to a specific Magento event or even a custom event.
Events are dispatched by modules when certain actions are triggered. In addition to its own events, Magento allows you to create your own events that can be dispatched in your code. When an event is dispatched, it can pass data to any observers configured to watch that event.
To dispatch an event, call the
dispatch function of the event manager class and provide it with the name of the event you want to dispatch along with an array of data you wish to provide to observers.
The following example shows you how to dispatch an event with and without an array of data.
Creating new events
Custom events can be dispatched by simply passing in a unique event name to the event manager when you call the
dispatch function. Your unique event name is referenced in your module’s
events.xml file where you specify which observers will react to that event.
Generally, the location of the
events.xml file will be under the
<module-root>/etc directory. Observers that are associated with events here will watch for these events globally. The
events.xml file can also be defined under the
<module-root>/etc/adminhtml directories to configure observers to only watch for events in those specific areas.
Observers are a certain type of Magento class that can influence general behavior, performance, or change business logic. Observers are executed whenever the event they are configured to watch is dispatched by the event manager.
Creating an observer
To create an observer, you must place your class file under your
<module-root>/Observer directory. Your observer class should implement
Magento\Framework\Event\ObserverInterface and define its
Below is an example of the basic observer class structure:
One of the more powerful feature of observers is that they are able to use parameters passed into the event when it was dispatched. Below is an example of an observer obtaining data passed in when the event was dispatched.
Subscribing to events
Observers can be configured to watch certain events in the
observer xml element has the following properties:
name(required) - The name of the observer for the event definition.
instance(required) - The fully qualified class name of the observer.
disabled- Determines whether this observer is active or not. Default value is false.
shared- Determines the lifestyle of the class. Default is false.
Below is an example of how to assign observers to watch certain events:
In the preceding example, we assign the observer
MyObserver to the custom event
Observer names must be unique per event definition. This means that you cannot have two observers with the same name in the same event definition. In the example, both observers have the name
myObserverName. This is acceptable because each of those observers belong to different event definitions.