Today, we’re going to teach you how to create custom events in Magento 2. An Event is basically an action, which can be anything, a mouse click, a user’s action, a system occurrence, or anything.
And since Magento 2 supports event-driven programming language, you can create any sort of custom events in your Magento 2 web store to extend functionality without affecting the core code in Magento 2.
Now, there are two ways you can extend a core functionality:
- Observer
- Event
What is Observer?
An observer is basically a class which implements Magento\Framework\Event\ObserverInterface interface.
However, you can also set your own custom code or required functionality to be executed as a response.
What is Event?
In Magento 2, events are generally dispatched from the Magento\Framework\Event\Manager class.
And while there are many core events already available to use, but you can also create custom events you want that can be dispatched in your actions’ code.
And in this tutorial, we are going to show you how to create custom events on your own in Magento 2.
Steps to Create Custom Events in Magento 2
In order to get started with creating a custom event, you’ll first need to create a simple module. Now, we have already created a separate tutorial on how to create a simple module in Magento 2.
So, if you don’t know how to create a simple module, you can refer to our tutorial. Once you’ve created your simple module, it’s time we proceed to create a custom event.
Step 1 :
First of all, to create a custom event in Magento 2, you will need to create a file called events.xml at app/code/MD/Helloworld/etc/frontend directory.
Now, copy the below code into the file.
<?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="md_customobserver_log"> <observer name="md_log_text" instance="MD\Helloworld\Observer\CustomObserver" /> </event> </config>
<observer> tag properties:
name: The name attribute indicates the observer’s name. It must be unique per event definition.
instance: The Instance attribute value is a Magento class.
disabled: It is used to set the value of an observer active or not.
shared: Shared attribute determines whether the created instance is shared or not. The default value is always false.
Step 2:
Next, we will be creating a Controller file to declare the observer with CustomObserverFile.php file at app/code/MD/Helloworld/Controller/Index directory.
Now, copy the following code in the file.
<?php namespace MD\CustomObserver\Controller\Index; class CustomObserverFile extends \Magento\Framework\App\Action\Action { protected $resultPageFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory, ) { $this->resultPageFactory = $resultPageFactory; parent::__construct($context); } public function execute() { $resultPage = $this->resultPageFactory->create(); $this->_eventManager->dispatch('md_customobserver_log', ['custom_text' => 'Custom Observer']); $resultPage->getConfig()->getTitle()->prepend(__('Welcome to MD Custom Observer module')); return $resultPage; } } ?>
Step 3:
The next step is to create the Observer file CustomObserver.php in app/code/MD/Helloworld/Observer and add the following code.
<?php namespace MD\Helloworld\Observer; class CustomObserver implements \Magento\Framework\Event\ObserverInterface { public function execute(\Magento\Framework\Event\Observer $observer) { $observer_data = $observer->getData('custom_text'); $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/admin.log'); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info($observer_data); return $this; } }
Step 4:
This is the last step of the process and here we will flush the cache and execute {base_url}/helloworld/index/customobserverfile controller.
Once you execute the controller, you will see the custom_text value displayed in your var/log/admin.log file.
Conclusion
That’s it! You’ve now finally learned how to create custom events in Magento 2. You can go ahead and start experimenting with this tutorial and create your own custom events in your Magento 2 store.
We hope that you found this tutorial helpful. And if you need our professional help with Magento Development Company, feel free to contact us.