How To Use Event In Magento 2
In the previous lesson, we learned How To Use Helper And Setting In Magento 2. Following the previous tutorial, we will learn about How To Use Event in Magento 2.
This is a very basic tutorial but it is very important.
Module File Structure
We updated our module file structure looks as follows:
Create Event
You create this file according to the path: Magetop/Helloworld/Controller/Index/TextEvent.php.
<?php
namespace Magetop\Helloworld\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class TextEvent extends \Magento\Framework\App\Action\Action
{
protected $session;
protected $resultPageFactory;
public function __construct(
Context $context,
PageFactory $resultPageFactory,
\Magento\Customer\Model\Session $session
)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
$this->session = $session;
}
/**
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
*/
public function execute()
{
$text = 'Hello ';
$this->session->setTextMessage($text);
$this->_eventManager->dispatch('magetop_hello_display_text_before', ['hello_message' => $text]);
echo $this->session->getTextMessage();
}
}
Catch Event
Catch event is very important because some time you have to custom data or action from core Magento.
Create event in xml file according to the path: Magetop/Helloworld/etc/frontend/events.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* events
*
* @copyright Copyright © 2020 Magetop. All rights reserved.
* @author [email protected]
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="magetop_hello_display_text_before">
<observer name="mb_show_text" instance="Magetop\Helloworld\Observer\DisplayText" />
</event>
</config>
If you want to catch the event in the backend, you can create the following path: Magetop/Helloworld/etc/backend/events.xml.
You can also create the following path: Magetop/Helloworld/etc/events.xml if you want to use it in both frontend and backend environments.
Next, You create this file according to the path: Magetop/Helloworld/Observer/DisplayText.php.
<?php
namespace Magetop\Helloworld\Observer;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
class DisplayText implements ObserverInterface
{
protected $session;
function __construct(
\Magento\Customer\Model\Session $session
)
{
$this->session = $session;
}
public function execute(EventObserver $observer)
{
// TODO: Implement execute() method.
$message = $observer->getData('hello_message');
$message .= ' Magetop'; // change text
$this->session->setTextMessage($message);
}
}
You access the link created on the store page to check it out.
<yourdomain>/helloworld/index/textevent
I hope through this series you can create your own complete module. Good luck!
In addition to How To Use Event In Magento 2, you can read the articles How To Create Admin Grid In Magento 2.
Follow us for the more helpful article!
We hope this is a useful series for you.
Thank you for reading!