Error Message

1
Drupal\Component\Plugin\Exception\ContextException: The 'entity:user' context is required and not present. in Drupal\Core\Plugin\Context\Context->getContextValue() (line 73 of core/lib/Drupal/Core/Plugin/Context/Context.php).

2023.11.15 - 115721

Working Solution for Me

File: public_html/core/lib/Drupal/Core/ParamConverter/EntityConverter.php b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php

1
2
3
4
5
6
7
8
9
-    if (isset($contexts[$context_id])) {
-      $account = $contexts[$context_id]->getContextValue();
-      unset($account->_skipProtectedUserFieldConstraint);
-      unset($contexts[$context_id]);
-    }
+    if (isset($contexts[$context_id]) && $contexts[$context_id]->hasContextValue()) {
+        $account = $contexts[$context_id]->getContextValue();
+        unset($account->_skipProtectedUserFieldConstraint, $contexts[$context_id]);
+    }

Reference

Community Solution 1

File: public_html/core/lib/Drupal/Core/ParamConverter/EntityConverter.php b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
diff --git a/core/lib/Drupal/Core/ParamConverter/EntityConverter.php b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php
index aa2b00d647..7f87d1c9c8 100644
--- a/core/lib/Drupal/Core/ParamConverter/EntityConverter.php
+++ b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php
@@ -145,10 +145,9 @@ public function convert($value, $definition, $name, array $defaults) {
     //   triggering some test failures. We can remove these lines once
     //   https://www.drupal.org/node/2934192 is fixed.
     $context_id = '@user.current_user_context:current_user';
-    if (isset($contexts[$context_id])) {
+    if (isset($contexts[$context_id]) && $contexts[$context_id]->hasContextValue()) {
       $account = $contexts[$context_id]->getContextValue();
-      unset($account->_skipProtectedUserFieldConstraint);
-      unset($contexts[$context_id]);
+      unset($account->_skipProtectedUserFieldConstraint, $contexts[$context_id]);
     }
     $entity = $this->entityRepository->getCanonical($entity_type_id, $value, $contexts);

diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityUrlTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityUrlTest.php
new file mode 100644
index 0000000000..70ab99719c
--- /dev/null
+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityUrlTest.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace Drupal\KernelTests\Core\Entity;
+
+use Drupal\Core\Url;
+use Drupal\entity_test\Entity\EntityTest;
+
+/**
+ * Tests URL method.
+ *
+ * @group Entity
+ */
+class EntityUrlTest extends EntityKernelTestBase {
+
+  public function testUrl() {
+    $this->installEntitySchema('entity_test');
+    $this->installEntitySchema('user');
+
+    $entity = EntityTest::create();
+    $entity->save();
+    $entityId = $entity->id();
+    $url = Url::fromUri('internal:/entity_test/' . $entityId);
+    $this->assertEquals('entity.entity_test.canonical', $url->getRouteName());
+    $this->assertEquals(['entity_test' => $entityId], $url->getRouteParameters());
+  }
+
+}

Reference

Community Solution 2

File: public_html/core/lib/Drupal/Core/ParamConverter/EntityConverter.php b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
diff --git a/core/lib/Drupal/Core/ParamConverter/EntityConverter.php b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php
index c3820baaca..3f69b2b858 100644
--- a/core/lib/Drupal/Core/ParamConverter/EntityConverter.php
+++ b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php
@@ -2,6 +2,7 @@

 namespace Drupal\Core\ParamConverter;

+use Drupal\Component\Plugin\Exception\ContextException;
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityRepositoryInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
@@ -146,8 +147,12 @@ public function convert($value, $definition, $name, array $defaults) {
     //   https://www.drupal.org/node/2934192 is fixed.
     $context_id = '@user.current_user_context:current_user';
     if (isset($contexts[$context_id])) {
-      $account = $contexts[$context_id]->getContextValue();
-      unset($account->_skipProtectedUserFieldConstraint);
+      try {
+        $account = $contexts[$context_id]->getContextValue();
+        unset($account->_skipProtectedUserFieldConstraint);
+      }
+      catch (ContextException $e) {
+      }
       unset($contexts[$context_id]);
     }
     $entity = $this->entityRepository->getCanonical($entity_type_id, $value, $contexts);

Reference: