The 'entity:user' context is required and not present

Created on 30 April 2023, over 1 year ago
Updated 8 September 2023, about 1 year ago

Problem/Motivation

Drupal suddenly stopped to work. I was working on a theme when Drupal started to throw a \Drupal\Component\Plugin\Exception\ContextException on every page view.

The error message and a backtrace are attached. Due to recursion in objects the output of debug_print_backtrace() could not be retrieved.

Steps to reproduce

I don't know. The last thing I remember was clearing all caches.

Proposed resolution

The function Drupal\Core\ParamConverter\EntityConverter->convert contains the code

    $context_id = '@user.current_user_context:current_user';
    if (isset($contexts[$context_id])) {
      $account = $contexts[$context_id]->getContextValue();
      unset($account->_skipProtectedUserFieldConstraint);
      unset($contexts[$context_id]);
    }

which fails. Obviously the result is thrown away for some reason. So there is no need to fail at this point.
The following code seems to work for me:

    $context_id = '@user.current_user_context:current_user';
    if (isset($contexts[$context_id])) {
      try {
        $account = $contexts[$context_id]->getContextValue();
        unset($account->_skipProtectedUserFieldConstraint);
      } catch (\Drupal\Component\Plugin\Exception\ContextException $e) {
      }
      unset($contexts[$context_id]);
    }

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Closed: duplicate

Version

11.0 ๐Ÿ”ฅ

Component
Entityย  โ†’

Last updated about 16 hours ago

  • Maintained by
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom @catch
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland @berdir
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany @hchonov
Created by

๐Ÿ‡ฉ๐Ÿ‡ชGermany keinstein

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

  • Issue created by @keinstein
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Environment: PHP 8.0 & MySQL 5.7 updated deps
    last update over 1 year ago
    Custom Commands Failed
  • The error is โ€œ The 'entity:user' context is required and not presentโ€.

    https://www.drupal.org/forum/support/post-installation/2022-03-12/how-to... โ†’

    https://www.drupal.org/project/migration_tools/issues/3353844 ๐Ÿ’ฌ 'entity:user' context is required and not present Closed: cannot reproduce

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany keinstein

    @cilefen What do you mean with the links?

    I don't see why change in templates/layout/page.html.twig or playing around with scss and css files should trigger such a bug.

    A framework that is so sensitive to changes is buggy in my opinion. Or in other words: If the theme is buggy the framework should help the themers to track down the error and not blur it.

    Btw.: How do the links relate to https://www.drupal.org/node/2934192 โ†’ and the comment in the following source code?

    // @todo At the moment we do not need the current user context, which is
    //   triggering some test failures. We can remove these lines once
    //   https://www.drupal.org/node/2934192 is fixed.
    
  • Those links discuss the same error. I thought they may help.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States webdrips

    The patch worked for me.

    The problem occurred during a migration (I believe it was during migrate-import --all).

    I couldn't view the site as an anonymous user.

  • Status changed to RTBC over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ฆUkraine rollins

    patch resolved the bug, now an anonymous user can see the website
    I am using Drupal Core 9.5.9

  • Status changed to Needs work over 1 year ago
  • The patch did not pass code quality tests. Also it lacks regression test coverage.

  • Status changed to Needs review over 1 year ago
  • last update over 1 year ago
    30,335 pass
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia karishmaamin

    Tried to fix custom code error with the patch

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Previously tagged for tests that still need to happen.

    Also should target the 11.x branch as that's the current development branch. Changes can be backported later.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi Perth, Australia

    Is this the same issue as ๐Ÿ› User context missing when using toUrl in some circumstances Needs work ? likey caused by a missing anonymous user row from a bad database dump

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim
    +++ b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php
    @@ -146,8 +147,12 @@ public function convert($value, $definition, $name, array $defaults) {
    +      catch (ContextException $e) {
    +      }
    

    Silently catching an exception doesn't seem like a good fix to me.

    At the very least this requires a comment to explain a) why this might happen and b) why it's ok to silently ignore it.

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance Syone

    Hello, I have the same issue after updating the database with the update.php script. Has anyone find out how to fix this?

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance Syone

    I finally find out what happened, this is what @dpi described.
    I've made a database dump and then reload it in my local MySQL database.
    The insert query try to create a user with a uid = 0 (the anonymous user) but in MySQL when the mode NO_AUTO_VALUE_ON_ZERO is not enable, it will use the auto increment sequence instead of inserting 0 as uid.

  • Status changed to Closed: duplicate over 1 year ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi Perth, Australia

    Closing this as duplicate ๐Ÿ› User context missing when using toUrl in some circumstances Needs work . I also see the patch in both issues are very similar.

    Reopen if the differences can be described.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi Perth, Australia
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium herved

    I have now stumbled on this for 2 projects in behat tests.
    FWIW in my case it's not the missing anonymous user issue (it's there) ๐Ÿ› The 'entity:user' context is required and not present Closed: duplicate
    Here is my stacktrace:

    Context.php:72, Drupal\Core\Plugin\Context\Context->getContextValue()
    EntityConverter.php:149, Drupal\Core\ParamConverter\EntityConverter->convert()
    ParamConverterManager.php:100, Drupal\Core\ParamConverter\ParamConverterManager->convert()
    ParamConversionEnhancer.php:45, Drupal\Core\Routing\Enhancer\ParamConversionEnhancer->enhance()
    Router.php:270, Drupal\Core\Routing\Router->applyRouteEnhancers()
    Router.php:150, Drupal\Core\Routing\Router->matchRequest()
    AccessAwareRouter.php:90, Drupal\Core\Routing\AccessAwareRouter->matchRequest()
    AccessAwareRouter.php:144, Drupal\Core\Routing\AccessAwareRouter->match()
    LanguageNegotiationUserAdmin.php:138, Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin->isAdminPath()
    LanguageNegotiationUserAdmin.php:104, Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin->getLangcode()
    LanguageNegotiator.php:197, Drupal\language\LanguageNegotiator->negotiateLanguage()
    LanguageNegotiator.php:137, Drupal\language\LanguageNegotiator->initializeType()
    ConfigurableLanguageManager.php:218, Drupal\language\ConfigurableLanguageManager->getCurrentLanguage()
    LanguageRequestSubscriber.php:92, Drupal\language\EventSubscriber\LanguageRequestSubscriber->setLanguageOverrides()
    LanguageRequestSubscriber.php:74, Drupal\language\EventSubscriber\LanguageRequestSubscriber->onKernelRequestLanguage()
    ContainerAwareEventDispatcher.php:111, call_user_func:{/home/herve/Documents/commission/ecdc/ecdc-monorepo/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php:111}()
    ContainerAwareEventDispatcher.php:111, Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch()
    HttpKernel.php:142, Symfony\Component\HttpKernel\HttpKernel->handleRaw()
    HttpKernel.php:74, Symfony\Component\HttpKernel\HttpKernel->handle()
    Session.php:58, Drupal\Core\StackMiddleware\Session->handle()
    KernelPreHandle.php:48, Drupal\Core\StackMiddleware\KernelPreHandle->handle()
    ReverseProxyMiddleware.php:48, Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
    NegotiationMiddleware.php:51, Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
    StackedHttpKernel.php:51, Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
    DrupalKernel.php:704, Drupal\Core\DrupalKernel->handle()
    index.php:19, {main}()
    

    This issue seems to happen somewhat randomly for me.
    After some investigation, this is happening exclusively on image styles routes.
    I believe the random factor is partially due to the fact that I use selenium, and the browser caches images (so disabling selenium's browser cache helps), but also a race condition as follows: behat ends the scenario, deletes the user, and at the same time, the homepage gets refreshed and lazy loads the images, creating new requests.
    Most probably the user still exists at the beginning of these image styles requests, but behat (still running in the background) deletes the user and when the image style requests arrives in \Drupal\user\ContextProvider\CurrentUserContext::getRuntimeContexts and attempts to load the user, it returns NULL (this I confirmed), causing in turn The 'entity:user' context is required and not present exception to be thrown from \Drupal\Core\Plugin\Context\Context::getContextValue

    I will see if ๐Ÿ› User context missing when using toUrl in some circumstances Needs work helps solving the issue.
    PS: I know this issue is closed but perhaps this info could be useful to someone else, who knows.

Production build 0.71.5 2024