·

photo credit: 三猿 / San Saru, The three wise monkeys, Nikko, Japan via photopin (license)

Events and Observers in Magento 2

Tags: ,

Just like Magento 1, Magento 2 offers different ways to achieve your goal in your webshop. One of these things are all the events dispatched by Magento. So before you start rewriting models, overriding templates or adding new classes, chances are that you can get things done, just by listening what Magento has to offer. In this article I’ll explain you how.


Please note that we use the boilerplate module from the article ‘Creating a module in Magento 2‘ as the entry point for this article.

Events dispatched by Magento

Magento dispatches tons of events. Some of them are dispatched automatically, others are dispatched intentionally by other scripts. To get a deeper understanding of what events are actually dispatched, take a look at Magento\Framework\Model\AbstractModel  and it’s _afterLoad() -method:

Just let it sink in to you what actually happens here:

  • There are 2 events dispatched:
    1. model_load_after , with the model in question as a parameter
    2. ($_event_prefix)_load_after , which is a generated event name according to the $_event_prefix -parameter that each model has.

And if you look further in this class, you’ll notice that there are tons of events dispatched like this; before- and after save, before- and after load, before- and after deletion, etc…
So what does this actually do? Well, since it’s an abstract class, and all models extend this abstract class, a lot of events are dispatched automatically. Take the product model for instance (Magento\Catalog\Model\Product ). This has the event prefix set to catalog_product . This means that each time you load a product model, the following events are dispatched:

  • model_load_before
  • catalog_product_load_before
  • model_load_after
  • catalog_product_load_after

Pretty neat huh? And those are only the events that are dispatched automatically. There are also a lot of custom events dispatched throughout Magento. It’s one thing that makes Magento extremely flexible to work with (but also one of the things why it’s so CPU intensive, but that’s beyond the scope of this topic).

How can we use these events?

Using events is very simple. The first thing you need to do is determine whether you want to use the event in the frontend or the backend. For example, if we want to cms_page_render -event on the frontend, create a file called events.xml  and put it in the etc/frontend -folder of your module:

As you might have guessed, when this event is dispatched, it looks for a class called Gielberkers\Example\Model\Observer\Cms\PageRender . In this class, Magento 2 will call the execute() -method. This is a big difference with Magento 1, where you would mostly have one observer for all your event handling (although that’s also a manner of choice). If you ask my, Magento 2’s approach is more SOLID.
Anyway, here’s the class:

Not very complex logic going on here. Just grab the content of the CMS page and make it all uppercase. Just your ordinary client request.
This pretty much sums up events in it’s most basic form. Utilise it to the fullest!

This post is part of the series Magento 2 Development from Scratch. See how you can use events to remove the wrapping paragraph around a widget in Magento 2.

Visitors give this article an average rating of 4.8 out of 5.

How would you rate this article?

Leave a Reply