Creating hook_init In Drupal 8

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.

name: Hook Init
type: module
description: 'Testing stuff.'
core: 8.x

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

services:
  hook_init.event_subscriber:
    class: Drupal\hook_init\EventSubscriber\HookInitEventSubscriber
    tags:
      - { name: event_subscriber }

The HookInitEventSubscriber 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.

<?php

namespace Drupal\hook_init\EventSubscriber;

use \Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

class HookInitEventSubscriber implements EventSubscriberInterface {

  public static function getSubscribedEvents() {

    $events[KernelEvents::CONTROLLER][] = array('onLoad');

    return $events;
  }

  public function onLoad(FilterControllerEvent $event) {
    drupal_set_message('Event triggered.');
  }

}

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.
CAPTCHA
9 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.