diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/source/d8/ContentEntity.php b/core/modules/migrate_drupal/src/Plugin/migrate/source/d8/ContentEntity.php index d1622d5..b9826be 100644 --- a/core/modules/migrate_drupal/src/Plugin/migrate/source/d8/ContentEntity.php +++ b/core/modules/migrate_drupal/src/Plugin/migrate/source/d8/ContentEntity.php @@ -3,6 +3,7 @@ namespace Drupal\migrate_drupal\Plugin\migrate\source\d8; use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -100,8 +101,8 @@ public function __toString() { /** * Initializes the iterator with the source data. * - * @return array - * An array of the data for this source. + * @return \Generator + * An generator of the data for this source. */ protected function initializeIterator() { $ids = $this->query()->execute(); @@ -112,36 +113,49 @@ protected function initializeIterator() { * Loads and yields a single entity one at a time. * * @param array $ids - * The entity ids. + * The entity IDs. * * @return \Generator * And iterable of loaded entities. - * - * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException */ protected function yield(array $ids) { + $storage = $this->entityTypeManager->getStorage($this->entityType); foreach ($ids as $id) { - /** @var \Drupal\Core\Entity\EntityInterface $entity */ - $entity = $this->entityTypeManager->getStorage($this->entityType)->load($id); - $value = []; - foreach ($entity->toArray() as $field => $values) { - $value[$field] = $values; + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ + $entity = $storage->load($id); + yield $this->toArray($entity); + foreach ($entity->getTranslationLanguages(FALSE) as $language) { + yield $this->toArray($entity->getTranslation($language->getId())); } - foreach (array_keys($this->getIds()) as $id) { - $reset = reset($value[$id]); - $value[$id] = reset($reset); - } - yield $value; } } /** + * Convert entity to array. + * + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * The entity to convert. + * + * @return array + * The entity converted to array. + */ + protected function toArray(ContentEntityInterface $entity) { + $return = []; + foreach ($entity->toArray() as $field => $values) { + $return[$field] = $values; + } + // The ids must be flat, they cannot be nested for the id map. + foreach (array_keys($this->getIds()) as $id) { + $reset = reset($return[$id]); + $return[$id] = reset($reset); + } + return $return; + } + + /** * Query to retrieve entities. * * @return \Drupal\Core\Entity\Query\QueryInterface - * - * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException - * @throws \Drupal\migrate\MigrateException */ public function query() { $query = $this->entityTypeManager @@ -157,21 +171,21 @@ public function query() { * {@inheritdoc} */ protected function doCount() { - $this->query()->count()->execute(); + return $this->query()->count()->execute(); } /** * {@inheritdoc} */ public function fields() { - $fieldDefinitions = $this->entityFieldManager->getBaseFieldDefinitions($this->entityType); + $field_definitions = $this->entityFieldManager->getBaseFieldDefinitions($this->entityType); $fields = []; - foreach ($fieldDefinitions as $fieldName => $definition) { - $fields[$fieldName] = (string) $definition->getLabel(); + foreach ($field_definitions as $field_name => $definition) { + $fields[$field_name] = (string) $definition->getLabel(); } if (!empty($this->configuration['bundle'])) { - foreach ($this->entityFieldManager->getFieldDefinitions($this->entityType, $this->configuration['bundle']) as $fieldName => $definition) { - $fields[$fieldName] = (string) $definition->getLabel(); + foreach ($this->entityFieldManager->getFieldDefinitions($this->entityType, $this->configuration['bundle']) as $field_name => $definition) { + $fields[$field_name] = (string) $definition->getLabel(); } } return $fields; @@ -181,12 +195,12 @@ public function fields() { * {@inheritdoc} */ public function getIds() { - $entityDefinition = $this->entityTypeManager->getDefinition($this->entityType); - $idKey = $entityDefinition->getKey('id'); + $entity_definition = $this->entityTypeManager->getDefinition($this->entityType); + $idKey = $entity_definition->getKey('id'); $ids[$idKey] = $this->getDefinitionFromEntity($idKey); - if ($entityDefinition->isTranslatable()) { - $langcodeKey = $entityDefinition->getKey('langcode'); - $ids[$langcodeKey] = $this->getDefinitionFromEntity($langcodeKey); + if ($entity_definition->isTranslatable()) { + $langcode_key = $entity_definition->getKey('langcode'); + $ids[$langcode_key] = $this->getDefinitionFromEntity($langcode_key); } return $ids; } @@ -205,13 +219,12 @@ public function getIds() { * @see \Drupal\migrate\Plugin\migrate\destination\EntityContentBase::getDefinitionFromEntity() */ protected function getDefinitionFromEntity($key) { - /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $fieldDefinitions */ - $fieldDefinitions = $this->entityFieldManager->getBaseFieldDefinitions($this->entityType); - $fieldDefinition = $fieldDefinitions[$key]; + /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */ + $field_definition = $this->entityFieldManager->getBaseFieldDefinitions($this->entityType)[$key]; return [ 'alias' => 'b', - 'type' => $fieldDefinition->getType(), - ] + $fieldDefinition->getSettings(); + 'type' => $field_definition->getType(), + ] + $field_definition->getSettings(); } } diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/source/d8/ContentEntityDeriver.php b/core/modules/migrate_drupal/src/Plugin/migrate/source/d8/ContentEntityDeriver.php index 3bb3b34..33052be 100644 --- a/core/modules/migrate_drupal/src/Plugin/migrate/source/d8/ContentEntityDeriver.php +++ b/core/modules/migrate_drupal/src/Plugin/migrate/source/d8/ContentEntityDeriver.php @@ -11,13 +11,6 @@ class ContentEntityDeriver extends DeriverBase implements ContainerDeriverInterface { /** - * The base plugin ID this derivative is for. - * - * @var string - */ - protected $basePluginId; - - /** * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface @@ -33,7 +26,6 @@ class ContentEntityDeriver extends DeriverBase implements ContainerDeriverInterf * The entity type manager. */ public function __construct($base_plugin_id, EntityTypeManagerInterface $entityTypeManager) { - $this->basePluginId = $base_plugin_id; $this->entityTypeManager = $entityTypeManager; } @@ -53,11 +45,12 @@ public static function create(ContainerInterface $container, $base_plugin_id) { */ public function getDerivativeDefinitions($base_plugin_definition) { $this->derivatives = []; - foreach ($this->entityTypeManager->getDefinitions() as $definition) { + foreach ($this->entityTypeManager->getDefinitions() as $id => $definition) { // The source plugin only supports entities that are a content entity. if ($definition instanceof ContentEntityTypeInterface) { - $this->derivatives[$definition->id()] = $base_plugin_definition; - $this->derivatives[$definition->id()]['entity_type'] = $definition->id(); + $this->derivatives[$id] = $base_plugin_definition; + // Provide this so the source can be used separate from a deriver. + $this->derivatives[$id]['entity_type'] = $id; } } return parent::getDerivativeDefinitions($base_plugin_definition); diff --git a/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/d8/ContentEntityTest.php b/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/d8/ContentEntityTest.php index 7a6f121..8ace91c 100755 --- a/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/d8/ContentEntityTest.php +++ b/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/d8/ContentEntityTest.php @@ -170,6 +170,7 @@ protected function setUp() { $node->save(); $node->addTranslation('fr', [ 'title' => 'Pommes', + $this->fieldName => $term->id(), ])->save(); @@ -239,8 +240,9 @@ public function testFileSource() { */ public function testNodeSource() { $nodeSource = $this->sourcePluginManager->createInstance('entity:node', ['bundle' => $this->bundle], $this->migration); - $this->migration->expects(self::once())->method('getSourcePlugin')->willReturn($nodeSource); - $this->migration->expects(self::once())->method('getDestinationIds')->willReturn([1]); + $this->migration->expects(self::exactly(2))->method('getSourcePlugin')->willReturn($nodeSource); + $this->migration->expects(self::at(0))->method('getDestinationIds')->willReturn([1, 'en']); + $this->migration->expects(self::at(1))->method('getDestinationIds')->willReturn([2, 'fr']); $ids = $nodeSource->getIds(); $this->assertArrayHasKey('langcode', $ids); $this->assertArrayHasKey('nid', $ids); @@ -254,8 +256,19 @@ public function testNodeSource() { $values = $nodeSource->current()->getSource(); $this->assertEquals($this->bundle, $values['type'][0]['target_id']); $this->assertEquals(1, $values['nid']); + $this->assertEquals('en', $values['langcode']); $this->assertEquals(1, $values['status'][0]['value']); $this->assertEquals('Apples', $values['title'][0]['value']); + $this->assertEquals(1, $values['default_langcode'][0]['value']); + $this->assertEquals(1, $values['field_entity_reference'][0]['target_id']); + $nodeSource->next(); + $values = $nodeSource->current()->getSource(); + $this->assertEquals($this->bundle, $values['type'][0]['target_id']); + $this->assertEquals(1, $values['nid']); + $this->assertEquals('fr', $values['langcode']); + $this->assertEquals(1, $values['status'][0]['value']); + $this->assertEquals('Pommes', $values['title'][0]['value']); + $this->assertEquals(0, $values['default_langcode'][0]['value']); $this->assertEquals(1, $values['field_entity_reference'][0]['target_id']); }