Skip to content

Latest commit

 

History

History
31 lines (28 loc) · 1.88 KB

File metadata and controls

31 lines (28 loc) · 1.88 KB

Event and Event Dispatcher

  • First create Custom Event class For event And containing all information to be transported to the listeners and this is basic php class that contain methods and member variables. check here FrontendRendringEvent.
  • Then Create EventListner Class of this Event in Classes Folder
  • Register the Event listener of custom event in Services.yaml file like.
MyVendor\MyExtension\EventListener\MyEventListener: 
      tags:
            - name: event.listener
            identifier: 'myeventidentifier'
            event: MyVendor\MyExtension\Event\MyEvent 
public function __invoke(MyEvent $event): void
{
      //write your logic here
}
  • Create an event object with the data that should be passed to the listeners. Use the data as it suits your business logic:
  • If no attribute method is given, the class is treated as invokable, thus its __invoke() method will be called:
  • After Add Event Dispatcher
$this->eventDispatcher->dispatch(
      // your event
      new MyEvent()
);
  • A complete list of all registered event listeners can be viewed in the the module System > Configuration > Event Listeners (PSR-14).