Event Subscribers in Drupal 8 are similar to hooks in Drupal 7. They help you hook into an event happening in a Drupal site and execute the appropriate code. So if you are a Drupal developer and would like to execute code when an event occurs it will be worthwhile to know about Event Subscribers.
On a particular Drupal project that we were working on, we needed to auto-approve users who had a particular email domain after they signed up using Google Auth Social. We checked the module’s configuration to see if it provided any options to do it. This was available in the D7 version of the module but missing in D8.
On the other hand, reviewing the Drupal 8 google auth social module codebase, we found that it had supplied events that we could hook into. So, the solution to the problem was to write an Event Subscriber.
Use cases for writing Event Subscribers
Event Subscribers are a good tool supplied by Drupal if you face scenarios similar to the following:
- Users with the editor role have to receive a notification mail to start reviewing when a user with the writer role has created a node and marked it for reviewing
- A module creates a user but does not provide configuration to act on the changes
- If a comment has been published and we need to send an email to the commenter informing that their comment has been published
- If a developer wishes to allow other modules to react to changes introduced by their custom module, Events are an ideal choice
- Events are used to alter routes
What are Events?
To understand Event Subscribers, we need to know about Events. Events are related to the plain English “Incidents” but in Drupal speak they mean the following:
- Events are part of the Symfony framework
- They allow different components of the system to interact & communicate with each other
- Have a unique string name
- One component dispatches or triggers the event
- Another component listens or subscribes to the event
What are the advantages of Events?
All event-based systems come with a certain set of advantages and Drupal Events inherit most of them. Events are usually compared with hooks which are also another type of Event-based system. Here are some of the advantages of Events when compared to hooks.
- In the Events based system the Event listener can take on any name. Hooks, on the other hand, follow a pattern
- Events can be called multiple times but hooks are called only once
- Events help you to work with the API provided by Symfony where hooks are restricted to only Drupal
- Events are based on Object-Oriented concepts and hence can be easily overridden and extended.
- They help different components communicate with each other
- They prevent the code of one component from being tightly coupled with another component
- Events are designed to work in conjunction with the hook system
How to subscribe to an Event in your custom code?
The following are the generic steps:
- Go to the module that triggers the event you want to subscribe to.
- You should see a class that defines the Events as constants. The class is probably final and cannot be extended and is usually found within the Events folder.
- Locate the Event Constant related to the event you need. Working example to come in Part 2 of this article.
- Next, create a class that implements EventSubscriberInterface and write a method call getSubscribedEvents in a custom module.
- In this method return to an array mapping the event constant and the method you want to call when the event occurs.
- Here, you also write the actual code that does the action when the event occurs.
- Finally, you set this class as a service and tag it as a type event_subscriber.
As mentioned in the last point, the Event Subscriber needs to be set as Service and tagged as an event Subscriber for it to work. We will provide a working example in Part 2 of this blog series.
Hooks vs Events
Events are not that different from hooks. A hook can be thought of as a code defining an event. All implementations/callbacks of hooks can be thought of as an event subscriber hooking into that Event. Events are basically the object-oriented version of Hooks. Still, there are some basic differences.
As a Drupal 8 developer, you would need to be familiar with Events & Event Subscribers. We will be doing a follow-up article on the actual implementation of the Event Subscriber with a real-world scenario soon.
Edit: Read about the practical implementation of this tutorial in Part 2 here!