0

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')->get('rsc');
$val = $rsc->get('temp_val');

if ($val == 100) {
  $rsc->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.

  • 3
    Could you please you tell us more about what you specifically tried and maybe share some code, as well as error codes or descriptions of what didn't work? – Beau Mar 18 at 14:46
  • 3
    More context around what you're specifically hoping to accomplish would be useful too, as that will probably steer where the code needs to go, how you approach caching, etc – Clive Mar 18 at 14:50
  • 2
    Something like this article? drupal8.ovh/en/tutoriels/357/… – Ryan Hartman Mar 18 at 14:57
  • @Beau I have added some example code and showed what I've tried. – Marshall Morrise Mar 18 at 15:13
  • 1
    This is essentially the same info as @RyanHartman's comment, but maybe easier to grasp: chromatichq.com/blog/… – Beau Mar 18 at 15:22
2

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

<?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->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->privateTempStore->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;
  }
}
| improve this answer | |
  • Thanks, I'll give this a try and report/accept the answer. – Marshall Morrise Mar 18 at 18:13
  • This worked for me after a little trial and error. Thanks so much @Ryan Hartman! – Marshall Morrise Mar 19 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.