Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

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.

    D7:

    function mymodule_init() {
      if (!empty($_GET['redirect-me'])) {
        drupal_goto('http://example.com/');
      }
    }
    

    D8:
    Register your Event Subscriber in your mymodule.services.yml and tag it as such:

    services:
      mymodule_event_subscriber:
        class: Drupal\mymodule\EventSubscriber\MymoduleSubscriber
        tags:
          - {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('http://example.com/'));
        }
      }
    
      /**
       * {@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
Impacts: 
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: 
Other updates done

Comments

EvanDonovan’s picture

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

Nonprofit / EdTech Mentor
http://drupal.org/user/168664

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: https://www.drupal.org/sandbox/sam/2486229

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]

See https://docs.drupalconsole.com/en/commands/generate-event-subscriber.html