Posted by ParisLiakos on February 6, 2013 at 2:22pm
Project:
Drupal core
Introduced in branch:
8.x Description:
In Drupal 7 hook_boot() was being fired on the beginning of all page requests, providing modules the ability to run code on cached pages.
In Drupal 8 hook_boot() was removed since the same results can be achieved with other ways, for example:
- A module that needs to interrupt the request very early based on certain conditions can use an event listener (see #1794754: Convert ban_boot() to an event listener)
- A module that needs to run on cached pages should prompt its users to add code in settings.php
- In any other case hook_init() should be used instead.
Drupal 7
<?php
/**
* Implementation of hook_boot(). Runs even for cached pages.
*/
function mymodule_boot() {
// @todo remove this debug code
drupal_set_message('mymodule_boot executed');
}
?>Drupal8
<?php
// file should be in /mymodule/lib/Drupal/mymodule/MyModuleBundle .php
/**
* @file
* Definition of Drupal\mymodule\MyModuleBundle .
*/
namespace Drupal\mymodule;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* The bundle for mymodule.module.
*/
class MyModuleBundle extends Bundle {
public function build(ContainerBuilder $container) {
$container->register('mymodule.mymodule_subscriber', Drupal\mymodule\MyModuleSubscriber')
->addTag('event_subscriber');
}
}
?><?php
// file shoud be in /mymodule/lib/Drupal/mymodule/MyModuleSubscriber.php
/**
* @file
* Definition of 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;
/**
* Implements 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');
}
/**
* Implements EventSubscriberInterface::getSubscribedEvents().
*/
static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array('MyModuleLoad', 20);
return $events;
}
}
?>Impacts:
Module developers