Events and Observers in Magento2
We can customize the core functionality of magento2 in different ways and this is one of them.
In this tutorial, we’ll get to know about how we can use events and observers in Magento2.
Events and Observers are used to run your custom code in response to a specific magento event or custom event. We can use the magento events or we can create our custom events to execute our code when that particular event associated with the observer gets dispatched.
Events in Magento2
First, we’ll create events.xml to declare our event and the observer class which will be called on an event dispatch.
events.xml can be created according to area specific.
Vendor/Module/etc/adminhtml/events.xml is used for the admin area.
Vendor/Module/etc/frontend/events.xml is used for the frontend area.
Vendor/Module/etc/events.xml is used for both the frontend and admin area.
Here we are creating our events.xml under etc/frontend/events.xml
<?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="Custom_event_action">
<observer name="display_custom_text"instance="Custom\Module\Observer\CustomText" />
</event>
</config>
Here,
Event name will be the name of the event, we’ll mention our custom event name or magento event name.
Observer name is a name of the observer and it cannot be the same for different observers.
Instance will be the observer class which will be called on an event dispatch.
Dispatch Event
Events can be dispatched using the Magento\Framework\Event\Manager class. We have to inject this class in our controller’s constructor and call the dispatch function of this class and add the event name you want to dispatch also we can pass the array of data to observer using dispatch function.
Create Custom\Module\Controller\Index.php to dispatch an event.
<?php
namespace Custom\Module\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
public function execute()
{
$customtext = new \Magento\Framework\DataObject(array('text' => 'Event Declared'));
$this->_eventManager->dispatch(Custom_event_action, ['cs_text' => $customtext]);
echo $customtext>getText();
exit;
}
}
Observers in Magento2
Observer is a class to add your required functionality on an event dispatch. Observers are created into the Observer\ folder of your module.
The Observer class should implement the Magento\Framework\Event\ObserverInterface class. We can also get data passed by dispatch function in our observer.
<?php
namespace Custom\Module\Observer;
class CustomText implements \Magento\Framework\Event\ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
$displayText = $observer->getData('cs_text');
echo $displayText->getText() . " - Event </br>";
$displayText->setText('Execute event successfully.');
return $this;
}
}
Observers are configured to observe that whenever the event gets triggered the functionality of the observer will be applied.
We can also disable observer using disable attribute in events.xml.
<?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="Custom_event_action">
<observer name="display_custom_text" disabled="true" />
</event>
</config>
Comments
Post a Comment