From c3ebbc485bee9c13f26a60c3ca1ad33d05e3f380 Mon Sep 17 00:00:00 2001
From: Lee Rowlands <lee.rowlands@previousnext.com.au>
Date: Mon, 5 Feb 2018 07:11:18 +1000
Subject: [PATCH] Issue #2936642 by tim.plunkett: Getting runtime contexts will
 generate an E_WARNING for anonymous users

---
 .../user/src/ContextProvider/CurrentUserContext.php       | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/core/modules/user/src/ContextProvider/CurrentUserContext.php b/core/modules/user/src/ContextProvider/CurrentUserContext.php
index 73be2caa0952..aacc5f774afb 100644
--- a/core/modules/user/src/ContextProvider/CurrentUserContext.php
+++ b/core/modules/user/src/ContextProvider/CurrentUserContext.php
@@ -50,9 +50,11 @@ public function __construct(AccountInterface $account, EntityManagerInterface $e
   public function getRuntimeContexts(array $unqualified_context_ids) {
     $current_user = $this->userStorage->load($this->account->id());
 
-    // @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;
+    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;
+    }
 
     $context = new Context(new ContextDefinition('entity:user', $this->t('Current user')), $current_user);
     $cacheability = new CacheableMetadata();
-- 
GitLab