1

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 listen_event.info.yml

type: module

name: Listen Event

description: Custom event

core_version_requirement: ^8 || ^9

version: '9.x'

2


 

   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

 

services:

   event.subscriber:

     class: Drupal\listen_event\EventSubscriber\CustomEvent

     tags:

       - {name: event_subscriber}

 

3


 

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

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

5

<?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');

      fwrite($file,$current_path);

      fclose($file);

    }

  }

}

6

 

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 

[root_directory]/sites/default/files/customevent.txt

 

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 .