Event and Observers in Magento 2

event_and_observer_magento

What is Event and Observer in Magento 2?

Overview

Working with events and observers is one of the main ways to extend Magento functionality. Using events and observers, you can run your custom code in response to a specific Magento event or even a custom event.

Events:

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.

Ex: catalog_product_load_after

Understand Catching and handling a event

Magento use area definition to manage the store. We will have a frontend area and admin area. With the configuration file, they can be put in 3 places:

  • Under etc/ folder is the configuration which can be used in both admin and frontend.
  • Under etc/frontend folder will be used for frontend area.
  • Under etc/adminhtml folder will be used for admin area.

The same with the event configuration file. You can create events configuration file for each area like this:

  • Admin area: app/code/Dzinehub/EventObserver/etc/adminhtml/events.xml
  • Frontend area: app/code/Dzinehub/EventObserver/etc/frontend/events.xml
  • Global area: app/code/Dzinehub/EventObserver/etc/events.xml

Initialize event.xml

<?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="my_module_event_before">
		<observer name="myObserverName" instance="MyCompany\MyModule\Observer\MyObserver"
/>
	</event>
	<event name="my_module_event_after">
		<observer name="myObserverName" instance="MyCompany\MyModule\Observer\AnotherObserver" />
	</event>
</config>

Observers:

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.

Observers are also called as interceptors

Observers are also useful when you need to log something specific

Create Observer class

<?php
namespace Dzinehub\EventObserver\Observer;
class ChangeDisplayText implements \Magento\Framework\Event\ObserverInterface
{
	public function execute(\Magento\Framework\Event\Observer $observer)
	{
		$displayText = $observer->getData('dz_text');
		echo $displayText->getText() . " - Event </br>";
		$displayText->setText('Execute event successfully.');
		return $this;
	}
}

Compared to Plugins:

Plugins are also called interceptors because they can intercept the function before, during, and after the call. See, the thing is you can change almost any function with a plugin:

  1. with Before plugins developers can alter input arguments;
  2. with After plugins they can change the result of the function;
  3. and using Around plugins they can completely change the logic of the function.

Compared to event observers, plugins can function anywhere in the system.

They can be used to change or override any public method in Magento.

Plugins are a valuable tool in custom development. For example, they are not affected by new Magento patches.

For me the main diff bet plugins and observers is:

Plugins can modify only public methods while observers can modify private, protected as well.

There is sort order for plugins but there is no sort order for observers.

You can add observer only to the events that are already dispatched in Magento. Plugins are more flexible here.

Event observers have disadvantages:

  1. observers are slower than plugins because Magento creates at least 3 objects upon the trigger event;
  2. observers offer less flexibility when you need to introduce platform-wide features;
  3. event observers have critical limitations since they MUST NOT change the objects that they influence.

When to use ?

Plugins and observers both can be used to run our custom script after certain Magento 2 – events or public methods are executed.

Which one to use ?

When we need to modify Magento core functionality (Ex: adding additional data to order collection object) then we have to use plugins. In this case we can’t use Events & Observer.

When we want to run our customization after certain event is dispatched without disturbing Magento Core functionality then we have to choose Event & Observer pattern.

About the author: dzine

Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes.