[Drupal] Create an event subscriber

Instructions

I will be creating a dedicated module using Drupal Console for this event subscriber. But feel free to use your own if you already have one! (don’t forget to backup)

  1. Access the drupal root folder in command line.
  2. Generate a new module:
    dakwamine@debian-drupal:/var/www/html/drupal$ drupal generate:module
    
     Enter the new module name:
     > Test Module          
    
     Enter the module machine name [test_module]:
     > 
    
     Enter the module Path [/modules/custom]:
     > 
    
     Enter module description [My Awesome Module]:
     > 
    
     Enter package name [Custom]:
     > 
    
     Enter Drupal Core version [8.x]:
     > 
    
     Do you want to generate a .module file (yes/no) [yes]:
     > no
    
     Define module as feature (yes/no) [no]:
     > 
    
     Do you want to add a composer.json file to your module (yes/no) [yes]:
     > no
    
     Would you like to add module dependencies (yes/no) [no]:
     > 
    
    
     Do you confirm generation? (yes/no) [yes]:
     > 
    
    Generated or updated files
     Site path: /var/www/html/drupal
     1 - modules/custom/test_module/test_module.info.yml
    
  3. Generate the event subscriber. We will subscribe to the kernel.request event:
    dakwamine@debian-drupal:/var/www/html/drupal$ drupal generate:event:subscriber
     Enter the module name [admin_toolbar]:
     > test_module
    
     Enter the service name [test_module.default]:
     > test_module.event_subscriber_example
    
     Class name [DefaultSubscriber]:
     > TestModuleExampleSubscriber
    
     
    Type the event name or use keyup or keydown.
    This is optional, press enter to continue
    
     Enter event name [ ]:
     > kernel.request
    
     Callback function name to handle event [kernel_request]:
     > 
    
     Enter event name [ ]:
     > 
     Do you want to load services from the container (yes/no) [no]:
     > 
    
    
     Do you confirm generation? (yes/no) [yes]:
     > 
    
    Generated or updated files
     Site path: /var/www/html/drupal
     1 - modules/custom/test_module/src/EventSubscriber/TestModuleExampleSubscriber.php
     2 - modules/custom/test_module/test_module.services.yml
     cache:rebuild
    
     Rebuilding cache(s), wait a moment please.
    
    
     [OK] Done clearing cache(s).
    
    
    
  4. Current state:
    • .
      ├── src
      │   └── EventSubscriber
      │       └── TestModuleExampleSubscriber.php
      ├── test_module.info.yml
      └── test_module.services.yml
      
    • name: Test Module
      type: module
      description: My Awesome Module
      core: 8.x
      package: Custom
      
      
    • services:
        test_module.event_subscriber_example:
          class: Drupal\test_module\EventSubscriber\TestModuleExampleSubscriber
          arguments: []
          tags:
            - { name: event_subscriber }
      
      
    • 
      
  5. Install the module using either drupal module:install or drush pm-enable:
    dakwamine@debian-drupal:/var/www/html/drupal$ drupal module:install test_module
     Installing module(s) test_module
    
    
     [OK] The following module(s) were installed successfully: test_module
    
    
     cache:rebuild
    
     Rebuilding cache(s), wait a moment please.
    
    
     [OK] Done clearing cache(s).
    
    
  6. Now, refresh your web page and on each request received server side, a message will tell you the event occurred!

Additional Information

  • Instructions made on Drupal 8.2.x.
  • Using Drupal Console, you can easily inject services such as the current_user one by answering yes when it asks if you want to load services from the container. Please check this article about service dependency injection: [Drupal] Service dependency injection in a service type class.
  • After editing your files, remember to run a cache rebuild either with drupal cache:rebuild or drush cache-rebuild.
  • Events documentation entry point: Events on Drupal 8. You will find a few core events here.
  • The callback function’s name can be changed to whatever you want.

Published by Dakwamine

Dakwamine, alias Quang-Minh Dang, né en 1987 en région parisienne. Un type sympa, pas bavard et pragmatique.