diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/source/ContentEntity.php b/core/modules/migrate_drupal/src/Plugin/migrate/source/ContentEntity.php index ba84b2c325..98640c83af 100644 --- a/core/modules/migrate_drupal/src/Plugin/migrate/source/ContentEntity.php +++ b/core/modules/migrate_drupal/src/Plugin/migrate/source/ContentEntity.php @@ -6,6 +6,7 @@ use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\ContentEntityTypeInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; +use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -70,6 +71,13 @@ class ContentEntity extends SourcePluginBase implements ContainerFactoryPluginIn protected $entityFieldManager; /** + * The entity type bundle info service. + * + * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface + */ + protected $entityTypeBundleInfo; + + /** * The entity type definition. * * @var \Drupal\Core\Entity\EntityTypeInterface @@ -89,18 +97,24 @@ class ContentEntity extends SourcePluginBase implements ContainerFactoryPluginIn /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) { if (empty($plugin_definition['entity_type'])) { throw new InvalidPluginDefinitionException($plugin_id, 'Missing required "entity_type" definition.'); } $this->entityTypeManager = $entity_type_manager; $this->entityFieldManager = $entity_field_manager; + $this->entityTypeBundleInfo = $entity_type_bundle_info; $this->entityType = $this->entityTypeManager->getDefinition($plugin_definition['entity_type']); if (!$this->entityType instanceof ContentEntityTypeInterface) { throw new InvalidPluginDefinitionException($plugin_id, 'The "content_entity" source plugin only supports content entities.'); } - if (!empty($configuration['bundle']) && !$this->entityType->hasKey('bundle')) { - throw new InvalidPluginDefinitionException($plugin_id, 'A bundle was provided but the entity type is not bundleable.'); + if (!empty($configuration['bundle'])) { + if (!$this->entityType->hasKey('bundle')) { + throw new InvalidPluginDefinitionException($plugin_id, 'A bundle was provided but the entity type is not bundleable.'); + } + if (!in_array($configuration['bundle'], array_keys($this->entityTypeBundleInfo->getBundleInfo($this->entityType->id())))) { + throw new InvalidPluginDefinitionException($plugin_id, 'The provided bundle is not valid for this entity type.'); + } } parent::__construct($configuration + $this->defaultConfiguration, $plugin_id, $plugin_definition, $migration); } @@ -115,7 +129,8 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_definition, $migration, $container->get('entity_type.manager'), - $container->get('entity_field.manager') + $container->get('entity_field.manager'), + $container->get('entity_type.bundle.info') ); } @@ -235,10 +250,9 @@ public function fields() { if (!empty($this->configuration['bundle'])) { $field_definitions += $this->entityFieldManager->getFieldDefinitions($this->entityType->id(), $this->configuration['bundle']); } - $fields = []; - foreach ($field_definitions as $field_name => $definition) { - $fields[$field_name] = (string) $definition->getLabel(); - } + $fields = array_map(function ($definition) { + return (string) $definition->getLabel(); + }, $field_definitions); return $fields; } @@ -272,7 +286,6 @@ protected function getDefinitionFromEntity($key) { /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */ $field_definition = $this->entityFieldManager->getBaseFieldDefinitions($this->entityType->id())[$key]; return [ - 'alias' => 'b', 'type' => $field_definition->getType(), ] + $field_definition->getSettings(); } diff --git a/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/ContentEntityTest.php b/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/ContentEntityTest.php index ddc0a3fa44..ac7e05c14c 100755 --- a/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/ContentEntityTest.php +++ b/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/ContentEntityTest.php @@ -221,6 +221,21 @@ public function testConstructorNotBundable() { } /** + * Tests the constructor for invalid entity bundle. + */ + public function testConstructorInvalidBundle() { + $migration = $this->prophesize(MigrationInterface::class)->reveal(); + $configuration = [ + 'bundle' => 'foo', + ]; + $plugin_definition = [ + 'entity_type' => 'node', + ]; + $this->setExpectedException(InvalidPluginDefinitionException::class, 'The provided bundle is not valid for this entity type.'); + ContentEntity::create($this->container, $configuration, 'content_entity:node', $plugin_definition, $migration); + } + + /** * Tests user source plugin. */ public function testUserSource() {