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
Comments
Comment #2
cilefen commentedThe 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
Comment #3
keinstein commented@cilefen What do you mean with the links?
I don't see why change in
templates/layout/page.html.twigor playing around withscssandcssfiles 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?
Comment #4
cilefen commentedThose links discuss the same error. I thought they may help.
Comment #5
webdrips commentedThe 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.
Comment #6
rollins commentedpatch resolved the bug, now an anonymous user can see the website
I am using Drupal Core 9.5.9
Comment #7
cilefen commentedThe patch did not pass code quality tests. Also it lacks regression test coverage.
Comment #8
karishmaamin commentedTried to fix custom code error with the patch
Comment #9
smustgrave commentedPreviously 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.
Comment #10
dpiIs this the same issue as #3056234: User context missing when using toUrl in some circumstances? likey caused by a missing anonymous user row from a bad database dump
I suppose this could also happen if a migration occurred and no user-0 was retained
Comment #11
joachim commentedSilently 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.
Comment #12
Syone commentedHello, I have the same issue after updating the database with the update.php script. Has anyone find out how to fix this?
Comment #13
Syone commentedI 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.
Comment #14
dpiClosing this as duplicate #3056234: User context missing when using toUrl in some circumstances. I also see the patch in both issues are very similar.
Reopen if the differences can be described.
Comment #15
dpiComment #16
herved commentedI 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) #3357354: The 'entity:user' context is required and not present
Here is my stacktrace:
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::getRuntimeContextsand attempts to load the user, it returns NULL (this I confirmed), causing in turnThe 'entity:user' context is required and not presentexception to be thrown from\Drupal\Core\Plugin\Context\Context::getContextValueI will see if #3056234: User context missing when using toUrl in some circumstances helps solving the issue.
PS: I know this issue is closed but perhaps this info could be useful to someone else, who knows.
Edit: I ended up applying #3158130: Many calls to ContextRepository::getAvailableContexts() due to entity upcasting which is the proper fix I believe.