diff --git a/core/core.services.yml b/core/core.services.yml index 2ddb545a49..76088786cd 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -544,9 +544,6 @@ services: parent: container.trait tags: - { name: plugin_manager_cache_clear } - entity.generator: - class: Drupal\Core\Entity\EntityGenerator - arguments: ['@entity_type.manager'] entity_type.repository: class: Drupal\Core\Entity\EntityTypeRepository arguments: ['@entity_type.manager'] diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index 9d6b8d64f5..0ce0f98a31 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -90,6 +90,31 @@ protected function doCreate(array $values) { } /** + * {@inheritdoc} + */ + public function createWithSampleValues($bundle, array $values = []) { + $bundle_key = $this->entityType->getKey('bundle'); + $values[$bundle_key] = $bundle; + // Bundle is already set, and ID and revision should never be set. + $forbidden_keys = [ + $this->entityType->getKey('id'), + $this->entityType->getKey('revision'), + $bundle_key + ]; + // 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) { + if (!in_array($field_name, $forbidden_keys)) { + $entity->get($field_name)->generateSampleItems(); + } + } + return $entity; + } + + /** * Initializes field values. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageInterface.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageInterface.php index 47e058d8e1..4e7ad6134e 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageInterface.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageInterface.php @@ -23,4 +23,21 @@ */ public function createTranslation(ContentEntityInterface $entity, $langcode, array $values = []); + + /** + * Generates an entity with sample field values. + * + * @param string $bundle + * The entity bundle. + * @param array $values + * (optional) Any default values to use during generation. + * + * @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. + */ + public function createWithSampleValues($bundle, array $values = []); + } diff --git a/core/lib/Drupal/Core/Entity/EntityGenerator.php b/core/lib/Drupal/Core/Entity/EntityGenerator.php deleted file mode 100644 index 7c2bfd9aec..0000000000 --- a/core/lib/Drupal/Core/Entity/EntityGenerator.php +++ /dev/null @@ -1,96 +0,0 @@ -entityTypeManager = $entity_type_manager; - } - - /** - * {@inheritdoc} - */ - public function generateFieldableEntity($entity_type_id, $bundle, array $values = []) { - $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); - if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) { - throw new EntityGeneratorMismatchException("Provided entity type \"%s\" is not fieldable. Use \Drupal\Core\Entity\EntityGenerator::generateConfigurationEntity() instead", $entity_type_id); - } - $bundle_key = $entity_type->getKey('bundle'); - $values[$bundle_key] = $bundle; - - // Allow custom entity generation per entity type. - if ($entity_type->hasHandlerClass('generator')) { - /** @var \Drupal\Core\Entity\GeneratorHandlerInterface $generator */ - $generator = $this->entityTypeManager->getHandler($entity_type_id, 'generator'); - $entity = $generator->generate($values); - } - else { - // Bundle is already set, and ID and revision should never be set. - $forbidden_keys = [ - $entity_type->getKey('id'), - $entity_type->getKey('revision'), - $bundle_key - ]; - // 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->entityTypeManager->getStorage($entity_type_id)->create($values); - foreach ($entity as $field_name => $value) { - if (!in_array($field_name, $forbidden_keys)) { - $entity->get($field_name)->generateSampleItems(); - } - } - } - return $entity; - } - - /** - * {@inheritdoc} - */ - public function generateConfigurationEntity($entity_type_id, array $values = []) { - $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); - if (!$entity_type->entityClassImplements(ConfigEntityInterface::class)) { - throw new EntityGeneratorMismatchException("Provided entity type \"%s\" is not a configuration entity. Use \Drupal\Core\Entity\EntityGenerator::generateFieldableEntity() instead", $entity_type_id); - } - // Allow custom entity generation per entity type. - /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ - if ($entity_type->hasHandlerClass('generator')) { - /** @var \Drupal\Core\Entity\GeneratorHandlerInterface $generator */ - $generator = $this->entityTypeManager->getHandler($entity_type_id, 'generator'); - return $generator->generate($values); - } - else { - throw new MissingEntityGeneratorHandlerException($entity_type_id); - } - } - -} diff --git a/core/lib/Drupal/Core/Entity/EntityGeneratorInterface.php b/core/lib/Drupal/Core/Entity/EntityGeneratorInterface.php deleted file mode 100644 index 2b199a578c..0000000000 --- a/core/lib/Drupal/Core/Entity/EntityGeneratorInterface.php +++ /dev/null @@ -1,50 +0,0 @@ - $random->name(), - 'name' => $random->string(), - 'description' => $random->paragraphs(1), - 'help' => $random->paragraphs(2), - 'new_revision' => $this->getBoolean(), - 'preview_mode' => $this->getPreviewMode(), - 'display_submitted' => $this->getBoolean(), - ]; - $values = array_merge($generated_values, $values); - return NodeType::create($values); - } - - /** - * Generates a random preview mode for node types. - * - * @return integer - * The preview mode to use. - */ - protected function getPreviewMode() { - $options = [ - DRUPAL_DISABLED, - DRUPAL_OPTIONAL, - DRUPAL_REQUIRED, - ]; - $key = mt_rand(0, 2); - return $options[$key]; - } - - /** - * Generates a random boolean value. - * - * @return bool - * The boolean value. - */ - protected function getBoolean() { - return (bool) mt_rand(0, 1); - } - -} diff --git a/core/tests/Drupal/KernelTests/Core/Entity/Element/EntityGeneratorTest.php b/core/tests/Drupal/KernelTests/Core/Entity/Element/EntityGeneratorTest.php index 7ca55c54fa..2b060f3f7a 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/Element/EntityGeneratorTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/Element/EntityGeneratorTest.php @@ -3,8 +3,6 @@ namespace Drupal\KernelTests\Core\Entity\Element; use Drupal\Core\Entity\ContentEntityType; -use Drupal\Core\Entity\Exception\EntityGeneratorMismatchException; -use Drupal\Core\Entity\Exception\MissingEntityGeneratorHandlerException; use Drupal\KernelTests\KernelTestBase; use Drupal\node\Entity\NodeType; use Drupal\taxonomy\Entity\Vocabulary; @@ -12,7 +10,7 @@ /** * Tests the EntityGenerator API. * - * @coversDefaultClass \Drupal\Core\Entity\EntityGenerator + * @coversDefaultClass \Drupal\Core\Entity\ContentEntityStorageBase * @group Entity */ class EntityGeneratorTest extends KernelTestBase { @@ -25,13 +23,6 @@ class EntityGeneratorTest extends KernelTestBase { protected $entityTypeManager; /** - * The entity generator. - * - * @var \Drupal\Core\Entity\EntityGenerator - */ - protected $entityGenerator; - - /** * {@inheritdoc} */ public static $modules = ['system', 'field', 'filter', 'text', 'file', 'user', 'node', 'comment', 'taxonomy']; @@ -52,7 +43,6 @@ protected function setUp() { $this->installEntitySchema('taxonomy_vocabulary'); $this->installEntitySchema('taxonomy_term'); $this->entityTypeManager = $this->container->get('entity_type.manager'); - $this->entityGenerator = $this->container->get('entity.generator'); NodeType::create(['type' => 'article', 'name' => 'Article'])->save(); NodeType::create(['type' => 'page', 'name' => 'Page'])->save(); Vocabulary::create(['name' => 'Tags', 'vid' => 'tags'])->save(); @@ -61,7 +51,7 @@ protected function setUp() { /** * Tests generation of all enabled content entity types. * - * * @covers ::generateFieldableEntity + * * @covers ::createWithSampleValues */ public function testGenerateContentEntity() { foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $definition) { @@ -69,60 +59,19 @@ public function testGenerateContentEntity() { // Generate entities with bundles. if ($bundle_type = $definition->getBundleEntityType()) { foreach ($this->entityTypeManager->getStorage($bundle_type)->loadMultiple() as $bundle) { - $entity = $this->entityGenerator->generateFieldableEntity($entity_type_id, $bundle->id()); + $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues($bundle->id()); $violations = $entity->validate(); $this->assertEquals(0, $violations->count()); } } // Generate entities without bundles. else { - $entity = $this->entityGenerator->generateFieldableEntity($entity_type_id, $entity_type_id); + $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues($entity_type_id); $violations = $entity->validate(); $this->assertEquals(0, $violations->count()); } } } - $this->setExpectedException(EntityGeneratorMismatchException::class, "Provided entity type \"node_type\" is not fieldable. Use \Drupal\Core\Entity\EntityGenerator::generateConfigurationEntity() instead"); - $this->entityGenerator->generateFieldableEntity('node_type', 'page'); - } - - /** - * Tests generation of configuration entity types. - * - * @covers ::generateConfigurationEntity - */ - public function testGenerateConfigEntity() { - /** @var \Drupal\node\Entity\NodeType $node_type */ - $node_type = $this->entityGenerator->generateConfigurationEntity('node_type'); - $this->assertTrue(is_string($node_type->id())); - $this->assertTrue(is_string($node_type->label())); - $this->assertTrue(is_string($node_type->getDescription())); - $this->assertTrue(is_string($node_type->getHelp())); - $this->assertTrue(is_bool($node_type->isNewRevision())); - $this->assertTrue(in_array($node_type->getPreviewMode(), [DRUPAL_DISABLED, DRUPAL_OPTIONAL, DRUPAL_REQUIRED])); - $this->assertTrue(is_bool($node_type->displaySubmitted())); - // Save it and make certain we don't throw an exception. - $this->assertTrue(is_int($node_type->save())); - } - - /** - * Tests mismatched entity generator exception for config entities. - * - * @covers ::generateConfigurationEntity - */ - public function testGeneratorMismatchException() { - $this->setExpectedException(EntityGeneratorMismatchException::class, "Provided entity type \"node\" is not a configuration entity. Use \Drupal\Core\Entity\EntityGenerator::generateFieldableEntity() instead"); - $this->entityGenerator->generateConfigurationEntity('node'); - } - - /** - * Tests missing generator handler entity. - * - * @covers ::generateConfigurationEntity - */ - public function testMissingGeneratorHandlerException() { - $this->setExpectedException(MissingEntityGeneratorHandlerException::class); - $this->entityGenerator->generateConfigurationEntity('taxonomy_vocabulary'); } }