Event dispatcher. Create event, Dispatch and Subscribe. | Drupal 8

Event dispatcher. Create event, Dispatch and Subscribe.

Submitted by editor on Fri, 01/19/2018 - 13:15

How to use Event dispatcher system of drupal 8 ?

Drupal's event system allow to create triggers when an event has been happened like create a custom hook on drupal 7.

Create an event.

The first step is, create an event on your custom module (example_events). This class must extend Symfony\Component\EventDispatcher\Event.
Example :
File : example_events/src/ExampleEvent.php

namespace Drupal\example_events;
use Symfony\Component\EventDispatcher\Event;
class ExampleEvent extends Event {
  const RUN = 'event.run';
  protected $data;
  public function __construct($data) {
    $this->data = $data;
  public function getData() {
    return $this->data;
  public function display() {
    drupal_set_message("This is as an example event : ".$this->data);

Dispatch the event.

You can dispatch the event anywate on your custom module.
Example :

// Use the namespace of the ExampleEvent class
use Drupal\example_events\ExampleEvent;
// Load dispatcher object through services.
$dispatcher = \Drupal::service('event_dispatcher');
// creating our event class object.
$event = new ExampleEvent("My Event");
// Dispatching the event through the ‘dispatch’  method,
// Passing event name and event object ‘$event’ as parameters.
$dispatcher->dispatch(ExampleEvent::RUN, $event);

Now you can Subscribe to this event in the same module or from another module.

Create event Subscriber : Subscribe to the event

Like any other event of drupal, now you can use ‘event_subscriber’ service (See this post)

File : example_events/src/EventSubscriber/ExampleEventSubScriber.php

 * @file
 * Contains \Drupal\example_events\ExampleEventSubScriber.
namespace Drupal\example_events\EventSubscriber;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Drupal\example_events\ExampleEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 * Class ExampleEventSubScriber.
 * @package Drupal\example_events
class ExampleEventSubScriber implements EventSubscriberInterface {
   * {@inheritdoc}
  public static function getSubscribedEvents() {
    // Priority greater than 200 = No Cache (like hook_boot).
    $events[ExampleEvent::RUN][] = array('doSomeAction', 300);
    return $events;
   * Subscriber Callback for the event.
   * @param ExampleEvent $event
  public function doSomeAction(ExampleEvent $event) {
    // Get data:
    $data = $event->getData();
    drupal_set_message("The Example Event has been subscribed, event->getData() returned : '" . $event->getData() . "'");
    // Run display() action.
    $data = $event->display();

Tag Event Subscriber Class with event_subscriber

Finally add your Event Subscriber Class to .services.yml
File : example_events.services.yml

    class: Drupal\example_events\EventSubscriber\ExampleEventSubScriber
      - { name: 'event_subscriber' }


Example from : http://valuebound.com/resources/blog/how-to-define-an-event-dispatcher-and-subscriber-drupal-8

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.