diff --git a/core/modules/migrate/src/Plugin/migrate/process/Iterator.php b/core/modules/migrate/src/Plugin/migrate/process/Iterator.php index 7affba9..4b8ad92 100644 --- a/core/modules/migrate/src/Plugin/migrate/process/Iterator.php +++ b/core/modules/migrate/src/Plugin/migrate/process/Iterator.php @@ -23,14 +23,16 @@ class Iterator extends ProcessPluginBase { */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { $return = array(); - foreach ($value as $key => $new_value) { - $new_row = new Row($new_value, array()); - $migrate_executable->processRow($new_row, $this->configuration['process']); - $destination = $new_row->getDestination(); - if (array_key_exists('key', $this->configuration)) { - $key = $this->transformKey($key, $migrate_executable, $new_row); + if (is_array($value)) { + foreach ($value as $key => $new_value) { + $new_row = new Row($new_value, array()); + $migrate_executable->processRow($new_row, $this->configuration['process']); + $destination = $new_row->getDestination(); + if (array_key_exists('key', $this->configuration)) { + $key = $this->transformKey($key, $migrate_executable, $new_row); + } + $return[$key] = $destination; } - $return[$key] = $destination; } return $return; } diff --git a/core/modules/taxonomy/migration_templates/d6_taxonomy_term.yml b/core/modules/taxonomy/migration_templates/d6_taxonomy_term.yml index 0c41c6d..13d793f 100644 --- a/core/modules/taxonomy/migration_templates/d6_taxonomy_term.yml +++ b/core/modules/taxonomy/migration_templates/d6_taxonomy_term.yml @@ -3,7 +3,7 @@ label: Taxonomy terms migration_tags: - Drupal 6 source: - plugin: taxonomy_term + plugin: d6_taxonomy_term process: tid: tid vid: diff --git a/core/modules/taxonomy/migration_templates/d7_taxonomy_term.yml b/core/modules/taxonomy/migration_templates/d7_taxonomy_term.yml index 7546660..0b40a32 100644 --- a/core/modules/taxonomy/migration_templates/d7_taxonomy_term.yml +++ b/core/modules/taxonomy/migration_templates/d7_taxonomy_term.yml @@ -3,7 +3,7 @@ label: Taxonomy terms migration_tags: - Drupal 7 source: - plugin: taxonomy_term + plugin: d7_taxonomy_term process: tid: tid vid: diff --git a/core/modules/taxonomy/src/Plugin/migrate/source/Term.php b/core/modules/taxonomy/src/Plugin/migrate/source/Term.php deleted file mode 100644 index 0b2a5c3..0000000 --- a/core/modules/taxonomy/src/Plugin/migrate/source/Term.php +++ /dev/null @@ -1,100 +0,0 @@ -getModuleSchemaVersion('taxonomy') >= 7000) { - $this->termDataTable = 'taxonomy_term_data'; - $this->termHierarchyTable = 'taxonomy_term_hierarchy'; - } - else { - $this->termDataTable = 'term_data'; - $this->termHierarchyTable = 'term_hierarchy'; - } - - $query = $this->select($this->termDataTable, 'td') - ->fields('td') - ->distinct() - ->orderBy('td.tid'); - - if (isset($this->configuration['vocabulary'])) { - $query->condition('td.vid', $this->configuration['vocabulary'], 'IN'); - } - - return $query; - } - - /** - * {@inheritdoc} - */ - public function fields() { - $fields = array( - 'tid' => $this->t('The term ID.'), - 'vid' => $this->t('Existing term VID'), - 'name' => $this->t('The name of the term.'), - 'description' => $this->t('The term description.'), - 'weight' => $this->t('Weight'), - 'parent' => $this->t("The Drupal term IDs of the term's parents."), - ); - if ($this->getModuleSchemaVersion('taxonomy') >= 7000) { - $fields['format'] = $this->t('Format of the term description.'); - } - return $fields; - } - - /** - * {@inheritdoc} - */ - public function prepareRow(Row $row) { - // Find parents for this row. - $parents = $this->select($this->termHierarchyTable, 'th') - ->fields('th', array('parent', 'tid')) - ->condition('tid', $row->getSourceProperty('tid')) - ->execute() - ->fetchCol(); - $row->setSourceProperty('parent', $parents); - - return parent::prepareRow($row); - } - - /** - * {@inheritdoc} - */ - public function getIds() { - $ids['tid']['type'] = 'integer'; - return $ids; - } - -} diff --git a/core/modules/taxonomy/src/Plugin/migrate/source/d6/Term.php b/core/modules/taxonomy/src/Plugin/migrate/source/d6/Term.php new file mode 100644 index 0000000..10017da --- /dev/null +++ b/core/modules/taxonomy/src/Plugin/migrate/source/d6/Term.php @@ -0,0 +1,91 @@ +termDataTable = 'term_data'; + $this->termHierarchyTable = 'term_hierarchy'; + + $query = $this->select($this->termDataTable, 'td') + ->fields('td') + ->distinct() + ->orderBy('tid'); + + if (isset($this->configuration['vocabulary'])) { + $query->condition('vid', $this->configuration['vocabulary'], 'IN'); + } + + return $query; + } + + /** + * {@inheritdoc} + */ + public function fields() { + $fields = array( + 'tid' => $this->t('The term ID.'), + 'vid' => $this->t('Existing term VID'), + 'name' => $this->t('The name of the term.'), + 'description' => $this->t('The term description.'), + 'weight' => $this->t('Weight'), + 'parent' => $this->t("The Drupal term IDs of the term's parents."), + ); + return $fields; + } + + /** + * {@inheritdoc} + */ + public function prepareRow(Row $row) { + // Find parents for this row. + $parents = $this->select($this->termHierarchyTable, 'th') + ->fields('th', array('parent', 'tid')) + ->condition('tid', $row->getSourceProperty('tid')) + ->execute() + ->fetchCol(); + $row->setSourceProperty('parent', $parents); + + return parent::prepareRow($row); + } + + /** + * {@inheritdoc} + */ + public function getIds() { + $ids['tid']['type'] = 'integer'; + return $ids; + } + +} diff --git a/core/modules/taxonomy/src/Plugin/migrate/source/d7/Term.php b/core/modules/taxonomy/src/Plugin/migrate/source/d7/Term.php new file mode 100644 index 0000000..635d555 --- /dev/null +++ b/core/modules/taxonomy/src/Plugin/migrate/source/d7/Term.php @@ -0,0 +1,102 @@ +termDataTable = 'taxonomy_term_data'; + $this->termHierarchyTable = 'taxonomy_term_hierarchy'; + $this->vocabularyTable = 'taxonomy_vocabulary'; + + $query = $this->select($this->termDataTable, 'td') + ->fields('td') + ->distinct() + ->orderBy('tid'); + $query->leftJoin($this->vocabularyTable, 'tv', 'td.vid = tv.vid'); + $query->addField('tv', 'machine_name'); + + if (isset($this->configuration['vocabulary'])) { + $query->condition('td.vid', $this->configuration['vocabulary'], 'IN'); + } + + return $query; + } + + /** + * {@inheritdoc} + */ + public function fields() { + $fields = array( + 'tid' => $this->t('The term ID.'), + 'vid' => $this->t('Existing term VID'), + 'machine_name' => $this->t('Vocabulary machine name'), + 'name' => $this->t('The name of the term.'), + 'description' => $this->t('The term description.'), + 'weight' => $this->t('Weight'), + 'parent' => $this->t("The Drupal term IDs of the term's parents."), + 'format' => $this->t("Format of the term description."), + ); + return $fields; + } + + /** + * {@inheritdoc} + */ + public function prepareRow(Row $row) { + // Get Field API field values. + foreach (array_keys($this->getFields('taxonomy_term', $row->getSourceProperty('machine_name'))) as $field) { + $tid = $row->getSourceProperty('tid'); + $row->setSourceProperty($field, $this->getFieldValues('taxonomy_term', $field, $tid)); + } + + // Find parents for this row. + $parents = $this->select($this->termHierarchyTable, 'th') + ->fields('th', array('parent', 'tid')) + ->condition('tid', $row->getSourceProperty('tid')) + ->execute() + ->fetchCol(); + $row->setSourceProperty('parent', $parents); + + return parent::prepareRow($row); + } + + /** + * {@inheritdoc} + */ + public function getIds() { + $ids['tid']['type'] = 'integer'; + return $ids; + } + +} diff --git a/core/modules/taxonomy/tests/src/Unit/Migrate/TermSourceWithVocabularyFilterTest.php b/core/modules/taxonomy/tests/src/Unit/Migrate/TermSourceWithVocabularyFilterTest.php deleted file mode 100644 index 67c46cb..0000000 --- a/core/modules/taxonomy/tests/src/Unit/Migrate/TermSourceWithVocabularyFilterTest.php +++ /dev/null @@ -1,25 +0,0 @@ -migrationConfiguration['source']['vocabulary'] = array(5); - parent::setUp(); - $this->expectedResults = array_values(array_filter($this->expectedResults, function($result) { - return $result['vid'] == 5; - })); - // We know there are two rows with vid == 5. - $this->expectedCount = 2; - } - -} diff --git a/core/modules/taxonomy/tests/src/Unit/Migrate/TermTest.php b/core/modules/taxonomy/tests/src/Unit/Migrate/TermTest.php deleted file mode 100644 index ad2706b..0000000 --- a/core/modules/taxonomy/tests/src/Unit/Migrate/TermTest.php +++ /dev/null @@ -1,12 +0,0 @@ - 'test', - 'highWaterProperty' => array('field' => 'test'), - 'source' => array( - 'plugin' => 'd6_taxonomy_term', - ), - ); - - protected $expectedResults = array( - array( - 'tid' => 1, - 'vid' => 5, - 'name' => 'name value 1', - 'description' => 'description value 1', - 'weight' => 0, - 'parent' => array(0), - ), - array( - 'tid' => 2, - 'vid' => 6, - 'name' => 'name value 2', - 'description' => 'description value 2', - 'weight' => 0, - 'parent' => array(0), - ), - array( - 'tid' => 3, - 'vid' => 6, - 'name' => 'name value 3', - 'description' => 'description value 3', - 'weight' => 0, - 'parent' => array(0), - ), - array( - 'tid' => 4, - 'vid' => 5, - 'name' => 'name value 4', - 'description' => 'description value 4', - 'weight' => 1, - 'parent' => array(1), - ), - array( - 'tid' => 5, - 'vid' => 6, - 'name' => 'name value 5', - 'description' => 'description value 5', - 'weight' => 1, - 'parent' => array(2), - ), - array( - 'tid' => 6, - 'vid' => 6, - 'name' => 'name value 6', - 'description' => 'description value 6', - 'weight' => 0, - 'parent' => array(3, 2), - ), - ); - - /** - * {@inheritdoc} - */ - protected function setUp() { - foreach ($this->expectedResults as $k => $row) { - foreach ($row['parent'] as $parent) { - $this->databaseContents['term_hierarchy'][] = array( - 'tid' => $row['tid'], - 'parent' => $parent, - ); - } - unset($row['parent']); - $this->databaseContents['term_data'][$k] = $row; - } - parent::setUp(); - } - -} diff --git a/core/modules/taxonomy/tests/src/Unit/Migrate/d6/TermSourceWithVocabularyFilterTest.php b/core/modules/taxonomy/tests/src/Unit/Migrate/d6/TermSourceWithVocabularyFilterTest.php new file mode 100644 index 0000000..c0dcd50 --- /dev/null +++ b/core/modules/taxonomy/tests/src/Unit/Migrate/d6/TermSourceWithVocabularyFilterTest.php @@ -0,0 +1,25 @@ +migrationConfiguration['source']['vocabulary'] = array(5); + parent::setUp(); + $this->expectedResults = array_values(array_filter($this->expectedResults, function($result) { + return $result['vid'] == 5; + })); + // We know there are two rows with vid == 5. + $this->expectedCount = 2; + } + +} diff --git a/core/modules/taxonomy/tests/src/Unit/Migrate/d6/TermTest.php b/core/modules/taxonomy/tests/src/Unit/Migrate/d6/TermTest.php new file mode 100644 index 0000000..a9e987c --- /dev/null +++ b/core/modules/taxonomy/tests/src/Unit/Migrate/d6/TermTest.php @@ -0,0 +1,12 @@ + 'test', + 'highWaterProperty' => array('field' => 'test'), + 'source' => array( + 'plugin' => 'd6_taxonomy_term', + ), + ); + + protected $expectedResults = array( + array( + 'tid' => 1, + 'vid' => 5, + 'name' => 'name value 1', + 'description' => 'description value 1', + 'weight' => 0, + 'parent' => array(0), + ), + array( + 'tid' => 2, + 'vid' => 6, + 'name' => 'name value 2', + 'description' => 'description value 2', + 'weight' => 0, + 'parent' => array(0), + ), + array( + 'tid' => 3, + 'vid' => 6, + 'name' => 'name value 3', + 'description' => 'description value 3', + 'weight' => 0, + 'parent' => array(0), + ), + array( + 'tid' => 4, + 'vid' => 5, + 'name' => 'name value 4', + 'description' => 'description value 4', + 'weight' => 1, + 'parent' => array(1), + ), + array( + 'tid' => 5, + 'vid' => 6, + 'name' => 'name value 5', + 'description' => 'description value 5', + 'weight' => 1, + 'parent' => array(2), + ), + array( + 'tid' => 6, + 'vid' => 6, + 'name' => 'name value 6', + 'description' => 'description value 6', + 'weight' => 0, + 'parent' => array(3, 2), + ), + ); + + /** + * {@inheritdoc} + */ + protected function setUp() { + foreach ($this->expectedResults as $k => $row) { + foreach ($row['parent'] as $parent) { + $this->databaseContents['term_hierarchy'][] = array( + 'tid' => $row['tid'], + 'parent' => $parent, + ); + } + unset($row['parent']); + $this->databaseContents['term_data'][$k] = $row; + } + parent::setUp(); + } + +} diff --git a/core/modules/taxonomy/tests/src/Unit/Migrate/d7/TermSourceWithVocabularyFilterTest.php b/core/modules/taxonomy/tests/src/Unit/Migrate/d7/TermSourceWithVocabularyFilterTest.php new file mode 100644 index 0000000..251746c --- /dev/null +++ b/core/modules/taxonomy/tests/src/Unit/Migrate/d7/TermSourceWithVocabularyFilterTest.php @@ -0,0 +1,25 @@ +migrationConfiguration['source']['vocabulary'] = array(5); + parent::setUp(); + $this->expectedResults = array_values(array_filter($this->expectedResults, function($result) { + return $result['vid'] == 5; + })); + // We know there are two rows with vid == 5. + $this->expectedCount = 2; + } + +} diff --git a/core/modules/taxonomy/tests/src/Unit/Migrate/d7/TermTest.php b/core/modules/taxonomy/tests/src/Unit/Migrate/d7/TermTest.php new file mode 100644 index 0000000..25f9335 --- /dev/null +++ b/core/modules/taxonomy/tests/src/Unit/Migrate/d7/TermTest.php @@ -0,0 +1,12 @@ + 'test', + 'highWaterProperty' => array('field' => 'test'), + 'source' => array( + 'plugin' => 'd7_taxonomy_term', + ), + ); + + protected $expectedResults = array( + array( + 'tid' => 1, + 'vid' => 5, + 'machine_name' => 'tags', + 'name' => 'name value 1', + 'description' => 'description value 1', + 'weight' => 0, + 'parent' => array(0), + ), + array( + 'tid' => 2, + 'vid' => 6, + 'machine_name' => 'categories', + 'name' => 'name value 2', + 'description' => 'description value 2', + 'weight' => 0, + 'parent' => array(0), + ), + array( + 'tid' => 3, + 'vid' => 6, + 'machine_name' => 'categories', + 'name' => 'name value 3', + 'description' => 'description value 3', + 'weight' => 0, + 'parent' => array(0), + ), + array( + 'tid' => 4, + 'vid' => 5, + 'machine_name' => 'tags', + 'name' => 'name value 4', + 'description' => 'description value 4', + 'weight' => 1, + 'parent' => array(1), + ), + array( + 'tid' => 5, + 'vid' => 6, + 'machine_name' => 'categories', + 'name' => 'name value 5', + 'description' => 'description value 5', + 'weight' => 1, + 'parent' => array(2), + ), + array( + 'tid' => 6, + 'vid' => 6, + 'machine_name' => 'categories', + 'name' => 'name value 6', + 'description' => 'description value 6', + 'weight' => 0, + 'parent' => array(3, 2), + ), + ); + + /** + * {@inheritdoc} + */ + protected function setUp() { + foreach ($this->expectedResults as $k => $row) { + foreach ($row['parent'] as $parent) { + $this->databaseContents['taxonomy_term_hierarchy'][] = [ + 'tid' => $row['tid'], + 'parent' => $parent, + ]; + } + unset($row['parent']); + $this->databaseContents['taxonomy_term_data'][$k] = $row; + $this->databaseContents['taxonomy_vocabulary'][$row['vid']] = [ + 'vid' => $row['vid'], + 'machine_name' => $row['machine_name'], + ]; + $this->databaseContents['field_config_instance'][$row['machine_name']] = [ + 'field_name' => 'field_term_field', + 'entity_type' => 'taxonomy_term', + 'bundle' => $row['machine_name'], + 'deleted' => 0, + ]; + $this->databaseContents['field_data_field_term_field'][$row['machine_name']] = [ + 'entity_type' => 'taxonomy_term', + 'bundle' => $row['machine_name'], + 'deleted' => 0, + 'entity_id' => 1, + 'delta' => 0, + ]; + } + + parent::setUp(); + } + +}