Commit e5a4531a authored by catch's avatar catch

Issue #2980996 by maxocub, masipila, mradcliffe, Gábor Hojtsy: Migrate Drupal...

Issue #2980996 by maxocub, masipila, mradcliffe, Gábor Hojtsy: Migrate Drupal 7 taxonomy term entity translations data to Drupal 8
parent cff48a22
id: d7_taxonomy_term_entity_translation
label: Taxonomy term entity translations
migration_tags:
- Drupal 7
- translation
- Content
- Multilingual
deriver: Drupal\taxonomy\Plugin\migrate\D7TaxonomyTermDeriver
source:
plugin: d7_taxonomy_term_entity_translation
process:
tid: entity_id
name: name
description/value: description
description/format: format
langcode: language
status: status
content_translation_source: source
content_translation_outdated: translate
content_translation_uid: uid
content_translation_created: created
changed: changed
forum_container: is_container
destination:
plugin: entity:taxonomy_term
translations: true
destination_module: content_translation
migration_dependencies:
required:
- language
- d7_entity_translation_settings
- d7_taxonomy_term
...@@ -3175,6 +3175,42 @@ ...@@ -3175,6 +3175,42 @@
'created' => '1529615813', 'created' => '1529615813',
'changed' => '1529615813', 'changed' => '1529615813',
)) ))
->values(array(
'entity_type' => 'taxonomy_term',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'en',
'source' => '',
'uid' => '1',
'status' => '1',
'translate' => '0',
'created' => '1531922259',
'changed' => '1531922259',
))
->values(array(
'entity_type' => 'taxonomy_term',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'fr',
'source' => 'en',
'uid' => '2',
'status' => '1',
'translate' => '1',
'created' => '1531922267',
'changed' => '1531922268',
))
->values(array(
'entity_type' => 'taxonomy_term',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'is',
'source' => 'en',
'uid' => '1',
'status' => '0',
'translate' => '0',
'created' => '1531922278',
'changed' => '1531922279',
))
->values(array( ->values(array(
'entity_type' => 'user', 'entity_type' => 'user',
'entity_id' => '2', 'entity_id' => '2',
...@@ -5050,10 +5086,34 @@ ...@@ -5050,10 +5086,34 @@
'revision_id' => '4', 'revision_id' => '4',
'language' => 'en', 'language' => 'en',
'delta' => '0', 'delta' => '0',
'description_field_value' => 'The third term.', 'description_field_value' => 'The third term in plain old English.',
'description_field_summary' => '', 'description_field_summary' => '',
'description_field_format' => 'full_html', 'description_field_format' => 'full_html',
)) ))
->values(array(
'entity_type' => 'taxonomy_term',
'bundle' => 'test_vocabulary',
'deleted' => '0',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'fr',
'delta' => '0',
'description_field_value' => 'The third term en français s\'il vous plaît.',
'description_field_summary' => '',
'description_field_format' => 'filtered_html',
))
->values(array(
'entity_type' => 'taxonomy_term',
'bundle' => 'test_vocabulary',
'deleted' => '0',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'is',
'delta' => '0',
'description_field_value' => 'The third term á íslensku.',
'description_field_summary' => '',
'description_field_format' => 'plain_text',
))
->execute(); ->execute();
$connection->schema()->createTable('field_data_field_boolean', array( $connection->schema()->createTable('field_data_field_boolean', array(
...@@ -6223,6 +6283,26 @@ ...@@ -6223,6 +6283,26 @@
'delta' => '0', 'delta' => '0',
'field_integer_value' => '6', 'field_integer_value' => '6',
)) ))
->values(array(
'entity_type' => 'taxonomy_term',
'bundle' => 'test_vocabulary',
'deleted' => '0',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'fr',
'delta' => '0',
'field_integer_value' => '5',
))
->values(array(
'entity_type' => 'taxonomy_term',
'bundle' => 'test_vocabulary',
'deleted' => '0',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'is',
'delta' => '0',
'field_integer_value' => '4',
))
->execute(); ->execute();
$connection->schema()->createTable('field_data_field_integer_list', array( $connection->schema()->createTable('field_data_field_integer_list', array(
...@@ -8648,7 +8728,29 @@ ...@@ -8648,7 +8728,29 @@
'revision_id' => '4', 'revision_id' => '4',
'language' => 'en', 'language' => 'en',
'delta' => '0', 'delta' => '0',
'name_field_value' => 'Term3', 'name_field_value' => 'Term3 in plain old English',
'name_field_format' => NULL,
))
->values(array(
'entity_type' => 'taxonomy_term',
'bundle' => 'test_vocabulary',
'deleted' => '0',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'fr',
'delta' => '0',
'name_field_value' => 'Term3 en français s\'il vous plaît',
'name_field_format' => NULL,
))
->values(array(
'entity_type' => 'taxonomy_term',
'bundle' => 'test_vocabulary',
'deleted' => '0',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'is',
'delta' => '0',
'name_field_value' => 'Term3 á íslensku',
'name_field_format' => NULL, 'name_field_format' => NULL,
)) ))
->execute(); ->execute();
...@@ -9392,8 +9494,32 @@ ...@@ -9392,8 +9494,32 @@
'revision_id' => '4', 'revision_id' => '4',
'language' => 'en', 'language' => 'en',
'delta' => '0', 'delta' => '0',
'description_field_value' => 'The third term.', 'description_field_value' => 'The third term in plain old English.',
'description_field_summary' => NULL, 'description_field_summary' => '',
'description_field_format' => 'full_html',
))
->values(array(
'entity_type' => 'taxonomy_term',
'bundle' => 'test_vocabulary',
'deleted' => '0',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'fr',
'delta' => '0',
'description_field_value' => 'The third term en français s\'il vous plaît.',
'description_field_summary' => '',
'description_field_format' => 'full_html',
))
->values(array(
'entity_type' => 'taxonomy_term',
'bundle' => 'test_vocabulary',
'deleted' => '0',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'is',
'delta' => '0',
'description_field_value' => 'The third term á íslensku.',
'description_field_summary' => '',
'description_field_format' => 'full_html', 'description_field_format' => 'full_html',
)) ))
->execute(); ->execute();
...@@ -10557,6 +10683,26 @@ ...@@ -10557,6 +10683,26 @@
'delta' => '0', 'delta' => '0',
'field_integer_value' => '6', 'field_integer_value' => '6',
)) ))
->values(array(
'entity_type' => 'taxonomy_term',
'bundle' => 'test_vocabulary',
'deleted' => '0',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'fr',
'delta' => '0',
'field_integer_value' => '5',
))
->values(array(
'entity_type' => 'taxonomy_term',
'bundle' => 'test_vocabulary',
'deleted' => '0',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'is',
'delta' => '0',
'field_integer_value' => '4',
))
->execute(); ->execute();
$connection->schema()->createTable('field_revision_field_integer_list', array( $connection->schema()->createTable('field_revision_field_integer_list', array(
...@@ -13015,7 +13161,29 @@ ...@@ -13015,7 +13161,29 @@
'revision_id' => '4', 'revision_id' => '4',
'language' => 'en', 'language' => 'en',
'delta' => '0', 'delta' => '0',
'name_field_value' => 'Term3', 'name_field_value' => 'Term3 in plain old English',
'name_field_format' => NULL,
))
->values(array(
'entity_type' => 'taxonomy_term',
'bundle' => 'test_vocabulary',
'deleted' => '0',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'fr',
'delta' => '0',
'name_field_value' => 'Term3 en français s\'il vous plaît',
'name_field_format' => NULL,
))
->values(array(
'entity_type' => 'taxonomy_term',
'bundle' => 'test_vocabulary',
'deleted' => '0',
'entity_id' => '4',
'revision_id' => '4',
'language' => 'is',
'delta' => '0',
'name_field_value' => 'Term3 á íslensku',
'name_field_format' => NULL, 'name_field_format' => NULL,
)) ))
->execute(); ->execute();
...@@ -34,6 +34,7 @@ process: ...@@ -34,6 +34,7 @@ process:
source: '@parent_id' source: '@parent_id'
forum_container: is_container forum_container: is_container
changed: timestamp changed: timestamp
langcode: language
destination: destination:
plugin: entity:taxonomy_term plugin: entity:taxonomy_term
migration_dependencies: migration_dependencies:
......
...@@ -56,10 +56,26 @@ public function fields() { ...@@ -56,10 +56,26 @@ public function fields() {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function prepareRow(Row $row) { 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'); $tid = $row->getSourceProperty('tid');
$row->setSourceProperty($field, $this->getFieldValues('taxonomy_term', $field, $tid)); $vocabulary = $row->getSourceProperty('machine_name');
$default_language = (array) $this->variableGet('language_default', ['language' => 'en']);
// If this entity was translated using Entity Translation, we need to get
// its source language to get the field values in the right language.
// The translations will be migrated by the d7_node_entity_translation
// migration.
$translatable_vocabularies = array_keys(array_filter($this->variableGet('entity_translation_taxonomy', [])));
$entity_translatable = $this->isEntityTranslatable('taxonomy_term') && in_array($vocabulary, $translatable_vocabularies, TRUE);
$source_language = $this->getEntityTranslationSourceLanguage('taxonomy_term', $tid);
$language = $entity_translatable && $source_language ? $source_language : $default_language['language'];
$row->setSourceProperty('language', $language);
// Get Field API field values.
foreach ($this->getFields('taxonomy_term', $vocabulary) as $field_name => $field) {
// Ensure we're using the right language if the entity and the field are
// translatable.
$field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
$row->setSourceProperty($field_name, $this->getFieldValues('taxonomy_term', $field_name, $tid, NULL, $field_language));
} }
// Find parents for this row. // Find parents for this row.
...@@ -87,6 +103,9 @@ public function prepareRow(Row $row) { ...@@ -87,6 +103,9 @@ public function prepareRow(Row $row) {
if (isset($description_field[0]['value'])) { if (isset($description_field[0]['value'])) {
$row->setSourceProperty('description', $description_field[0]['value']); $row->setSourceProperty('description', $description_field[0]['value']);
} }
if (isset($description_field[0]['format'])) {
$row->setSourceProperty('format', $description_field[0]['format']);
}
} }
return parent::prepareRow($row); return parent::prepareRow($row);
......
<?php
namespace Drupal\taxonomy\Plugin\migrate\source\d7;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
/**
* Provides Drupal 7 taxonomy term entity translation source plugin.
*
* @MigrateSource(
* id = "d7_taxonomy_term_entity_translation",
* source_module = "entity_translation"
* )
*/
class TermEntityTranslation extends FieldableEntity {
/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('entity_translation', 'et')
->fields('et')
->fields('td', [
'name',
'description',
'format',
])
->fields('tv', [
'machine_name',
])
->condition('et.entity_type', 'taxonomy_term')
->condition('et.source', '', '<>');
$query->innerJoin('taxonomy_term_data', 'td', 'td.tid = et.entity_id');
$query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
if (isset($this->configuration['bundle'])) {
$query->condition('tv.machine_name', (array) $this->configuration['bundle'], 'IN');
}
return $query;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$tid = $row->getSourceProperty('entity_id');
$vocabulary = $row->getSourceProperty('machine_name');
$language = $row->getSourceProperty('language');
// Get Field API field values.
foreach ($this->getFields('taxonomy_term', $vocabulary) 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, NULL, $field_language));
}
// 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.
if ($this->moduleExists('title')) {
$name_field = $row->getSourceProperty('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']);
}
if (isset($description_field[0]['format'])) {
$row->setSourceProperty('format', $description_field[0]['format']);
}
}
// Determine if this is a forum container.
$forum_container_tids = $this->variableGet('forum_containers', []);
$row->setSourceProperty('is_container', in_array($tid, $forum_container_tids));
return parent::prepareRow($row);
}
/**
* {@inheritdoc}
*/
public function fields() {
return [
'entity_type' => $this->t('The entity type this translation relates to'),
'entity_id' => $this->t('The entity ID this translation relates to'),
'revision_id' => $this->t('The entity revision ID this translation relates to'),
'language' => $this->t('The target language for this translation.'),
'source' => $this->t('The source language from which this translation was created.'),
'uid' => $this->t('The author of this translation.'),
'status' => $this->t('Boolean indicating whether the translation is published (visible to non-administrators).'),
'translate' => $this->t('A boolean indicating whether this translation needs to be updated.'),
'created' => $this->t('The Unix timestamp when the translation was created.'),
'changed' => $this->t('The Unix timestamp when the translation was most recently saved.'),
'name' => $this->t('The name of the term.'),
'description' => $this->t('The term description.'),
'format' => $this->t('Format of the term description.'),
'machine_name' => $this->t('Vocabulary machine name'),
];
}
/**
* {@inheritdoc}
*/
public function getIds() {
return [
'entity_id' => [
'type' => 'integer',
'alias' => 'et',
],
'language' => [
'type' => 'string',
'alias' => 'et',
],
];
}
}
...@@ -15,11 +15,15 @@ class MigrateTaxonomyTermTest extends MigrateDrupal7TestBase { ...@@ -15,11 +15,15 @@ class MigrateTaxonomyTermTest extends MigrateDrupal7TestBase {
public static $modules = [ public static $modules = [
'comment', 'comment',
'content_translation',
'datetime', 'datetime',
'forum', 'forum',
'image', 'image',
'language',
'link', 'link',
'menu_ui', 'menu_ui',
// Required for translation migrations.
'migrate_drupal_multilingual',
'node', 'node',
'taxonomy', 'taxonomy',
'telephone', 'telephone',
...@@ -38,16 +42,23 @@ class MigrateTaxonomyTermTest extends MigrateDrupal7TestBase { ...@@ -38,16 +42,23 @@ class MigrateTaxonomyTermTest extends MigrateDrupal7TestBase {
*/ */
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->installEntitySchema('comment');
$this->installEntitySchema('node');
$this->installEntitySchema('taxonomy_term'); $this->installEntitySchema('taxonomy_term');
$this->installConfig(static::$modules); $this->installConfig(static::$modules);
$this->executeMigrations([ $this->executeMigrations([
'language',
'd7_user_role',
'd7_user',
'd7_node_type', 'd7_node_type',
'd7_comment_type', 'd7_comment_type',
'd7_field', 'd7_field',
'd7_taxonomy_vocabulary', 'd7_taxonomy_vocabulary',
'd7_field_instance', 'd7_field_instance',
'd7_taxonomy_term', 'd7_taxonomy_term',
'd7_entity_translation_settings',
'd7_taxonomy_term_entity_translation',
]); ]);
} }
...@@ -110,7 +121,7 @@ public function testTaxonomyTerms() { ...@@ -110,7 +121,7 @@ public function testTaxonomyTerms() {
$this->assertEntity(2, 'Term1 (This is a real field!)', 'test_vocabulary', 'The first term. (This is a real field!)', 'filtered_html', 0, [], NULL, 3); $this->assertEntity(2, 'Term1 (This is a real field!)', 'test_vocabulary', 'The first term. (This is a real field!)', 'filtered_html', 0, [], NULL, 3);
$this->assertEntity(3, 'Term2', 'test_vocabulary', 'The second term.', 'filtered_html'); $this->assertEntity(3, 'Term2', 'test_vocabulary', 'The second term.', 'filtered_html');
$this->assertEntity(4, 'Term3', 'test_vocabulary', 'The third term.', 'full_html', 0, [3], 6); $this->assertEntity(4, 'Term3 in plain old English', 'test_vocabulary', 'The third term in plain old English.', 'full_html', 0, [3], 6);
$this->assertEntity(5, 'Custom Forum', 'forums', 'Where the cool kids are.', NULL, 3); $this->assertEntity(5, 'Custom Forum', 'forums', 'Where the cool kids are.', NULL, 3);
$this->assertEntity(6, 'Games', 'forums', '', NULL, 4, [], NULL, NULL, 1); $this->assertEntity(6, 'Games', 'forums', '', NULL, 4, [], NULL, NULL, 1);
$this->assertEntity(7, 'Minecraft', 'forums', '', NULL, 1, [6]); $this->assertEntity(7, 'Minecraft', 'forums', '', NULL, 1, [6]);
...@@ -162,4 +173,56 @@ protected function assertHierarchy($vid, $tid, array $parent_ids) { ...@@ -162,4 +173,56 @@ protected function assertHierarchy($vid, $tid, array $parent_ids) {
$this->assertEquals($parent_ids, array_filter($term->parents), "Term $tid has correct parents in taxonomy tree"); $this->assertEquals($parent_ids, array_filter($term->parents), "Term $tid has correct parents in taxonomy tree");
} }
/**
* Tests the migration of taxonomy term entity translations.
*/
public function testTaxonomyTermEntityTranslations() {
$manager = $this->container->get('content_translation.manager');
// Get the term and its translations.
$term = Term::load(4);
$term_fr = $term->getTranslation('fr');
$term_is = $term->getTranslation('is');
// Test that fields translated with Entity Translation are migrated.
$this->assertSame('Term3 in plain old English', $term->getName());
$this->assertSame('Term3 en français s\'il vous plaît', $term_fr->getName());
$this->assertSame('Term3 á íslensku', $term_is->getName());
$this->assertSame('The third term in plain old English.', $term->getDescription());
$this->assertSame('The third term en français s\'il vous plaît.', $term_fr->getDescription());
$this->assertSame('The third term á íslensku.', $term_is->getDescription());
$this->assertSame('full_html', $term->getFormat());
$this->assertSame('filtered_html', $term_fr->getFormat());
$this->assertSame('plain_text', $term_is->getFormat());
$this->assertSame('6', $term->field_integer->value);
$this->assertSame('5', $term_fr->field_integer->value);
$this->assertSame('4', $term_is->field_integer->value);
// Test that the French translation metadata is correctly migrated.
$metadata_fr = $manager->getTranslationMetadata($term_fr);
$this->assertTrue($metadata_fr->isPublished());
$this->assertSame('en', $metadata_fr->getSource());
$this->assertSame('2', $metadata_fr->getAuthor()->uid->value);
$this->assertSame('1531922267', $metadata_fr->getCreatedTime());
$this->assertSame('1531922268', $metadata_fr->getChangedTime());
$this->assertTrue($metadata_fr->isOutdated());
// Test that the Icelandic translation metadata is correctly migrated.
$metadata_is = $manager->getTranslationMetadata($term_is);
$this->assertFalse($metadata_is->isPublished());
$this->assertSame('en', $metadata_is->getSource());
$this->assertSame('1', $metadata_is->getAuthor()->uid->value);