diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index 32cbcfb6be..f6e8155380 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -4,7 +4,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; -use Drupal\Core\Entity\Exception\MissingEntityBundleException; +use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -36,6 +36,13 @@ protected $cacheBackend; /** + * The entity type bundle information object. + * + * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface + */ + protected $entityTypeBundleInfo; + + /** * Constructs a ContentEntityStorageBase object. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type @@ -44,12 +51,15 @@ * The entity manager. * @param \Drupal\Core\Cache\CacheBackendInterface $cache * The cache backend to be used. + * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info + * The entity type bundle information object. */ - public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, CacheBackendInterface $cache) { + public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, EntityTypeBundleInfoInterface $entity_type_bundle_info) { parent::__construct($entity_type); $this->bundleKey = $this->entityType->getKey('bundle'); $this->entityManager = $entity_manager; $this->cacheBackend = $cache; + $this->entityTypeBundleInfo = $entity_type_bundle_info; } /** @@ -59,7 +69,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI return new static( $entity_type, $container->get('entity.manager'), - $container->get('cache.entity') + $container->get('cache.entity'), + $container->get('entity_type.bundle.info') ); } @@ -93,26 +104,25 @@ 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. + public function createWithSampleValues($bundle = FALSE, array $values = []) { + // ID and revision should never have sample values generated for them. $forbidden_keys = [ $this->entityType->getKey('id'), - $this->entityType->getKey('revision'), - $bundle_key ]; + if ($revision_key = $this->entityType->getKey('revision')) { + $forbidden_keys[] = $revision_key; + } + if ($bundle_key = $this->entityType->getKey('bundle')) { + if (!$bundle) { + throw new EntityStorageException("No entity bundle was specified"); + } + if (!array_key_exists($bundle, $this->entityTypeBundleInfo->getBundleInfo($this->entityTypeId))) { + throw new EntityStorageException(sprintf("Missing entity bundle. The \"%s\" bundle does not exist", $bundle)); + } + $values[$bundle_key] = $bundle; + // Bundle is already set + $forbidden_keys[] = $bundle_key; + } // Forbid sample generation on any keys whose values were submitted. $forbidden_keys = array_merge($forbidden_keys, array_keys($values)); /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ diff --git a/core/lib/Drupal/Core/Entity/Exception/MissingEntityBundleException.php b/core/lib/Drupal/Core/Entity/Exception/MissingEntityBundleException.php deleted file mode 100644 index a26682b770..0000000000 --- a/core/lib/Drupal/Core/Entity/Exception/MissingEntityBundleException.php +++ /dev/null @@ -1,29 +0,0 @@ -get('database'), $container->get('entity.manager'), $container->get('cache.entity'), + $container->get('entity_type.bundle.info'), $container->get('language_manager') ); } @@ -159,11 +161,13 @@ public function getFieldStorageDefinitions() { * The entity manager. * @param \Drupal\Core\Cache\CacheBackendInterface $cache * The cache backend to be used. + * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info + * The entity type bundle information object. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. */ - public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager) { - parent::__construct($entity_type, $entity_manager, $cache); + public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, EntityTypeBundleInfoInterface $entity_type_bundle_info, LanguageManagerInterface $language_manager) { + parent::__construct($entity_type, $entity_manager, $cache, $entity_type_bundle_info); $this->database = $database; $this->languageManager = $language_manager; $this->initTableLayout(); diff --git a/core/modules/comment/src/CommentStorage.php b/core/modules/comment/src/CommentStorage.php index d164d8ad9e..fe9c3b1d6a 100644 --- a/core/modules/comment/src/CommentStorage.php +++ b/core/modules/comment/src/CommentStorage.php @@ -5,6 +5,7 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\FieldableEntityInterface; @@ -41,11 +42,13 @@ class CommentStorage extends SqlContentEntityStorage implements CommentStorageIn * The current user. * @param \Drupal\Core\Cache\CacheBackendInterface $cache * Cache backend instance to use. + * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info + * The entity type bundle information object. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. */ - public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, AccountInterface $current_user, CacheBackendInterface $cache, LanguageManagerInterface $language_manager) { - parent::__construct($entity_info, $database, $entity_manager, $cache, $language_manager); + public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, AccountInterface $current_user, CacheBackendInterface $cache, EntityTypeBundleInfoInterface $entity_type_bundle_info, LanguageManagerInterface $language_manager) { + parent::__construct($entity_info, $database, $entity_manager, $cache, $entity_type_bundle_info, $language_manager); $this->currentUser = $current_user; } @@ -59,6 +62,7 @@ public static function createInstance(ContainerInterface $container, EntityTypeI $container->get('entity.manager'), $container->get('current_user'), $container->get('cache.entity'), + $container->get('entity_type.bundle.info'), $container->get('language_manager') ); } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/Element/CreateSampleEntityTest.php b/core/tests/Drupal/KernelTests/Core/Entity/Element/CreateSampleEntityTest.php index 4217d7af94..513266c3d1 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/Element/CreateSampleEntityTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/Element/CreateSampleEntityTest.php @@ -61,14 +61,14 @@ public function testGenerateContentEntity() { foreach ($this->entityTypeManager->getStorage($bundle_type)->loadMultiple() as $bundle) { $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues($bundle->id()); $violations = $entity->validate(); - $this->assertEquals(0, $violations->count()); + $this->assertCount(0, $violations); } } // Create sample entities without bundles. else { - $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues($entity_type_id); + $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues(); $violations = $entity->validate(); - $this->assertEquals(0, $violations->count()); + $this->assertCount(0, $violations); } } }