diff --git a/core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php b/core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php index 0e45f108184c..08c620b637d0 100644 --- a/core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php +++ b/core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php @@ -57,13 +57,15 @@ class EntityContextDefinition extends ContextDefinition { $constraints = $this->getConstraintObjects(); $entity_type_manager = \Drupal::entityTypeManager(); $entity_type_id = $this->getEntityTypeId(); + $entity_type = $entity_type_manager->getDefinition($entity_type_id); $storage = $entity_type_manager->getStorage($entity_type_id); // If the storage can generate a sample entity we might delegate to that. if ($storage instanceof ContentEntityStorageInterface) { if (!empty($constraints['Bundle']) && $constraints['Bundle'] instanceof BundleConstraint) { foreach ($constraints['Bundle']->getBundleOption() as $bundle) { // We have a bundle, we are bundleable and we can generate a sample. - yield EntityAdapter::createFromEntity($storage->createWithSampleValues($bundle)); + $values = [$entity_type->getKey('bundle') => $bundle]; + yield EntityAdapter::createFromEntity($storage->create($values)); } return; } diff --git a/core/tests/Drupal/Tests/Core/Plugin/Context/EntityContextDefinitionIsSatisfiedTest.php b/core/tests/Drupal/Tests/Core/Plugin/Context/EntityContextDefinitionIsSatisfiedTest.php index 1552c12e4c5a..69adfdd27154 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/Context/EntityContextDefinitionIsSatisfiedTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/Context/EntityContextDefinitionIsSatisfiedTest.php @@ -112,10 +112,16 @@ class EntityContextDefinitionIsSatisfiedTest extends UnitTestCase { $content_entity_storage = $this->prophesize(ContentEntityStorageInterface::class); $this->entityTypeManager->getStorage('test_config')->willReturn($entity_storage->reveal()); $this->entityTypeManager->getStorage('test_content')->willReturn($content_entity_storage->reveal()); + + $config_entity_type = new EntityType(['id' => 'test_config']); + $content_entity_type = new EntityType(['id' => 'test_content']); + $this->entityTypeManager->getDefinition('test_config')->willReturn($config_entity_type); + $this->entityTypeManager->getDefinition('test_content')->willReturn($content_entity_type); $this->entityManager->getDefinitions()->willReturn([ - 'test_config' => new EntityType(['id' => 'test_config']), - 'test_content' => new EntityType(['id' => 'test_content']), + 'test_config' => $config_entity_type, + 'test_content' => $content_entity_type, ]); + $this->entityTypeBundleInfo->getBundleInfo('test_config')->willReturn([ 'test_config' => ['label' => 'test_config'], ]); @@ -186,14 +192,25 @@ class EntityContextDefinitionIsSatisfiedTest extends UnitTestCase { $entity->getEntityTypeId()->willReturn('test_content'); $entity->getIterator()->willReturn(new \ArrayIterator([])); $entity->bundle()->willReturn($bundle); - $content_entity_storage->createWithSampleValues($bundle) + $content_entity_storage->create(['the_bundle_key' => $bundle]) ->willReturn($entity->reveal()) ->shouldBeCalled(); } - $entity_type = new EntityType(['id' => 'test_content']); + // Creating entities with sample values can lead to performance issues when + // called many times. Ensure that createWithSampleValues() is not called. + $content_entity_storage->createWithSampleValues(Argument::any())->shouldNotBeCalled(); + + $entity_type = new EntityType([ + 'id' => 'test_content', + 'entity_keys' => [ + 'bundle' => 'the_bundle_key', + ], + ]); $this->entityTypeManager->getStorage('test_content')->willReturn($content_entity_storage->reveal()); + + $this->entityTypeManager->getDefinition('test_content')->willReturn($entity_type); $this->entityManager->getDefinitions()->willReturn([ 'test_content' => $entity_type, ]);