Magento 2 Observers

- Fire development, Magento 2

Observers in Magento 2

This article sheds light on Magento 2 observers – event handlers that listen to events they are attached to and respond to these events. Since Magento 2 signifiantly differs from 1.X, the observers registration and creation include some new aspects described below.

'
  • The first important change is the usage of an independent events.xml file, which is necessary for observer registration.
  • The second difference introduced in Magento 2 is related to the way the system defines the scope of the observer. If previously the xml node has been used, now the system relies on the events.xml file location.
  • The last vital aspect you should pay attention to is a revamped XML syntax.

Magento 2 Observers: Separate events.xml

As we’ve mentioned above, Magento 2 stores all module observers in an independent xml file under:

Another place where you can find them is:

Let’s have a look at the example of an xml file:

You can see the event node and the name attribute that defines a special event which acts as a trigger that calls the observer.

As for the observer node, it defines not only the observer itself, but also its attributes, such as:

  • name – the registration name of the observer. Not that it shouldn’t coincide;
  • instance identifies a class and a method executed if a definite event takes place;
  • method – the executed method.

Now, let’s see how the observer looks like (app/code/vendor/module/Model/Observer.php):

The  only significant difference here is related to the way the classes are named. In the aforementioned example, the verificate function receives a current observer object as an argument.

Magento 2 Observers: The observer scope definition

Now, let’s shed light on the observer scope definition mentioned above. Just look at the following config.xml file:

In case of 1.x, this approach has been used to define the observer on the frontend only. However, Magento 2 defines the scope of the observer by the events.xml file and its location.

For frontend it is

For admin:

For both locations:

Source

Magento 2 Observers: Other useful materials

If you are not familiar with events.xml configuration files, they contain event/observer configuration. Their stage is characterised with two terms: global and area. As for the Configuration object, it is \Magento\Framework\Event. For further information, check this manual: Module configuration files.

In its turn, Magento\Framework\Event includes the code responsible for publishing synchronous events as well as handling observers for any Magento event. For further information, follow this link: Magento Framework.

The way you can get the list of events/observers in Magento 2 is described here: How to get Magento 2 events/observers.

If you want to handle events:

  • Take a module’s events.xml file and declare event handlers there.
  • Check the following example of event handler declaration:

Now, you can create a new event handler class. Note that it should implement ObserverInterface with a single execute method. The interface should be handled as follows:

For further information, check this post: How to handle events in Magento 2.

____

Have any questions regarding Magento 2 observers? Leave them in comments

'