Event Subscriber to replace hook_boot and hook_init | Drupal 8

Event Subscriber to replace hook_boot and hook_init

Submitted by editor on Fri, 01/19/2018 - 11:27
Question

The hook boot (hook_boot()) and hook init (hook_init()) are removed from drupal 8. How to use those events and trigger on boot ?
What are the possibilities to replace Hook Boot and Hook Init ?

Use Event Subscriber service.

The EventSubscriber allow to execute an action on boot as hook_boot or hook_init of drupal 7.

Example.

# file should be in /mymodule/mymodule.services.yml
services:
  mymodule.mymodule_subscriber:
    class: Drupal\mymodule\MyModuleSubscriber
    tags:
      - { name: 'event_subscriber' }

// file shoud be in /mymodule/src/MyModuleSubscriber.php
<?php

/**
 * @file
 * Contains \Drupal\mymodule\MyModuleSubscriber.
 */
namespace Drupal\mymodule;

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


/**
 * Provides a MyModuleSubscriber.
 */
class MyModuleSubscriber implements EventSubscriberInterface {

 
/**
   * // only if KernelEvents::REQUEST !!!
   * @see Symfony\Component\HttpKernel\KernelEvents for details
   *
   * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The Event to process.
   */
  public function MyModuleLoad(GetResponseEvent $event) {
    // @todo remove this debug code
    drupal_set_message('MyModule: subscribed');
  }

 
/**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('MyModuleLoad', 20); // Like hook_init (Cached)
    // Priority greater than 200 alow to skip caching, like functionality of hook_boot.
    //$events[KernelEvents::REQUEST][] = array('MyModuleLoad', 300); // Like hook_boot (not Cached)
    return $events;
  }
}

 

 

Using StackMiddleware (Not tested)

Example :

mymodule/mymodule.services.yml

services:
  http_middleware.mymodule:
    class: Drupal\mymodule\StackMiddleware\MyModule
    tags:
      - { name: http_middleware, priority: 180, responder: true }

mymodule/src/StackMiddleware/MyModule.php

<?php

namespace Drupal\mymodule\StackMiddleware;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
 * Provides a HTTP middleware.
 */
class MyModule implements HttpKernelInterface {

  /**
   * The wrapped HTTP kernel.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $httpKernel;

  /**
   * Constructs a MyModule object.
   *
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $kernel
   *   The decorated kernel.
   * @param mixed $optional_argument
   *   (optional) An optional argument.
   */
  public function __construct(HttpKernelInterface $http_kernel) {
    $this->httpKernel = $http_kernel;
  }

  /**
   * {@inheritdoc}
   */
  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
    // Your code here.
    return $this->httpKernel->handle($request, $type, $catch);
  }
}

 

Examples from : https://www.drupal.org/node/1909596

Add new comment