Create Drupal Event Listener

Most of the time we have no idea on the current data being processed when we create request from server . It is very important that we know the life cycle of Drupal . The very first thing that you think when you create Event Listener function is to understand how the flow of data travels form start to end . 



Basically the Diagram shows us that the Drupal has Http Kernel to handle the request from the user and pass the data to the route handler to process whatever logic behind the controller then finally provide specified data to the client. In this setup , we already know the basic flow of the whole structure of Drupal and we can insert which part of event listener are gonna implement.


In this example ,im gonna show you how to create simple event listener that it will listens all the requests and create some simple logic behind the event.


We are gonna use EventSubscriber Service Library from Drupal . This Service gonna provide custom event listener and we create custom module named ListenEvent


  1. Create folder ListenEvent , under the folder create

type: module

name: Listen Event

description: Custom event

core_version_requirement: ^8 || ^9

version: '9.x'



   2. Create file listen_event.service.yml

This file will register your event subscriber service during clear cache

The event.subscriber refers to an service id 

The class refers to namespace of you Custom event file ( w/o .php extension )

The tags must be printed on this file so that it would identify that this service id is event_subscriber




     class: Drupal\listen_event\EventSubscriber\CustomEvent


       - {name: event_subscriber}




3. Create folder src and then create another folder EventSubscriber . 

4 Create file CustomEvent.php ( \ListenEvent\src\EventSubscriber\CustomEvent.php )



namespace Drupal\listen_event\EventSubscriber;

use Symfony\Component\HttpKernel\KernelEvents;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;


 * Class .


 * @package Drupal\


class CustomEvent implements EventSubscriberInterface {



   * {@inheritdoc}


   * Get Subscribed Events

   * @return array()


  static function getSubscribedEvents() {

    $events[KernelEvents::REQUEST][]  = [logFile,27];

    return $events;


  public function logFile(GetResponseEvent $event){

   $routeName    = \Drupal::routeMatch()->getRouteName();

    $current_path = \Drupal::request()->getRequestUri();

    if($routeName == 'entity.node.canonical'){

     $filename = 'public://customevent.txt';

      $file   = fopen($filename,'a');








Before we proceed to step 5 , we have to understand what going on inside the CustomEvent.php

First we have to implement EventSubscriberInterface to invoke the getSubscriberEvents method and register your function . KernelEvents::REQUEST means you have to insert the function during every requests from client which is logFile . 27 means you can priorities your function so its on 27th order .


The function logFile is all about creating file that contains all the requests url visited from each node page ,and this will specify the entity.node.canonical . The file will be saved on the directory 



5. Run clear cache then visit some node pages like /node/1 etc.

6. Check the file and there will be the list of urls being created from the event subscriber

We conclude that implementing custom event is just a piece of cake and we don’t have to worry about complicated stuff . This will be possible to make different variations of logic design depending on what do you want to achieve but basically the core foundation is on the example above . Once you already have the simple event listener that functions well , you could make it more complicated and integrate with another Drupal services .


Profile picture for user Renier John Sediego
Renier John Sediego Software Developer

Hello everyone and welcome to my Profile . My name is Renier and my life was started when I was born and raise not in Cebu but in Negros Island with simple life and living. I like solving problems where my knowledge capacity is hitting hard. I always do sports activities like bicycling , jogging and other physical activities to boost not only my immune system but also my mental state . I also love to hear party musics specially when i'm do coding stuff because I'm feel more focused and alive.