diff --git a/core/lib/Drupal/Core/ParamConverter/EntityConverter.php b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php index 939757fd1e19d5189c232e0cfbb8b8ccc30fad51..515517b91129009e07d408da1f211c772f111eaf 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\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Plugin\Context\Context; @@ -142,7 +143,11 @@ public function convert($value, $definition, $name, array $defaults) { } $entity = $this->entityRepository->getCanonical($entity_type_id, $value, $contexts); - if (!empty($definition['bundle']) && !in_array($entity->bundle(), $definition['bundle'], TRUE)) { + if ( + !empty($definition['bundle']) && + $entity instanceof EntityInterface && + !in_array($entity->bundle(), $definition['bundle'], TRUE) + ) { return NULL; } diff --git a/core/tests/Drupal/KernelTests/Core/ParamConverter/EntityConverterTest.php b/core/tests/Drupal/KernelTests/Core/ParamConverter/EntityConverterTest.php index 2ad31fa97af04630d6384cfdfc466d85beded37a..9dfb943cd451f64eee9267c4d5cc96f4a3be4dde 100644 --- a/core/tests/Drupal/KernelTests/Core/ParamConverter/EntityConverterTest.php +++ b/core/tests/Drupal/KernelTests/Core/ParamConverter/EntityConverterTest.php @@ -85,6 +85,10 @@ public function testRouteParamWithBundleDefinition(): void { $converted = $converter->convert($entity3->id(), $definition, 'qux', []); $this->assertNull($converted); + // A non-existing entity returns NULL. + $converted = $converter->convert('some-non-existing-entity-id', $definition, 'qux', []); + $this->assertNull($converted); + $definition = [ 'type' => 'entity:entity_test', ]; @@ -96,6 +100,8 @@ public function testRouteParamWithBundleDefinition(): void { $this->assertSame($entity2->id(), $converted->id()); $converted = $converter->convert($entity3->id(), $definition, 'qux', []); $this->assertSame($entity3->id(), $converted->id()); + $converted = $converter->convert('some-non-existing-entity-id', $definition, 'qux', []); + $this->assertNull($converted); } }