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)
- Access the drupal root folder in command line.
- 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
- 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).
- 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 }
-
-
- Install the module using either
drupal module:install
ordrush 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).
- 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
ordrush 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.