How can you contact us

2By phone at +40 749 014 360

If you still have problems, please let us know, by sending an email to info@westweb-solutions.com Thank you!

OFFICE HOURS

Mon-Fri 9:00AM - 6:00AM
Sat - 9:00AM - 1:00PM
Sundays by appointment only!

Drupal 8 - How to dispatch an event

/ / Drupal 8

In this 'How to' I will show you how to dispatch an event from a module called 'othermodule'.

First, we need a class that is extending the \Symfony\Component\EventDispatcher\Event class.
This calss will contains all the information we want to send when we dispatch the event.

Place this class in \othermodule\src\Event\OtherModuleEvent.php


namespace Drupal\othermodule\Event;

use Symfony\Component\EventDispatcher\Event;

class OtherModuleEvent extends Event {
  protected $message;

  /**
   * Constructor for the OtherModuleEvent class.
   *
   * @param string $message
   */
  public function __construct($message) {
    $this->message = $message;
  }

  /**
   * @return string
   */
  public function getMessage() {
    return $this->message;
  }

  /**
   * @param string $message
   */
  public function setMessage($message) {
    $this->message = $message;
  }
}


This class is using only one protected property callled $message.

Even if this is not really needed, we can have another class that will define all the events that can be dispatched as constants strings.

Create a new class OtherModuleEvents and place it under \othermodule\src\Event\OtherModuleEvents.php


namespace Drupal\othermodule\Event;

class OtherModuleEvents {

  /**
   * Name of the event that will be dispatched.
   */
  const EVENT_NAME = 'othermodule.event_name';

  //
  // Other events that we want to disptach.
  //

}


In this calss, I've defined a new const EVENT_NAME that has the value 'othermodule.event_name'.
We will use this EVENT_NAME when the event will be dispatched.

In a custom controller, in order to dispatch the EVENT_NAME we need to:

  • Create a new object of OtherModuleEvent class (that will store the event details)
  • dispatch the event using the 'event_dispatcher' service

/**
 * Class Example2Controller.
 *
 * @package Drupal\othermodule\Controller
 */
class Example2Controller extends ControllerBase {

  /**
   * The event_dispatcher object.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $event_dispatcher;

  /**
   * {@inheritdoc}
   */
  public function __construct(EventDispatcherInterface $event_dispatcher) {
    $this->event_dispatcher = $event_dispatcher;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('event_dispatcher')
    );
  }

  /**
   * Example dispatch an event.
   */
  public function example() {

    // Create a new OtherModuleEvent object that will be used in the dispatch.
    $otherModuleEvent = new OtherModuleEvent('This is a message from OtherModuleEvent');

    // Dispatch the event and notify all subscribers.
    $this->event_dispatcher->dispatch(OtherModuleEvents::EVENT_NAME, $otherModuleEvent);

    return [
      '#type' => 'markup',
      '#markup' => $this->t('Example - Dispatch an Event')
    ];
  }
}

As you can see in the above example, I'm using the dependency injection to inject the 'event_dispatcher' service into our controller class,
using the __construct() constructor and public static function create().

In the next article I will show you how to subscribe to this Event in a custom module 'mymodule'.

Sign in your account to have access to different features

Forgot your details?

TOP