In this section, we will learn how to define custom event, how to dispatch custom event and how to subscribe custom event.
There will be following things which we we will see step wise step :
Step 1 :- Create modulename.info.yml file where filename is "drupal_custom_events.info.yml", here our module name is "drupal_custom_events".
name: Drupal Custom Events
type: module
description: 'This module will guide us about drupal events.'
package: Custom
version: 1.0
core_version_requirement: ^8 || ^9
Step 2 :- Create a routing file drupal_custom_events.routing.yml where we will define a route and call a form.
drupal_custom_events.custom_event_dispatch_form:
path: 'event_dispatch_form'
defaults:
_form: '\Drupal\drupal_custom_events\Form\CustomEventDispatchForm'
_title: 'Event Dispatch Form'
requirements:
_permission: 'administrator'
Step 3:- Create a Events folder inside src folder then create a file "CustomEvent.php". This file tell us about our custom event. "CUSTOM_FORM_SUBMIT" is our custom event name.
<?php
namespace Drupal\drupal_custom_events\Events;
use Drupal\user\UserInterface;
use Symfony\Component\EventDispatcher\Event;
/**
* Custom Events will be fired.
*/
class CustomEvent extends Event {
const CUSTOM_FORM_SUBMIT = 'custom_form_submit';
/**
* A Reference ID
*/
public $referenceId;
/**
* Constructs the object.
*
*/
public function __construct($referenceId) {
$this->referenceId = $referenceId;
}
/**
* getReferenceId()
*/
public function getReferenceId() {
return $this->referenceId;
}
/**
* getEventDescription()
*/
public function getEventDescription() {
return "This is custom event!";
}
}
Step 4:- Create a "Form" folder inside src folder then give filename "CustomEventDispatchForm.php" according to our routing file. When this form will submit then we dispatch custom event.
<?php
namespace Drupal\drupal_custom_events\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\drupal_custom_events\Events\CustomEvent;
/**
* Class CustomEventDispatchForm.
*/
class CustomEventDispatchForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'custom_event_dispatch_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['name'] = [
'#type' => 'textfield',
'#title' => $this->t('Reference'),
'#description' => $this->t('Type any name here that will set event object, while subscribing it.'),
'#maxlength' => 64,
'#size' => 64,
];
$form['dispatch'] = [
'#type' => 'submit',
'#value' => $this->t('Dispatch'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// How to dispatch an event in drupal 9
$dispatcher = \Drupal::service('event_dispatcher');
$event = new CustomEvent($form_state->getValue('name'));
$dispatcher->dispatch(CustomEvent::CUSTOM_FORM_SUBMIT, $event);
}
}
Step 5:- Create a drupal_custom_events.services.yml file for services where we will create a service for dispatch custom event.
services:
# Name of this service.
# Subscriber to the event we dispatch in custom event dispatch form submit, with dependencies injected.
custom_event_subscriber:
class: '\Drupal\drupal_custom_events\EventSubscriber\CustomEventSubscriber'
arguments:
- '@messenger'
tags:
- { name: 'event_subscriber' }
Step 6:- Create a folder "EventSubscriber" inside src folder then give filename "CustomEventSubscriber.php" for subcribing events when custom form will submit
<?php
namespace Drupal\drupal_custom_events\EventSubscriber;
use Drupal\drupal_custom_events\Events\CustomEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Messenger\MessengerInterface;
/**
* Class CustomEventSubscriber.
*
* @package Drupal\drupal_custom_events\EventSubscriber
*/
class CustomEventSubscriber implements EventSubscriberInterface {
/**
* Messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* CustomEventSubscriber constructor.
*
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* Messenger.
*/
public function __construct(MessengerInterface $messenger) {
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
CustomEvent::CUSTOM_FORM_SUBMIT => 'onEventDispatchFormSubmit',
];
}
/**
* This will react on custom event dispatched.
*/
public function onEventDispatchFormSubmit(CustomEvent $event) {
$this->messenger->addStatus(t('The custom event has been subscribed, which has been dispatch on CustomEventDispatchForm submit with '.$event->getReferenceId()." as Reference"));
}
}
asdf
ReplyDelete