How To

How to Create Custom Dispatch Event in Magento 2

The coolest thing about Magento is that it supports the Event-driven programming model. The event simply means action and it can be anything like user actions, mouse click, system occurrences or anything. And the system which responds to events is generally known as an event-driven programming language. The entire concept is pretty straightforward. When an event gets fired, and your observers catch the event and executes a certain piece of code. And the Best part in that, you can write your custom code apart from the core modification.
In Magento 2, events can be dispatched using the Magento\Framework\Event\Manager class. While developing a custom extension, we need to create a custom dispatch event, so in future, if any other developer needs to extend functionality at that time they can use this dispatch event. So, here it goes.

In the first step, we need to create “My event.php” file to perform particular action.
app\code\vendor\extension\Controller\Index\Myevent.php

namespace Vendor\Extension\Controller\Index;

class Myevent extends \Magento\Framework\App\Action\Action
{
 public function execute()
 {
  $nameobj = new \Magento\Framework\DataObject(array('name' => 'Magecomp'));
  $this->_eventManager->dispatch('magecomp_customevent_update_name', ['obj' => $nameobj]);
  //$this->_eventManager this object has already created in Action Class so don’t need to create again.
  echo $textDisplay->getText();
  return true;
 }
}

In next step, we need to create “events.xml” file for calling custom dispatch event.
app\code\vendor\extension\etc\frontend\events.xml



    
        
    

In last step, we need to create Event Observer file by creating “Updatevalue.php”.
app\code\vendor\extension\observer\Updatevalue.php

namespace Vendor\Extension\Observer;

class Updatevalue implements \Magento\Framework\Event\ObserverInterface
{
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
  $displayname = $observer->getData('obj');
  echo $displayname->getName() . " - Event ";
  $displayname->setText('Execute event successfully.');

  return $this;
 }
}

That’s it! You have successfully created your custom dispatch event in Magento 2. You are free to play and manipulate this according to your need for creating custom dispatch event in Magento 2 Extension.
That’s it for today, Let us know if you are facing an issue while implementing using this code by commenting below.
Happy Coding!

Click to rate this post!
[Total: 10 Average: 4.2]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

10 hours ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

10 hours ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

1 day ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

3 days ago

HYVĂ„ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

3 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

4 days ago