how to create custom event and observer in Magento 2

How to create custom event in Magento2

In today’s post we will see how to create a custom event in Magento 2. Events are one of the best choices to extend functionality of Magento without making changing in core files. In Magento2 event and observer system is based on publish-subscribe pattern.  Magento modules dispatch a lot of useful events after performing some task and we can hook those events using observers to perform our tasks.

Sometimes we need to create our own custom event which can be used throughout the modules. It can be used in modules which will be dependent on the module we are building. Let’s see how to dispatch a custom event and hook it using observer.

create custom event and observer magento 2

In Magento2 events can be dispatched using  the Magento\Framework\Event\Manager class. To use this class we will have to inject the instance of this class in constructor using dependency injection.

Below code can be used to dispatch an event in Magento2

namespace Mega\EventDemo\Controller\Index;
use Magento\Framework\Event\ObserverInterface;
class Index{
  /**
  * @var EventManager
  */
  private $eventManager;

  public function __construct(\Magento\Framework\Event\Manager $eventManager){
    $this->eventManager = $eventManager;
    /* some more dependencies */
  }

  public function something(){
    $eventData = array();
    // Code...

    $this->eventManager->dispatch('mega_my_custom_event',['custom_data'=>$eventData]);
  }
}
The above code dispatches an event named "mega_my_custom_event" and passes $eventData with event. This $eventData will be available in observer when any observer hooks the above event. Now let's see how to hook this event. create a file di.xml in etc folder of your module ,add below xml code to it
<?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="mega_my_custom_event"> <observer name="myObserverName" instance="Mega\EventDemo\Observer\EventDemoObserver" /> </event> </config>

After the above code is added di.xml , we will have to create an observer for this event. Create a class EventDemoObserver in Mega\EventDemo\Observer namespace and add below code to it.


namespace Mega\EventDemo\Observer;
use Magento\Framework\Event\ObserverInterface;
class EventDemoObserver implements ObserverInterface{
    public function execute(\Magento\Framework\Event\Observer $observer)
 {
    $eventData = $observer->getEvent()->getCustomData();
    //some more code 
 }
}
    
  

All required classes can be injected in observer using dependency injection in constructor. Event Areas Normally di.xml is placed inside etc directory. when di.xml is placed inside etc folder it will watch for the events globally. To watch for specific are place di.xml inside etc/adminhtml and etc/ frontend folder. etc/adminhtml will watch for the events dispatched in admin area while etc/frontend will watch for events dispatched in frontend. QUICK TIP: You should always give a unique name to your event. Its a good idea to prefix your module namespace and module name in event to make it unique.

some of the best practices that must be taken care are available at Magento 2 event best practices 

That’s all for this post. If you need any help feel free to connect with us on http://codeinnovers.com/contact-us.

Add a Comment

Your email address will not be published. Required fields are marked *