Example: Events

Same name in other branches
  1. 8.x-1.x events_example/events_example.module \events_example
  2. 4.0.x modules/events_example/events_example.module \events_example

Demonstrates subscribing to, and dispatching, events.

Events allow for different components of the system to interact and communicate with each other. Modules can either dispatch events, subscribe to events, or both.

Subscribing to an event allows a module to declare that it would like to be notified anytime a specific event happens. A module can subscribe to any number of events, and can even subscribe to the same event more than once.

Dispatching an event allows a module, or Drupal core subsystem, to notify any registered subscribers that a specific event has just taken place. When an event is dispatched the registered code for each subscriber is executed. For example, whenever a configuration entity is updated the Configuration API dispatches a new event allowing all subscribers to react to the change.

This allows modules to extend other systems without the need to modify the original code.

Each event has a unique string name. This string is often referred to as "the event", or "the event name". This string is how you identify which event(s) you are interested in. A complete list of events dispatched by core is available at https://api.drupal.org/api/drupal/core%21core.api.php/group/events/

Drupal's event system is an extension of the Symfony EventDispatcher component, and implements the Mediator pattern.

Subscribing to an event requires:

  • Defining a service in your module, tagged with 'event_subscriber'.
  • Defining a class for your subscriber service that implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
  • Using the getSubscribedEvents method to return a list of the events you want to subscribe to, and which methods on the class should be called for each one.

For an example of subscribing to an event see the events_example.services.yml file. And the \Drupal\events_example\EventSubscriber\EventsExampleSubscriber class.

Dispatching an event requires:

This example code is based off of the article Responding to Events in Drupal 8.

See also

Events

\Symfony\Component\EventDispatcher\EventDispatcherInterface

\Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher

Service Tags

Parent topics

File

modules/events_example/events_example.module, line 8

Classes

Title Sort descending File name Summary
EventsExampleForm modules/events_example/src/Form/EventsExampleForm.php Implements the SimpleForm form controller.
EventsExampleServiceTest modules/events_example/tests/src/Kernel/EventsExampleServiceTest.php Test to ensure 'events_example_subscriber' service is reachable.
EventsExampleSubscriber modules/events_example/src/EventSubscriber/EventsExampleSubscriber.php Subscribe to IncidentEvents::NEW_REPORT events and react to new reports.
EventsExampleTest modules/events_example/tests/src/Functional/EventsExampleTest.php Test the functionality of the Events Example module.
IncidentEvents modules/events_example/src/Event/IncidentEvents.php Defines events for the events_example module.
IncidentReportEvent modules/events_example/src/Event/IncidentReportEvent.php Wraps a incident report event for event subscribers.