Events API in Drupal

Events in Drupal are a mechanism for triggering and responding to specific actions within the system. Developers need to know how to use events to implement custom behaviors and integrations in a decoupled and flexible manner.

This course covers the fundamental concepts of events, including how to discover existing events, subscribe to them, and dispatch new events. By the end of the course, you will be able to effectively use events to extend and customize Drupal's functionality via custom modules.

Key topics

  • What events are, their purpose, and how they work
  • Discovering existing events to subscribe to
  • Subscribing to events by defining a tagged service
  • Defining new events and dispatching them from a custom module
  • Differences between hooks and events
  • Best practices for organizing and documenting your event implementations
Tutorials in this course
Categories
Drupal 8, 9, 10, and 11
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, 10, and 11
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, 10, and 11
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, 10, and 11
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.

This course appears in the following guides:
Module Development
Learn how to alter Drupal with hooks, plugins, services, and events APIs.

Alter Drupal in Modules