Change record status: 
Introduced in branch: 

As of Drupal 8, hook_init() no longer exists, since, after introducing the Symfony kernel and events it was not serving any specific need.

  • If your module needs to perform changes on the request/response object very early to the request an event subscriber should be used listening to the kernel.request event.


    function mymodule_init() {
      if (!empty($_GET['redirect-me'])) {

    Register your Event Subscriber in your and tag it as such:

        class: Drupal\mymodule\EventSubscriber\MymoduleSubscriber
          - {name: event_subscriber}

    Your MymoduleSubscriber should be placed in folder structure of modules/src/EventSubscriber/MymoduleSubscriber.php and should implement the EventSubscriberInterface interface and in its getSubscribedEvents() point to the method that should be executed:

    namespace Drupal\mymodule\EventSubscriber;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    use Symfony\Component\HttpKernel\KernelEvents;
    use Symfony\Component\HttpKernel\Event\GetResponseEvent;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    class MymoduleSubscriber implements EventSubscriberInterface {
      public function checkForRedirection(GetResponseEvent $event) {
        if ($event->getRequest()->query->get('redirect-me')) {
          $event->setResponse(new RedirectResponse(''));
       * {@inheritdoc}
      public static function getSubscribedEvents() {
        $events[KernelEvents::REQUEST][] = array('checkForRedirection');
        return $events;
  • If your module just needs to add css and/or javascript, hook_page_attachments should be used instead
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other updates done


EvanDonovan’s picture

Yay, now Drupal developers will need to use an IDE to generate boilerplate code just like Java programmers :/

Nonprofit / EdTech Mentor

acb’s picture

I agree. seems awfully involved for what used to be 1 line of code.

Sam152’s picture

For production code, this will help keep things organised. For debugging, I now use a sandbox:

dsteplight’s picture

Understanding the importance of event subscribers and when to tap into the HTTP Kernel is such an important part of Symfony 2. Using this in an OO manner is also just so much cleaner. Don't be discouraged because you are using more than 1 line of code, but happy that you are not using procedural code and the right portions of code are encapsulated better instead of all of it being mixed into one .module file.

zkent’s picture

Using Drupal Console:
$ drupal generate:event:subscriber [options]


tbisciglia’s picture

This one tripped me up for a while. The service name needs to be:


Notice there's a dot, not an underscore, between the module name and event_subscriber. Otherwise, the code will not be connected. Otherwise, spot on and very helpful!