diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index 0ce0f98a31..32cbcfb6be 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -4,6 +4,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Entity\Exception\MissingEntityBundleException; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -93,6 +94,17 @@ protected function doCreate(array $values) { * {@inheritdoc} */ public function createWithSampleValues($bundle, array $values = []) { + $bundle_type = $this->entityType->getBundleEntityType(); + if (!$bundle_type && $bundle != $this->entityTypeId) { + $bundle = $this->entityTypeId; + } + if ($bundle_type) { + $bundle_type = $this->entityManager->getStorage($bundle_type); + $bundle_entity = $bundle_type->load($bundle); + if (!$bundle_entity) { + throw new MissingEntityBundleException($bundle); + } + } $bundle_key = $this->entityType->getKey('bundle'); $values[$bundle_key] = $bundle; // Bundle is already set, and ID and revision should never be set. @@ -103,7 +115,6 @@ public function createWithSampleValues($bundle, array $values = []) { ]; // Forbid sample generation on any keys whose values were submitted. $forbidden_keys = array_merge($forbidden_keys, array_keys($values)); - // Fallback entity generation in case no custom generator is supplied. /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ $entity = $this->create($values); foreach ($entity as $field_name => $value) { diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageInterface.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageInterface.php index 4e7ad6134e..f851723758 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageInterface.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageInterface.php @@ -25,7 +25,7 @@ public function createTranslation(ContentEntityInterface $entity, $langcode, arr /** - * Generates an entity with sample field values. + * Creates an entity with sample field values. * * @param string $bundle * The entity bundle. @@ -35,8 +35,8 @@ public function createTranslation(ContentEntityInterface $entity, $langcode, arr * @return \Drupal\Core\Entity\FieldableEntityInterface * A fieldable content entity. * - * @throws \Drupal\Core\Entity\Exception\EntityNotFieldableException - * Thrown if the chosen entity type is not a content entity. + * @throws \Drupal\Core\Entity\Exception\MissingEntityBundleException + * Thrown if the requested entity bundle does not exist. */ public function createWithSampleValues($bundle, array $values = []); diff --git a/core/lib/Drupal/Core/Entity/Exception/MissingEntityBundleException.php b/core/lib/Drupal/Core/Entity/Exception/MissingEntityBundleException.php new file mode 100644 index 0000000000..a26682b770 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/Exception/MissingEntityBundleException.php @@ -0,0 +1,29 @@ +entityTypeManager->getDefinitions() as $entity_type_id => $definition) { - if ($definition instanceof ContentEntityType) { - // Generate entities with bundles. + if ($definition->entityClassImplements(FieldableEntityInterface::class)) { + // Create sample entities with bundles. if ($bundle_type = $definition->getBundleEntityType()) { foreach ($this->entityTypeManager->getStorage($bundle_type)->loadMultiple() as $bundle) { $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues($bundle->id()); @@ -64,7 +64,7 @@ public function testGenerateContentEntity() { $this->assertEquals(0, $violations->count()); } } - // Generate entities without bundles. + // Create sample entities without bundles. else { $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues($entity_type_id); $violations = $entity->validate();