How to create custom event in Magento 2
Magento 2, Using events and observers, you can run your custom code in response to a specific Magento event or even a custom event. 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.
You can define events.xml based on area. Below the type of location, you can define events.xml file.
Introduce several ways to create custom event in Magento 2, Taken Dolphin as Package name and CustomEvent as Modulename for simplicity. code snippets are under a module named Dolphin_CustomEvent.
Before creating the custom event, we need to create a new module We already have a blog on “how to create a basic module in Magento 2“. We need to create module.xml and registration.php file. You can create module.xml and registration.php from this tutorial.
Create a “Index.php” file in the app/code/Dolphin/CustomEvent/Controller/Index folder with the following code.
<?php namespace Dolphin\CustomEvent\Controller\Index; use Magento\Framework\DataObject; use Magento\Framework\App\Action\Action; class Index extends Action { public function execute() { $blogData = new DataObject(['blog_id' => '1', 'blog_title' => 'Blog Title']); $this->_eventManager->dispatch('dolphin_custom_event_blog', [ 'blog' => $blogData ] ); echo "Dolphin Custom Event";exit; } }
Read this: Event Web API Rest and Web API Soap in Magento 2
Create an “events.xml” file in the app/code/Dolphin/CustomEvent/etc folder with the following code.
<?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="dolphin_custom_event_blog"> <observer name="dolphin_blog_custom" instance="Dolphin\CustomEvent\Observer\GetBlog"/> </event> </config>
This event file for admin and frontend both areas.
A name attribute is a custom event.
An instance is a class name, we have to manage events.
Create a “GetBlog.php” file in the app/code/Dolphin/CustomEvent/Observer folder with the following code.
?php namespace Dolphin\CustomEvent\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; class GetBlog implements ObserverInterface { public function execute(Observer $observer) { $blogData = $observer->getEvent()->getBlog(); $blogId = $blogData->getBlogId(); $blogTitle = $blogData->getBlogTitle(); $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/blog.log'); //custom log file $logger = new \Zend_Log(); $logger->addWriter($writer); $logger->info('blog id: '.$blogId); $logger->info('blog title: '.$blogTitle); return $this; } }
you can fetch data in $observer argument.
Run Command [dt_highlight color=”” text_color=”” bg_color=””]php bin/magento c:c[/dt_highlight]
Goto to the magento root/var/log you see outr custom log file blog.log
[dt_code]2021-10-05T12:04:30+00:00 INFO (6): blog id: 1
2021-10-05T12:04:30+00:00 INFO (6): blog title: Blog Title[/dt_code]
I Hope, This instruction will be helpful for you.
If you have any difficulties regarding Magento blog, do consider them posting in the Comments section below!
I’m here to help.
Thank you!