How to Create Event Subscribers In Drupal 8 - Part 2

May 7, 2021 | 3 Minute Read

Tags:Module Development,Drupal

Summarising what we planned to do in Part One of this series: We needed to auto-approve users whose email domain matched a particle address after signing up to our site using Google Auth Social. The Drupal 7 version of the module did have an option, but it was taken down in the Drupal 8 version. So, the obvious answer was to use Event Subscribers in Drupal 8. 

Identifying the event

First, we find the event that we need to subscribe to; it is usually in the module. In our case, it is the Google Social Auth module. This has to be 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.

Next, we proceed to the module social_auth and then move to the Events Folder, and in the class socialAuthEvents, we can soon find the event that we needed to subscribe to. 

const USER_FIELDS = 'social_auth.user.fields' 

So with that part done, the next step in the process was to write a custom module with an Event listener to listen to that event. Next, we proceeded to create a module named ct_user and created a class called ContribTrackerEventListener. The code of the class is shown below: 

In this class, we need to implement the method getSubscribedEvents to link the event subscriber with the technique that takes action when the event occurs. This is a crucial part of the process. 

Once that is done, we can write out the custom code that defines the action in the method we wrote down in the getSubscribedEvents method.

Finally, we need to set this class as a service of type EventSubscriber as all event subscribers are services. This is achieved by placing the class naming in the services.yml file of the module and tagging it as an event subscriber, as shown in the code below: 

When a user tries to sign up using Google Social login for the first time, the user is first sent to Google for authentication. Assuming the user authenticated and also authorized Google to send data to our website, the user is redirected back to the site with a callback. Google Social Auth module responds to this callback, creates a Drupal user (if required), and fires the events we mentioned above (SocialAuthEvents::USER_FIELDS)

As our event subscriber is on the lookout for such an event, it will immediately send an alert for its occurrence.

A good analogy would be like a telephone operator calling their customer of the event they have been looking out for had occurred. The events framework will then pass the control over to the method we had linked to the event, which would be onSocialAuthUserFieldEvent.

Since we are extending the EventSubscriberInterface, this method will also get the event variable containing all the data generated by the triggering event. In our example, we get the user's email (which we use to verify the domain name) and then update the user's status and log the outcome.

Conclusion

In continuation to part one of this series, this article showed an actual working example with the basic steps involved. Hope it helped you further!