How to define an Event, Dispatcher and Subscriber in Drupal 8?
Blog

How to define an Event, Dispatcher and Subscriber in Drupal 8?

As we all know Symfony Event Components are  included in Drupal8 core. In future versions of Drupal Core, Events are going to play a major role for sure. Let’s see how the Event component is going to help our Drupal Development. 

In one of the recent project, we got a tricky requirement in content authoring and publishing workflow. In specific, the Editor has to get a notification for reviewing, once a content author writes an article and saves it for reviewing. By using Events we can easily achieve this kind of  requirement. While ‘Content Author’ is saving content, we need to dispatch an Event, that has to be subscribed and a notification has to be sent to the respective ‘Editor’.

So here we are going to discuss about, how to define, dispatch and subscribe an event from our custom module. The following steps are involved: 

  1. How to define your own event in Drupal 8?
  2. How to dispatch the event in Drupal 8?
  3. How to subscribe an event in Drupal 8?

​First we are going to create .info.yml for our module called ‘example_events’ , which is example_events.info.yml

 

How to define your own Event in Drupal 8?

We have to define our event class under the ‘src/’ folder in your module root directory. Here we are defining our event class name as “ExampleEvent” and the file name as “src/ExampleEvent.php

While defining our event ExampleEvent, the class should be extended from

use Symfony\Component\EventDispatcher\Event

In the event class, you can define your own methods which can be accessed whenever dispatching the event or in subscriber call back. Here in our event class ExampleEvent, we use constructor to set the $referenceID, which can be accessed in Event Subscriber callback doSomeAction. Whenever the event ExampleEvent::SUBMIT is getting dispatched the $referenceID gets constructed.

 

How to dispatch the event in Drupal 8?

Event can be dispatched whenever you want like mentioned above.

In our “example_events” module, we have created a form[src/Form/DemoEventDispatchForm.php] and dispatched the event on Submit of the Form.

In the event dispatcher, first we need to load the Symfony event dispatcher object through services $dispatcher = \Drupal::service('event_dispatcher'); Then we should create our event class object. $event = new ExampleEvent($form_state->getValue('name')); Finally, we are going to dispatch the event through the dispatch method of the Symfony event component object $dispatcher by passing event name ExampleEvent::SUBMIT as first parameter and event object $event as the second parameter.

    $dispatcher = \Drupal::service('event_dispatcher');
    $event = new ExampleEvent($form_state->getValue('name'));
    $dispatcher->dispatch(ExampleEvent::SUBMIT, $event);
 

How to subscribe the events in Drupal 8?

There are a couple of steps involved in subscribing event in Drupal 8 as follows.

  1. Define your event Subscriber class that implements Symfony ‘EventSubscriberInterface’.
  2. Tag your Event Subscriber Class with ‘event_subscriber
 

1. Define your event Subscriber class that implements Symfony EventSubscriberInterface.

We should defining the Event Subscriber class under ‘ModuleRoot/src/EventSubscriber/’ directory, In our example, path will be,

example_events/src/EventSubscriber/ExampleEventSubScriber.php

In the EventSubscriber class ExampleEventSubScriber is implementing the interface EventSubscriberInterface from

 use Symfony\Component\EventDispatcher\EventSubscriberInterface;

In that we have to define the method public static function getSubscribedEvents() . This method will be returning a multi dimensional array containing the Event name ExampleEvent::SUBMIT and the Subscriber Callback doSomeAction with priority 800. In the Subscriber call back you can define your action which has to be taken whenever the event is getting dispatched. The event object will be passed to the Subscriber call back. Here our event object contains the referenceID.


In our ExampleEventSubScriber, we are subscribing one of the default event dispatched by Drupal Core which is ConfigEvents::SAVE defined by

use Drupal\Core\Config\ConfigEvents;

It will be dispatched , whenever you have any configuration change in Drupal. Please check the following video for the Demo.

 


 

2. Tag your Event Subscriber Class with event_subscriber

In your module.services.yml tag your event subscriber class as ‘event_subscriber’. Just like our event class. Tags indicate these service should be registered or for specific purpose, or that it belongs to a category. For more details on tags refer: Symfony documentation on service tags.

The source of our example is: https://github.com/rakeshjames/example_events


Conclusion:

Drupal 8 Events

Symfony Event Components are introduced in Drupal 8 Core. Still there is a long way to go. Entity APIs are still using hooks, which are likely to be available in Events on Drupal 9 version onwards. Anyway it’s good to learn and start using it because, its gives more ability to developer to develop things easier, especially in the era of real time updation.