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:
1 2 3 4 5 6 7 8 9 10 11 |
/** * Processing object after load data * * @return $this */ protected function _afterLoad() { $this->_eventManager->dispatch('model_load_after', ['object' => $this]); $this->_eventManager->dispatch($this->_eventPrefix . '_load_after', $this->_getEventData()); return $this; } |
Just let it sink in to you what actually happens here:
- There are 2 events dispatched:
- model_load_after , with the model in question as a parameter
- ($_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:
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="cms_page_render"> <observer name="gb_cms_page_render" instance="Gielberkers\Example\Model\Observer\Cms\PageRender" /> </event> </config> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php namespace Gielberkers\Example\Model\Observer\Cms; use Magento\Framework\Event\ObserverInterface; /** * Class PageRender * @package Gielberkers\Example\Model */ class PageRender implements ObserverInterface { /** * @param \Magento\Framework\Event\Observer $observer */ public function execute(\Magento\Framework\Event\Observer $observer) { /** @var \Magento\Cms\Model\Page $page */ $page = $observer->getPage(); $content = $page->getContent(); $page->setContent(strtoupper($content)); } } |
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.1 out of 5.
How would you rate this article?
★ ★ ★ ★ ★