QR codes have been printed out for a bunch of students in a university. When they visit the site via those QR codes, and they are logged-in, the refcode query parameter used in the link needs to be saved for their account in a field_usr_ref_code field (a Text (plain) field).

In a .module file I am using the following code.

use Drupal\user\Entity\User;

 * Implements hook_entity_load().
function my_custom_module_entity_load() {
  $refcode = \Drupal::request()->query->get('refcode');
  $user_id = \Drupal::currentUser()->id();
  if ($user_id > '0' && isset($refcode)) {
    $account = User::load($user_id);    
    $account->set('field_usr_ref_code', $refcode);

However, for some reason no data is being saved, without any logged error.

What is wrong with the code I am using?

  • Quotes are for strings, not for numbers/integers. $currentuserid > '0' is best to have as $currentuserid > 0. In addition, else { // Do nothing } if you are really doing nothing, then delete the else code, as having an else is not mandatory for if statements.
    – No Sssweat
    Commented Jun 17, 2023 at 11:00
  • However, despite having those 2 blemishes that I pointed out in my previous comment, your code should've worked. So your issue is else where. In the hook function name, did you rename the my_custom_module part to your actual custom module's name? Did you make sure your module is indeed enabled? You could also add \Drupal::logger('my_custom_module')->notice('My HOOK works!'); in your hook code to check that your hook is needed being executed. Then go to /admin/reports/dblog and check if it got logged.
    – No Sssweat
    Commented Jun 17, 2023 at 11:17
  • 1
    Drupal 8 is end of life. Each day the risk increases that the site will be hacked.
    – mona lisa
    Commented Jun 17, 2023 at 11:20
  • @NoSssweat I have updated my code based on your input; And yes the module is enabled, all caches are cleared and the module is renamed as it should be... Still not working
    – Maher B
    Commented Jun 17, 2023 at 12:42
  • Is the module’s meta file named my_custom_module.info.yml?
    – mona lisa
    Commented Jun 17, 2023 at 13:06

1 Answer 1


Use a request event subscriber with a priority of 299:



namespace Drupal\mymodule\EventSubscriber;

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

 * Mymodule event subscriber.
class MymoduleSubscriber implements EventSubscriberInterface {

   * Kernel request event handler.
   * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
   *   The request event.
  public function onKernelRequest(RequestEvent $event) {
    if (!$event->isMainRequest()) {
    // put here your code

   * {@inheritdoc}
  public static function getSubscribedEvents() {
    return [
      KernelEvents::REQUEST => ['onKernelRequest', 299],


    class: Drupal\mymodule\EventSubscriber\MymoduleSubscriber
      - { name: event_subscriber }

Drupal runs authentication with priority 300.

This code is for Drupal 9/10.

For Drupal 8 you need to use Symfony\Component\HttpKernel\Event\GetResponseEvent and $event->isMasterRequest().

  • 2
    For Drupal 8 you need to use Drupal 9 😏
    – mona lisa
    Commented Jun 17, 2023 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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