After updating my local d 8.5.7 to d 8.6.0 (with composer) and testing as anonymous the below error was blown in my face ! No such error when browsing drupal as authenticated user.

The website encountered an unexpected error. Please try again later.
TypeError: Argument 1 passed to Drupal\Core\Plugin\Context\EntityContext::fromEntity() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in pathToDrupal\d8root\core\modules\user\src\ContextProvider\CurrentUserContext.php on line 58 in Drupal\Core\Plugin\Context\EntityContext::fromEntity() (line 56 of core\lib\Drupal\Core\Plugin\Context\EntityContext.php).

Drupal\Core\Plugin\Context\EntityContext::fromEntity(NULL, Object) (Line: 58)
Drupal\user\ContextProvider\CurrentUserContext->getRuntimeContexts(Array) (Line: 79)
Drupal\Core\Plugin\Context\LazyContextRepository->getRuntimeContexts(Array) (Line: 91)
Drupal\block\BlockAccessControlHandler->checkAccess(Object, 'view', Object) (Line: 105)
Drupal\Core\Entity\EntityAccessControlHandler->access(Object, 'view', Object, 1) (Line: 365)
Drupal\Core\Entity\Entity->access('view', NULL, 1) (Line: 56)
Drupal\block\BlockRepository->getVisibleBlocksPerRegion(Array) (Line: 137)
Drupal\block\Plugin\DisplayVariant\BlockPageVariant->build() (Line: 259)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 156)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 184)
Drupal\page_cache\StackMiddleware\PageCache->fetch(Object, 1, 1) (Line: 121)
Drupal\page_cache\StackMiddleware\PageCache->lookup(Object, 1, 1) (Line: 75)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 665)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

I found out it's caused by some of the enabled blocks which have their visibility restricted for some blocks by Roles + something else.
The error also was triggered by another custom basic block called "Message for anonymous" which has visibility set by Content types, Pages, and Roles.

  • I removed all restrictions but it didn't help.
  • I deleted the block and created a new one with only Roles restriction but that caused the error too!

A third custom basic block that's meant to be seen by certain roles also caused the error. Its visibility is restricted by Pages + Roles.
I removed the Roles restriction and kept the Pages only and that fixed it.

Two or more other blocks that caused the error too were restricted only by roles and the error was gone when I removed the restriction.

I hope someone can shed some light on how to fix this or what I should do to better manage it.

Thanks

Comments

cestmoi created an issue. See original summary.

cestmoi’s picture

Category: Bug report » Support request

I changed category from Bug report to Support request after I tested on simplytest.me and couldn't reproduce the error. So it seems it's something with my install but I have yet to figure it out hopefully with your kind help here.

If someone can direct me in the right direction for how to debug it and find the culprit, it would be greatly appreciated.

Thank you

cilefen’s picture

cestmoi’s picture

I should've added that I first ran the update from D 8.5.5 with composer update and that updated to D 8.5.7 instead of 8.6.0

Trying to make it update to the latest 8.6.0, I tried several composer commands to no avail until I edited the composer.json and changed drupal core versions to ^8.6.0 and then:
composer update drupal/core webflo/* --with-dependencies

That made the update from 8.5.7 to 8.6.0 succeed.

I then: drush updatedb

Then: drush cr

I tested as admin user and all looked fine except for the breadcrumbs menu first showing the word "node" instead of the title of the node !! This seemed to solve itself later idk how or if related to drush cr I did .

I tested as anonymous and that's when I first noticed the TypeError.

The only common trigger among troublesome blocks seems to be setting block visibility by Roles whether alone or in addition to others. Other visibility settings don't seem to trigger the error.

The type of block doesn't seem to matter. I even tried setting the visibility by roles for the "Site Branding" block and that triggered the error.

I have the Content Access module installed if that may matter to know.

I don't know what else to try. Please advise me on what else I could do.

I was going to move the site to production right before I made this bad update!

+ In addition to the oroginal error mentioned here, I have the below warning (which is reported by others elsewhere lately) when I browse to /admin/reports/dblog

Warning: Illegal string offset 'value' in Drupal\views\Plugin\views\area\Text->preQuery() (line 50 of core\modules\views\src\Plugin\views\area\Text.php).

		Drupal\views\Plugin\views\area\Text->preQuery() (Line: 1011)
		Drupal\views\ViewExecutable->_preQuery() (Line: 1233)
		Drupal\views\ViewExecutable->build() (Line: 390)
		Drupal\views\Plugin\views\display\PathPluginBase->execute() (Line: 180)
		Drupal\views\Plugin\views\display\Page->execute() (Line: 1630)
		Drupal\views\ViewExecutable->executeDisplay('page', Array) (Line: 77)
		Drupal\views\Element\View::preRenderViewElement(Array)
		call_user_func(Array, Array) (Line: 378)
		Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
		Drupal\Core\Render\Renderer->render(Array, ) (Line: 226)
		Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 582)
		Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227)
		Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
		Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
		Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
		call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
		Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 156)
		Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
		Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
		Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
		Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
		Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
		Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
		Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
		Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
		Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 665)
		Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Thanks

cestmoi’s picture

I'm wondering if this issue is somehow related to Block visibility setting tab for Roles not showing (2952962)

cestmoi’s picture

A list of installed modules just in case:

drush config-get core.extension module

'core.extension:module':
  addtoany: 0
  automated_cron: 0
  big_pipe: 0
  block: 0
  block_content: 0
  book: 0
  breakpoint: 0
  checklistapi: 0
  ckeditor: 0
  ckeditor_media_embed: 0
  color: 0
  comment: 0
  config: 0
  config_translation: 0
  contact: 0
  content_access: 0
  contextual: 0
  datetime: 0
  datetime_range: 0
  dblog: 0
  draggableviews: 0
  editor: 0
  editor_advanced_link: 0
  fakeobjects: 0
  field: 0
  field_ui: 0
  file: 0
  filter: 0
  footnotes: 0
  help: 0
  hierarchical_taxonomy_menu: 0
  history: 0
  image: 0
  imce: 0
  language: 0
  link: 0
  linkit: 0
  locale: 0
  menu_ui: 0
  migrate: 0
  migrate_drupal: 0
  migrate_drupal_multilingual: 0
  migrate_drupal_ui: 0
  migrate_manifest: 0
  migrate_plus: 0
  migrate_tools: 0
  migrate_upgrade: 0
  node: 0
  options: 0
  path: 0
  quickedit: 0
  responsive_image: 0
  search: 0
  seo_checklist: 0
  shortcut: 0
  social_media_links: 0
  statistics: 0
  system: 0
  taxonomy: 0
  telephone: 0
  text: 0
  toolbar: 0
  tour: 0
  tracker: 0
  update: 0
  user: 0
  views_ui: 0
  weight: 0
  menu_link_content: 1
  content_translation: 10
  views: 10
  standard: 1000
cestmoi’s picture

Any help whatsoever !!! Any hints on where/how to start to troubleshoot this issue/bug ???

Unfortunately, I was so lazy to make a backup before that stupid update and my only option now to use a module to set different theme for different roles and remove any visibility restrictions by Roles from any block in the theme for anonymous.

This still does NOT address the original issue/bug but my experience with drupal support over more than 10 years has been very very negative and in most cases I ended up solving things on my own unlike this time I feel I'm out of ideas or directions where to go next.

This is a minor version (feature release) of Drupal 8 and is ready for use on production sites

The above statement on the drupal 8.6.0 release page is misleading and I think shouldn't be there. A BIG warning and reminder to backup and be ready for the worst should be in its place instead.

It seems drupal is trying hard to push its loyal users away. Everything is changing all the time and one just needs to keep running to catch up ! That's including in addition to drupal major upgrades and the big changes involved, changes in drush versions and deprecation of some helpful commands, as well as the still not user-friendly nor intuitive at all composer and yet, drupal is somehow forcing everyone to use drush and composer !!!

Thanks for your understanding and if not then please spare me any lectures. That's how I feel and I'm sure so many others do whether some like it or not.

If anyone is reading ever !

mwnemesis’s picture

I have a website running on 8.5.7 and trying to update to 8.6.1 happen the error you wrote, did you find any way to fix it? The error happen only for anonymus user, for logged user the site is working fine.

UPDATE: I found a temporary fix for my problem:

In the CurrentUserContext (path: /core/modules/user/src/ContextProvider/CurrentUserContext.php) in the function:
public function getRuntimeContexts(array $unqualified_context_ids) {

there is a condition:

if ($current_user) {
  // @todo Do not validate protected fields to avoid bug in TypedData,
  //   remove this in https://www.drupal.org/project/drupal/issues/2934192.
  $current_user->_skipProtectedUserFieldConstraint = TRUE;
}

I've changed it with:

if ($current_user) {
  // @todo Do not validate protected fields to avoid bug in TypedData,
  //   remove this in https://www.drupal.org/project/drupal/issues/2934192.
  $current_user->_skipProtectedUserFieldConstraint = TRUE;
} else {
  $current_user = \Drupal\user\Entity\User::getAnonymousUser();
}
cestmoi’s picture

@mwnemesis thanks so much for sharing the fix, it worked for me too.

It would be great though if you share more info on the steps you took to do the update and how you could reproduce the issue and if any different than my steps, did you update via composer/drush or UI ? what modules do you have installed ? any other info to help shed more light on the culprit and hopefully get drupal developers more interested in fixing this bug/issue at its root .

In the meantime, I had found a way around so I could allow anonymous users to browse the site and same time hide restricted blocks from them. I did so by using the role_theme_switcher module and enabling a different theme (actually a copy of my original theme) and removing the blocks that are not meant for anonymous users from it in its settings. I am probably going to keep this later on, even if the root cause of this issue was found and fixed, because it gives me more peace of mind and more control over what to show to each role.

mwnemesis’s picture

@cestmoi i had the same error starting form my site that is hardly customized so i don't know if the problem exist only for my customization or is depending on something else. I hope soon to have time to try and reproduce the problem starting from an new installation (maybe is depending on some other module or it's because the site is born from a Drupal 8.4.x and has been update on every drupal release).

The only step that i had is:

- (obviously) backup of apache folder and database.
- Delete all root files and folders (excude modules, site and theme)
- copy all the new files from the new version (exclude the site folder that has been merged manually)
- run the update.php from the browser

I cannot do the update via composer/drush because the only access we have is via ftp

trickfun’s picture

I have the same problem.
When the user is anonymous, i get the error. when i am logged in no error is throw.

The error appears only when i set the visibility to "authenticated user"

This is my block class

use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'Hello' Block.
 *
 * @Block(
 *   id = "gey_commerce_masquerade_block",
 *   admin_label = @Translation("Dkz: Commerce Masquerade block"),
 *   category = @Translation("Commerce"),
 * )
 */
class MasqueradeBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
      
      $renderable = [
          '#theme' => 'gey_masquerade__info_block',
      ];
      
      $content = \Drupal::service('renderer')->render($renderable);
      
      return [
          '#markup' => $content,
          '#attached' => array(
              'library' =>  array(
                  'gey_commerce_masquerade/masquerade'
              ),
              'drupalSettings' => array(
                  'gey_commerce_masquerade' => ['url' => '/api/masquerade'] 
              ) 
          ),
          '#cache' => array(
              'max-age' => 0
          )
      ];
  }

}

jeroenvl’s picture

Version: 8.6.0 » 8.6.1
Component: entity system » block.module
Category: Support request » Bug report
Issue tags: -8.6.0 update, -Needs steps to reproduce +8.6.1 update

We encountered the same issue when updating to 8.6.1...

It indeed seems the issue appears when a block (in our case a custom block with some plain HTML), is placed on the front page (with ), specifically when a role is required. In our case the error shows if the block is shown to all users except for Anonymous, but also in case of different combinations. (I hope these steps help to reproduce the issue)

We temporarily solved the issue by not applying any role and simply displaying the block to all users :)

tim.plunkett’s picture

Title: TypeError: Argument 1 passed to Drupal\Core\Plugin\Context\EntityContext::fromEntity after updating to D 8.6.0 » [regression] \Drupal\user\ContextProvider\CurrentUserContext broken for anonymous users
Version: 8.6.1 » 8.7.x-dev
Component: block.module » plugin system
Priority: Critical » Major
Status: Active » Needs review
Issue tags: -8.6.1 update
StatusFileSize
new2.43 KB
new3.52 KB

As there are workarounds this is not critical, but still very major.

Here's a test to expose the bug, as well as a fix.

tim.plunkett’s picture

+++ b/core/modules/user/tests/src/Kernel/ContextProvider/CurrentUserContextTest.php
@@ -0,0 +1,62 @@
+    $this->assertTrue($contexts['@user.current_user_context:current_user']->getContextValue());

This is a bad assertion, whoops. Should be something like $this->assertInstanceOf(UserInterface::class, ...

The last submitted patch, 13: 2998335-currentusercontext-13-FAIL.patch, failed testing. View results

tim.plunkett’s picture

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

johnzzon’s picture

Status: Needs review » Reviewed & tested by the community

Encountered this issue when writing a test for a custom context provider. This patch solved the issue.

Had a quick review of it as well, looks good to me!

larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed 0b47db5 and pushed to 8.8.x. Thanks!
c/p as 769dbcdbda and pushed to 8.7.x

  • larowlan committed 769dbcd on 8.7.x
    Issue #2998335 by tim.plunkett: [regression] \Drupal\user\...
  • larowlan committed 0b47db5 on 8.8.x
    Issue #2998335 by tim.plunkett: [regression] \Drupal\user\...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Flyxl’s picture

I am experiencing a problem with the current fix because the user does not get the "anonymous" role.

I have a use case where I have a menu for "anonymous" users and one for "authenticated". This doesn't work with the current fix.

Using #8 fixes this problem but this was not commited to 8.7.x or 8.8.x. Should I just create my own patch and apply everytime I update core or whats the way to go here?

I am currently using 8.7.1

xjm’s picture

Version: 8.8.x-dev » 8.7.x-dev
Issue tags: +Needs followup

@Flyxl, I'd suggest filing a followup for that. Thanks!