Module Development
Topic

Events for Drupal 8, 9, and 10

Events in Drupal 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 for a while. Expect contributed modules to start dispatching events in place of invoking hooks for some types of interactions.

Drupalize.Me resources

Posted on Wednesday, January 24, 2024 - 19:54 by Blake Hall

In Part 2 of our exploration of Symfony components in Drupal, we focus on the event dispatcher.

The event dispatcher is a tool that enables the application to communicate across objects by subscribing to and listening for events. It achieves this by creating a directory for various event types, and the corresponding registered listeners for each event type. When a specific type of event occurs, the code that has registered a listener for that event is invoked. If you're familiar with the Mediator and Observer design patterns you might recognize similarities here.

Categories
Drupal 8, 9, and 10
More information

Drupal uses events to allow modules and various subsystems to communicate with one another in an object-oriented manner. Understanding how the Event API works is critical knowledge for all module developers. In this tutorial we'll:

  • Explain what events are
  • Discuss the use case for subscribing to events
  • Give an example use case for dispatching events

By the end of this tutorial you should be able to explain what events are, understand when to use them in your own code, and know where to learn more about how to do so.

Categories
Drupal 8, 9, and 10
More information

Modules can declare their interest in being notified when a specific event, or events, are triggered by implementing an event subscriber. This can be used to react to events in order to add your own logic, or to alter Drupal's existing functionality.

In this tutorial we'll cover how to subscribe to an event from your own module. After completing this tutorial you should be able to subscribe to any event and ensure that your custom code is executed when the specific event is triggered.

Categories
Drupal 8, 9, and 10
More information

Some events are dispatched by Drupal core, some by underlying Symfony components, and some by contributed modules. The list of events that you can subscribe to depends on the modules you've got installed. This can make it tricky to get a complete list.

In this tutorial, we'll look at different ways you can get a complete list of the available events for your Drupal application, and where to find documentation for those events.

Categories
Drupal 8, 9, and 10
More information

Modules or subsystems can dispatch events in order to allow another developer to subscribe to those events and react to what's happening within the application. As a module developer you'll learn how to:

  • Define and document new events
  • Define new event objects
  • Use the event dispatcher service to alert event subscribers when specific conditions are met in your own code.

By the end of this tutorial, you should be able to explain the use case for dispatching events, and be able to trigger one more events from within your own code.

Posted on Tuesday, February 24, 2015 - 08:05 by joe

Events in Drupal allow various system components to interact and communicate with one another while remaining independent, or decoupled. The event system is built on the Symfony event dispatcher component, and is an implementation of the Mediator design pattern. This post takes an in-depth look at how module developers can subscribe to events in Drupal.

Services

Topic
Categories
Drupal 8, 9, and 10
More information

Services are objects that encapsulate the code for performing specific tasks in a reusable and decoupled way.

Symfony

Topic
Categories
Drupal 8, 9, and 10
More information

Symfony is a set of reusable PHP components, and a framework for building PHP applications. Drupal makes use of various Symfony components.

Guides

Not sure where to start? Our guides provide useful learning tracks for all skill levels.

Navigate 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.