<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=278116885016877&amp;ev=PageView&amp;noscript=1">

, , ,

Jul 15, 2020 | 3 Minute Read

How To Create Event Subscribers In Drupal 8 - Part 1

Table of Contents


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:

  1. Events are part of the Symfony framework
  2. They allow different components of the system to interact & communicate with each other
  3. Have a unique string name
  4. One component dispatches or triggers the event
  5. 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. 

  1. In the Events based system the Event listener can take on any name. Hooks, on the other hand, follow a pattern 
  2. Events can be called multiple times but hooks are called only once
  3. Events help you to work with the API provided by Symfony where hooks are restricted to only Drupal
  4. Events are based on Object-Oriented concepts and hence can be easily overridden and extended.
  5. They help different components communicate with each other
  6. They prevent the code of one component from being tightly coupled with another component
  7. 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:

  1. Go to the module that triggers the event you want to subscribe to.
  2. 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.
  3. Locate the Event Constant related to the event you need. Working example to come in Part 2 of this article. 
  4. Next, create a class that implements EventSubscriberInterface and write a method call getSubscribedEvents in a custom module. 
  5. In this method return to an array mapping the event constant and the method you want to call when the event occurs.
  6. Here, you also write the actual code that does the action when the event occurs.
  7. 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. 

comparison table - Hooks vs Events


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!

About the Author
Binny Thomas, PHP/Drupal Engineer - L2
About the Author

Binny Thomas, PHP/Drupal Engineer - L2

Addicted to Quora, he is a geek but also gentle and nostalgic. His idea of an enjoyable holiday is a quiet day relaxing at home. And if you find him pulling his hair, he is probably deep-diving in his thoughts!

Back to Top