From 43d32db8264676c00800acb83110b5e13691ac18 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Sun, 9 Dec 2018 11:30:32 +0100 Subject: [PATCH] thesaurus and tags i18n working --- ...e_plus.migration.d7_taxonomy_term_tags.yml | 6 +- ...s.migration.d7_taxonomy_term_tags_i18n.yml | 39 +++++++ ...s.migration.d7_taxonomy_term_thesaurus.yml | 3 - ...ration.d7_taxonomy_term_thesaurus_i18n.yml | 6 +- .../migrate/source/D7TaxonomyTermTags.php | 47 ++++---- .../migrate/source/D7TaxonomyTermTagsI18n.php | 100 ++++++++++++++++++ .../source/D7TaxonomyTermThesaurusI18n.php | 5 +- 7 files changed, 170 insertions(+), 36 deletions(-) create mode 100644 modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_tags_i18n.yml create mode 100644 modules/custom/materio_migrate/src/Plugin/migrate/source/D7TaxonomyTermTagsI18n.php diff --git a/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_tags.yml b/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_tags.yml index 73510296..035dfa1b 100644 --- a/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_tags.yml +++ b/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_tags.yml @@ -39,10 +39,10 @@ process: plugin: default_value default_value: 0 source: '@parent_id' - forum_container: is_container changed: timestamp #synonyms - field_synonyms: synonyms + field_synonyms: synonyms_synonym - # TODO translation + # used on advanced search + # field_used_on_advanced_search: field_used_on_advanced_search diff --git a/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_tags_i18n.yml b/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_tags_i18n.yml new file mode 100644 index 00000000..034cbae9 --- /dev/null +++ b/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_tags_i18n.yml @@ -0,0 +1,39 @@ +id: d7_taxonomy_term_tags_i18n +label: Taxonomy terms Tags i18n +migration_group: d7_materio +audit: true +migration_tags: + - Drupal 7 + - Content + - Materio +deriver: Drupal\taxonomy\Plugin\migrate\D7TaxonomyTermDeriver + +source: + plugin: d7_taxonomy_term_tags_i18n + bundle: tag_libres + translations: true + +destination: + plugin: entity:taxonomy_term + translations: true + +process: + tid: + plugin: migration_lookup + migration: d7_taxonomy_term_tags + source: tid + vid: + plugin: default_value + default_value: tags + langcode: language + + name: name + + 'description/value': description + 'description/format': format + + field_synonyms: synonyms_synonym + +migration_dependencies: + required: + - d7_taxonomy_term_tags diff --git a/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_thesaurus.yml b/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_thesaurus.yml index ed47417e..7b8e82c4 100644 --- a/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_thesaurus.yml +++ b/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_thesaurus.yml @@ -22,7 +22,6 @@ process: vid: plugin: default_value default_value: thesaurus - # langcode: language name: name 'description/value': description 'description/format': format @@ -47,5 +46,3 @@ process: # used on advanced search field_used_on_advanced_search: field_used_on_advanced_search - - # TODO translation diff --git a/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_thesaurus_i18n.yml b/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_thesaurus_i18n.yml index cd826f1d..94f79dc9 100644 --- a/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_thesaurus_i18n.yml +++ b/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_taxonomy_term_thesaurus_i18n.yml @@ -18,12 +18,10 @@ destination: translations: true process: - # If you are using this file to build a custom migration consider removing - # the tid field to allow incremental migrations. tid: - plugin: migration - source: tid + plugin: migration_lookup migration: d7_taxonomy_term_thesaurus + source: tid vid: plugin: default_value default_value: thesaurus diff --git a/modules/custom/materio_migrate/src/Plugin/migrate/source/D7TaxonomyTermTags.php b/modules/custom/materio_migrate/src/Plugin/migrate/source/D7TaxonomyTermTags.php index c7d25ed1..7cb26ff4 100644 --- a/modules/custom/materio_migrate/src/Plugin/migrate/source/D7TaxonomyTermTags.php +++ b/modules/custom/materio_migrate/src/Plugin/migrate/source/D7TaxonomyTermTags.php @@ -21,8 +21,13 @@ class D7TaxonomyTermTags extends FieldableEntity { public function query() { $query = $this->select('taxonomy_term_data', 'td') ->fields('td') - ->distinct() + ->condition('et.entity_type', 'taxonomy_term') + ->condition('et.source', '') ->orderBy('tid'); + + $query->innerJoin('entity_translation', 'et', 'et.entity_id = td.tid'); + $query->fields('et'); + $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid'); $query->addField('tv', 'machine_name'); @@ -47,6 +52,10 @@ class D7TaxonomyTermTags extends FieldableEntity { 'parent' => $this->t("The Drupal term IDs of the term's parents."), 'format' => $this->t("Format of the term description."), 'synonyms' => $this->t("Term's Synonyms (field : synonyms_synonym)"), + 'displayed_on_advanced_search' => $this->t("field_used_on_advanced_search"), + 'revision_id' => $this->t('Revision ID'), + 'language' => $this->t('Term translation language'), + 'source' => $this->t('Term translation source language'), ]; return $fields; } @@ -56,12 +65,19 @@ class D7TaxonomyTermTags extends FieldableEntity { * {@inheritdoc} */ public function prepareRow(Row $row) { - print("\n\n".'-- prepareRow -- '.$row->getSourceProperty('tid')." -- ".$row->getSourceProperty('name')."\n"); + $language = $row->getSourceProperty('language'); + drush_print('-- '.$language.' -- '.$row->getSourceProperty('tid')." -- ".$row->getSourceProperty('name')." -- ".$row->getSourceProperty('weight')); + + $tid = $row->getSourceProperty('tid'); + // vocabulary machine name + $machine_name = $row->getSourceProperty('machine_name'); + $revid = $row->getSourceProperty('revision_id'); // 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)); + foreach ($this->getFields('taxonomy_term', $machine_name) as $field_name => $field) { + // Ensure we're using the right language if the entity is translatable. + $field_language = $field['translatable'] ? $language : NULL; + $row->setSourceProperty($field_name, $this->getFieldValues('taxonomy_term', $field_name, $tid, $revid, $field_language)); } // Find parents for this row. @@ -71,31 +87,14 @@ class D7TaxonomyTermTags extends FieldableEntity { ->execute() ->fetchCol(); $row->setSourceProperty('parent', $parents); - // print_r($parents); - // If the term name or term description were replaced by real fields using - // the Drupal 7 Title module, use the fields value instead of the term name - // or term description. - // TODO: translations + // If the node title was replaced by a real field using the Drupal 7 Title + // module, use the field value instead of the node title. if ($this->moduleExists('title')) { $name_field = $row->getSourceProperty('name_field'); - // print_r($name_field); if (isset($name_field[0]['value'])) { $row->setSourceProperty('name', $name_field[0]['value']); } - $description_field = $row->getSourceProperty('description_field'); - if (isset($description_field[0]['value'])) { - $row->setSourceProperty('description', $description_field[0]['value']); - } - } - - // Synonyms - if ($this->moduleExists('title')) { - $synonyms = $row->getSourceProperty('synonyms_synonym'); - // print_r($synonyms); - if(!empty($synonyms)){ - $row->setSourceProperty('synonyms', $synonyms); - } } return parent::prepareRow($row); diff --git a/modules/custom/materio_migrate/src/Plugin/migrate/source/D7TaxonomyTermTagsI18n.php b/modules/custom/materio_migrate/src/Plugin/migrate/source/D7TaxonomyTermTagsI18n.php new file mode 100644 index 00000000..6981e519 --- /dev/null +++ b/modules/custom/materio_migrate/src/Plugin/migrate/source/D7TaxonomyTermTagsI18n.php @@ -0,0 +1,100 @@ +select('taxonomy_term_data', 'td') + ->fields('td') + ->condition('et.entity_type', 'taxonomy_term') + ->condition('et.source', '', '<>') + ->orderBy('tid'); + + $query->innerJoin('entity_translation', 'et', 'et.entity_id = td.tid'); + $query->fields('et'); + $query->leftJoin('taxonomy_vocabulary', 'tv', 'tv.vid = td.vid'); + $query->fields('tv', ['machine_name']); + + if (isset($this->configuration['bundle'])) { + $query->condition('tv.machine_name', (array) $this->configuration['bundle'], 'IN'); + } + return $query; + } + + /** + * {@inheritdoc} + */ + public function fields() { + $fields = [ + '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.'), + 'revision_id' => $this->t('Revision ID'), + 'language' => $this->t('Term translation language'), + 'source' => $this->t('Term translation source language'), + '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) { + $language = $row->getSourceProperty('language'); + $tid = $row->getSourceProperty('tid'); + + drush_print('-- '. $language .' -- '.$tid." -- ".$row->getSourceProperty('name')." -- ".$row->getSourceProperty('weight')); + + // vocabulary machine name + $machine_name = $row->getSourceProperty('machine_name'); + $revid = $row->getSourceProperty('revision_id'); + + // Get Field API field values. + foreach ($this->getFields('taxonomy_term', $machine_name) as $field_name => $field) { + // Ensure we're using the right language if the entity is translatable. + $field_language = $field['translatable'] ? $language : NULL; + $row->setSourceProperty($field_name, $this->getFieldValues('taxonomy_term', $field_name, $tid, $revid, $field_language)); + } + + // If the node title was replaced by a real field using the Drupal 7 Title + // module, use the field value instead of the node title. + if ($this->moduleExists('title')) { + $name_field = $row->getSourceProperty('name_field'); + if (isset($name_field[0]['value'])) { + $row->setSourceProperty('name', $name_field[0]['value']); + } + } + + return parent::prepareRow($row); + } + + /** + * {@inheritdoc} + */ + public function getIds() { + $ids['tid']['type'] = 'integer'; + return $ids; + } + +} diff --git a/modules/custom/materio_migrate/src/Plugin/migrate/source/D7TaxonomyTermThesaurusI18n.php b/modules/custom/materio_migrate/src/Plugin/migrate/source/D7TaxonomyTermThesaurusI18n.php index 1ee5415c..18b805e2 100644 --- a/modules/custom/materio_migrate/src/Plugin/migrate/source/D7TaxonomyTermThesaurusI18n.php +++ b/modules/custom/materio_migrate/src/Plugin/migrate/source/D7TaxonomyTermThesaurusI18n.php @@ -62,9 +62,10 @@ class D7TaxonomyTermThesaurusI18n extends FieldableEntity { */ public function prepareRow(Row $row) { $language = $row->getSourceProperty('language'); - drush_print('-- '. $language .' -- '.$row->getSourceProperty('tid')." -- ".$row->getSourceProperty('name')." -- ".$row->getSourceProperty('weight')); - $tid = $row->getSourceProperty('tid'); + + drush_print('-- '. $language .' -- '.$tid." -- ".$row->getSourceProperty('name')." -- ".$row->getSourceProperty('weight')); + // vocabulary machine name $machine_name = $row->getSourceProperty('machine_name'); $revid = $row->getSourceProperty('revision_id');