Creating hook_init In Drupal 8

23rd September 2018 - 4 minutes read time

When developing sites in Drupal 7 I found the hook_init() hook was a good way of quickly testing certain things in code. By adding little blocks of code it was possible to build complex SQL queries, inspect internal configuration, or even test complex node interactions.

When I started to develop sites in Drupal 8 I found that I needed a similar mechanism to do similar things. However, the hook_init() hook doesn't exist in Drupal 8 any more so I needed to look at another way of doing this.

In order to do this in Drupal 8 we need to utilise the event subscribers. There are a few different types of event subscribers available, but in order to use them we need to create a little module.

Here is the info.yml file of a module called hook_init.

  1. name: Hook Init
  2. type: module
  3. description: 'Testing stuff.'
  4. core: 8.x

Within this module we create a file that we use to register a class called HookInitEventSubsciber with the event subscriber. We don't detail what kind of event here, we just register the class that will handle that detail.

  1. services:
  2.   hook_init.event_subscriber:
  3.   class: Drupal\hook_init\EventSubscriber\HookInitEventSubscriber
  4.   tags:
  5.   - { name: event_subscriber }

The HookInitEventSubsciber class implements the EventSubscriberInterface interface and as such must implement a static function called getSubscribedEvents(). This static function returns an array of the events that we want to trigger and what functions in this class we should trigger.

In the class I am using the KernelEvents::CONTROLLER event, which is triggered when a controller is found for handling the request. The triggered event method (onLoad) is passed an FilterControllerEvent object which can be used to alter the controller being called or the arguments passed to it. This is quite late in the loading process so Drupal is bootstrapped and there is a lot of information available about the request. In this class we are just using this method to intercept the page request, so we aren't doing anything with the FilterControllerEvent object here.

  1. <?php
  3. namespace Drupal\hook_init\EventSubscriber;
  5. use \Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  9. class HookInitEventSubscriber implements EventSubscriberInterface {
  11. public static function getSubscribedEvents() {
  13. $events[KernelEvents::CONTROLLER][] = array('onLoad');
  15. return $events;
  16. }
  18. public function onLoad(FilterControllerEvent $event) {
  19. drupal_set_message('Event triggered.');
  20. }
  22. }

When this module is activated the message "Event triggered" will be shown on the screen on every page load. I should note that this is very much a testing and development module and shouldn't be used in production. That said, I have used this sort of system to create events that allow me to record or interact with different events being triggered by Drupal. The event subscriber system can be very powerful and this simple example shows how you can easily hook into the core events in Drupal to inspect or change the outcome of the request.

There are a few other KernelEvents events available to be used, as detailed in the documentation of the class.

Add new comment

The content of this field is kept private and will not be shown publicly.