In this article, you going to see a discussion regarding Magento 2 Create Custom event and observer programmatically in detail and step by step.
If you are looking for this topic, then you are at a right place.
Let’s start with topic Magento 2 Create Custom Event and Observer Programmatically
To Create custom event and observer is very simple.
With the help of Event name, Magento 2 Supports in Creating Custom Event Observer.
First you have to create module.
You can create custom event in exist module.
In the next step, under your module define event using the method called dispatch()
method and events.xml
file.
With the help of Observer Class, You can handle the Events data.
Our next step is the Definition of dispatch()
method to create custom Event
public function dispatch($eventName, array $data = []);
Parameter detail information :
$eventName
: The dispatch method is get called with $eventName
is your custom event name.$data
: It specifies an array to get value inside the Observer class.
After the declaration of event is done.
Next step is to define events using the events.xml file.
For Event list you can check Full List of all events in Magento 2.
As per your requirement you can define your events in events.xml file.
There are five type of Location where you can define your events.xml file are as follow.
- Global:
Insideetc/
the folder is the configuration that is used for both admin and frontend. - Frontend area:
Insideetc/
frontend folder which is used for the frontend area. - Back End :
Insideetc/
adminhtml folder which is used for the admin area. - REST API Events:
Insideetc/
webapi_rest folder which is used for the REST API area. - SOAP API Events:
Insideetc/
webapi_soap folder which is used to the SOAP API area.
Now main important point is to Create a Simple Module to define Custom Event with Magento 2 Way.
First Step : Dispatch Event with the help of Controller
Now create a custom event for our module with the help of the Controller file.
To do this we need to first define event.
Below is the code snippet from which you can understand.
Module Name: Techone_CustomEvent
.
Create file name index with the path app/code/Techone/CustomEvent/Controller/Index/Index.php
<?php
namespace Techone\CustomEvent\Controller\Index;
use Magento\Framework\DataObject;
use Magento\Framework\App\Action\Action;
class Index extends Action
{
public function execute()
{
$customData = new DataObject(['record_id' => '10001', 'name' => 'record_name']);
// define custom event name with array data
$this->_eventManager->dispatch('techone_custom_event_observer',
[
'record' => $customData
]
);
}
}
Our Custom Event Name is techone_custom_event_observer
event.
You can also create a custom event using Plugin.
Plugin with controller execute method.
Then You need to define $_eventManager
using the ManagerInterface class file path is Magento\Framework\Event\ManagerInterface.
Below is code snippet for it.
<?php
use Magento\Framework\Event\ManagerInterface as EventManager;
/**
* @var EventManager
*/
private $_eventManager;
public function __construct(
EventManager $eventManager
) {
$this->_eventManager = $eventManager;
}
public function aroundExecute(Classname $subject, callable $proceed)
{
$this->_eventManager->dispatch('techone_custom_event_observer',
[
'record' => $customData
]
);
$result = $proceed();
return $result;
}
Second Step : Magento 2 Create Custom Event
Create a event file with the file path : app/code/Techone/CustomEvent/etc/events.xml
Below is code snippet from which you can understand easily.
<?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="techone_custom_event_observer">
<observer name="your_observer_name"
instance="Techone\CustomEvent\Observer\GetRecord"/>
</event>
</config>
The attribute "name"
is our custom event.
The name "instance"
is a class name, where we have to handle events
Third Step : Magento 2 Create Custom Event
Create Observer file with the file path app/code/Techone/CustomEvent/Observer/GetRecord.php
to handle our event with execute()
methods.
Below is code snippet.
<?php
namespace Techone\CustomEvent\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class GetRecord implements ObserverInterface
{
public function execute(Observer $observer)
{
$recordData = $observer->getEvent()->getRecord();
$recordId = $recordData->getRecordId(); //get record id
$name = $recordData->getName(); //get record name
return $this;
}
}
Using a $observer
argument, You can fetch Event Data.
As we have passed 'record'
array using our custom event.
Next step is to capture that record data under the observer class.
After this run the below command.
bin/magento cache:clear
Conclusion :
Now we have done with point of Magento 2 Create Custom Event and Observer Programmatically
I hope you like this article and if you have any query, please comment below.