Events are dispatched by Magento modules on the trigger of a specific action. Not only that, but Magento also allows you to also create your own custom event that can be dispatched in your code. When the action is triggered, it will pass data to the relevant observer configured for the dispatched event.
Events are always the best approach to add features on top of core modules. The reason being they don’t conflict with other modules, multiple event observers can be attached to a single event.
How to set up events
To add events we first need to decide whether its a frontend event or admin event. Depending on this we need to create a configuration file
For frontend event
1 |
Webiators/Hello/etc/frontend/events.xml |
For admin event
1 |
Webiators/Hello/etc/adminhtml/events.xml |
for now, we will look at frontend events only.
So in our module, we add the file etc/frontend/events.xml, with the following content
1 2 3 4 5 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="controller_action_predispatch"> <observer name="webiators_hello_test_observer" instance="Webiators\Hello\Observer\Predispatch" /> </event> </config> |
The event name should be unique.
Next, let’s define the observer
1 |
Webiators\Hello\Observer\Predispatch.php |
and write the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php namespace Webiators\Hello\Observer; use \Psr\Log\LoggerInterface; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; class Predispatch implements ObserverInterface { protected $logger; public function __construct(LoggerInterface $logger) { $this->logger = $logger; } public function execute(Observer $observer) { $this->logger->warn('Observer Works'); //exit; un commet this to make sure event works } } |
Since we have used the event ‘controller_action_predispatch’, this observer should work on every page. If you open any frontend page, you should see ‘Observer Works’ in the var/log/system.log folder.
Let’s see another example of using events that are a bit more complex.
To find events that are emitted through Magento you need to open up core files and find events. Let’s write a code to log the customer name when he gets logged in.
Looking at the model ‘Magento\Customer\Model\Customer’ we find the relevant event
1 2 3 4 |
$this->_eventManager->dispatch( 'customer_customer_authenticated', ['model' => $this, 'password' => $password] ); |
Let’s write observer for this event in our events.xml file
1 2 3 |
<event name="customer_customer_authenticated"> <observer name="webiators_hello_customer_customer_authenticated" instance="Webiators\Hello\Observer\Customer\Authenticated" /> </event> |
next, let’s create the class
1 |
Webiators\Hello\Observer\Customer\Authenticated.php |
with code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php namespace Webiators\Hello\Observer\Customer; use \Psr\Log\LoggerInterface; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; class Authenticated implements ObserverInterface { protected $logger; public function __construct(LoggerInterface $logger) { $this->logger = $logger; } public function execute(Observer $observer) { $customer = $observer->getModel(); //print_r($customer->getData());exit; $this->logger->warn('Customer Logged IN:' . $customer->getFirstname()); } } |
I have tried to explain this topic in an easy way. However, if any related questions arise, feel free to mention them in the Comments section below.