Topic

Events

Last updated August 22, 2017

Events in Drupal 8 allow various system components to interact and communicate with one another while remaining independent, or decoupled. They are one of the ways that module developers can alter or extend Drupal without modifying existing code. (Other ways include Plugins, Hooks, and Services). The system consists of an event dispatcher which is used to trigger a known event at a specific point during code execution. It also consists of a pattern for subscribing in order to be notified whenever an event is triggered by the dispatcher so that your custom code can react.

The event system is built on the Symfony event dispatcher component and is an implementation of the Mediator design pattern.

Example tasks

  • Subscribe to an existing event in order to react with custom logic whenever the event occurs
  • Dispatch a new event when critical actions happen in your code, giving other components the option to react

Confidence

The event system in Drupal is unlikely to change in Drupal 8. Expect contributed modules to start dispatching events in place of invoking hooks for some types of interactions.

Drupalize.Me resources

  • What Are Events?
    • Learn about what events are and the role they play in the Drupal module developers toolkit
  • Subscribe to an Event
    • Learn how to subscribe to an existing event from within a custom module
  • Discover Existing Events
    • Get a list of all the events available to subscribe to in your specific Drupal application
  • Dispatch an Event
    • Learn about how to dispatch an event from within a custom module, and when you might want to do so

More Guides

We have guides on many Drupal skills and topics.

Explore guides

External resources

  • Events (api.drupal.org)
    • Technical overview of Drupal's event dispatching and subscribing systems. Use this if you're already familiar with the concept of events and you're just looking for a quick reminder of how the pattern works within Drupal.
  • Symfony event dispatcher component (symfony.com)
    • Documentation for the Symfony event dispatcher, upon which Drupal's event dispatcher is built. Knowing how it works will help you better understand Drupal's dispatcher.