How to execute custom code at Drupal startup What's the correct way to execute code from an existing custom module at Drupal startup? The code checks certain private_tempstore values and sets other values based on what it finds. It should be executed before page processing occurs so that pages can respond to the changed values.

I've read many posts, looked through the list of hooks, tried a few without success, looked at event subscribing, etc. All my searching and trying hasn't found an approach that works correctly for my situation.

Example custom code (not the real code):

$rsc = \Drupal::service('user.private_tempstore')-&gt;get('rsc');
$val = $rsc-&gt;get('temp_val');

if ($val == 100) {
  $rsc-&gt;set('temp_val', 200);
}


What I've tried:

hook_page_build() // in mymodule.module
hook_page_alter() // in mymodule.module
path: '/*' // in mymodule.routing.yml


No error messages, just no results via an error_log() in the custom code to show that it was executed.
First of all, User module's user.private_tempstore and user.shared_tempstore services moved to core. So we want to use the tempstore.private.

The code below will get your started. you will want to read more about how getSubscribedEvents() works, as here is where you tweak the where and when of your event.

Don't forget an .info.yml file!

/mymodule/mymodule.services.yml

services:
  mymodule.mymodule_subscriber:
    class: Drupal\mymodule\MyModuleSubscriber
    arguments:
      - '@tempstore.private'
    tags:
      - { name: 'event_subscriber' }


/mymodule/src/MyModuleSubscriber.php

&lt;?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;
use Drupal\Core\TempStore\PrivateTempStore;


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

   /**
   * The PrivateTempStore Service
   *
   * @var PrivateTempStore
   */
   protected $privateTempStore;

   /**
   * MyModuleSubscriber constructor.
   *
   * @param PrivateTempStore $privateTempStore
   */
   public function __construct(PrivateTempStore $privateTempStore)
   {
     $this-&gt;privateTempStore = $privateTempStore;
   }


   /**
   * // 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');
    // Interact with the tempstore
    $value = $this-&gt;privateTempStore-&gt;doSomething();
  }


/**
   * {@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;
  }
}