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 @@
'created' => '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(
'entity_type' => 'user',
'entity_id' => '2',
......@@ -5050,10 +5086,34 @@
'revision_id' => '4',
'language' => 'en',
'delta' => '0',
'description_field_value' => 'The third term.',
'description_field_value' => 'The third term in plain old English.',
'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' => '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();
$connection->schema()->createTable('field_data_field_boolean', array(
......@@ -6223,6 +6283,26 @@
'delta' => '0',
'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();
$connection->schema()->createTable('field_data_field_integer_list', array(
......@@ -8648,7 +8728,29 @@
'revision_id' => '4',
'language' => 'en',
'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,
))
->execute();
......@@ -9392,8 +9494,32 @@
'revision_id' => '4',
'language' => 'en',
'delta' => '0',
'description_field_value' => 'The third term.',
'description_field_summary' => NULL,
'description_field_value' => 'The third term in plain old English.',
'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',
))
->execute();
......@@ -10557,6 +10683,26 @@
'delta' => '0',
'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();
$connection->schema()->createTable('field_revision_field_integer_list', array(
......@@ -13015,7 +13161,29 @@
'revision_id' => '4',
'language' => 'en',
'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,
))
->execute();
......@@ -34,6 +34,7 @@ process:
source: '@parent_id'
forum_container: is_container
changed: timestamp
langcode: language
destination:
plugin: entity:taxonomy_term
migration_dependencies:
......
......@@ -56,10 +56,26 @@ public function fields() {
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$tid = $row->getSourceProperty('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 (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', $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.
......@@ -87,6 +103,9 @@ public function prepareRow(Row $row) {
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']);
}
}
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 {
public static $modules = [
'comment',
'content_translation',
'datetime',
'forum',
'image',
'language',
'link',
'menu_ui',
// Required for translation migrations.
'migrate_drupal_multilingual',
'node',
'taxonomy',
'telephone',
......@@ -38,16 +42,23 @@ class MigrateTaxonomyTermTest extends MigrateDrupal7TestBase {
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('comment');
$this->installEntitySchema('node');
$this->installEntitySchema('taxonomy_term');
$this->installConfig(static::$modules);
$this->executeMigrations([
'language',
'd7_user_role',
'd7_user',
'd7_node_type',
'd7_comment_type',
'd7_field',
'd7_taxonomy_vocabulary',
'd7_field_instance',
'd7_taxonomy_term',
'd7_entity_translation_settings',
'd7_taxonomy_term_entity_translation',
]);
}
......@@ -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(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(6, 'Games', 'forums', '', NULL, 4, [], NULL, NULL, 1);
$this->assertEntity(7, 'Minecraft', 'forums', '', NULL, 1, [6]);
......@@ -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");
}
/**
* 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);
$this->assertSame('1531922278', $metadata_is->getCreatedTime());
$this->assertSame('1531922279', $metadata_is->getChangedTime());
$this->assertFalse($metadata_is->isOutdated());
// Test that untranslatable properties are the same as the source language.
$this->assertSame($term->bundle(), $term_fr->bundle());
$this->assertSame($term->bundle(), $term_is->bundle());
$this->assertSame($term->getWeight(), $term_fr->getWeight());
$this->assertSame($term->getWeight(), $term_is->getWeight());
$this->assertSame($term->parent->terget_id, $term_fr->parent->terget_id);
$this->assertSame($term->parent->terget_id, $term_is->parent->terget_id);
}
}
<?php
namespace Drupal\Tests\taxonomy\Kernel\Plugin\migrate\source\d7;
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
/**
* Tests taxonomy term entity translation source plugin.
*
* @covers \Drupal\taxonomy\Plugin\migrate\source\d7\TermEntityTranslation
* @group taxonomy
*/
class TermEntityTranslationTest extends MigrateSqlSourceTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['taxonomy', 'migrate_drupal'];
/**
* {@inheritdoc}
*/
public function providerSource() {
$tests = [];
// The source data.
$tests[0]['source_data']['entity_translation'] = [
[
'entity_type' => 'taxonomy_term',
'entity_id' => 1,
'revision_id' => 1,
'language' => 'en',
'source' => '',
'uid' => 1,
'status' => 1,
'translate' => 0,
'created' => 1531343498,
'changed' => 1531343498,
],
[
'entity_type' => 'taxonomy_term',
'entity_id' => 1,
'revision_id' => 1,
'language' => 'fr',
'source' => 'en',
'uid' => 2,
'status' => 1,
'translate' => 1,
'created' => 1531343508,
'changed' => 1531343508,
],
[
'entity_type' => 'taxonomy_term',
'entity_id' => 1,
'revision_id' => 1,
'language' => 'es',
'source' => 'en',
'uid' => 1,
'status' => 0,
'translate' => 0,
'created' => 1531343528,
'changed' => 1531343528,
],
];
$tests[0]['source_data']['field_config'] = [
[
'id' => 1,
'field_name' => 'field_test',
'type' => 'text',
'module' => 'text',
'active' => 1,
'storage_type' => 'field_sql_storage',
'storage_module' => 'field_sql_storage',
'storage_active' => 1,
'locked' => 1,
'data' => 'a:0:{}',
'cardinality' => 1,
'translatable' => 1,
'deleted' => 0,
],
[
'id' => 2,
'field_name' => 'name_field',
'type' => 'text',
'module' => 'text',
'active' => 1,
'storage_type' => 'field_sql_storage',
'storage_module' => 'field_sql_storage',
'storage_active' => 1,
'locked' => 1,
'data' => 'a:0:{}',
'cardinality' => 1,
'translatable' => 1,
'deleted' => 0,
],
[
'id' => 3,
'field_name' => 'description_field',
'type' => 'text',
'module' => 'text',
'active' => 1,
'storage_type' => 'field_sql_storage',
'storage_module' => 'field_sql_storage',
'storage_active' => 1,
'locked' => 1,
'data' => 'a:0:{}',
'cardinality' => 1,
'translatable' => 1,
'deleted' => 0,
],
];
$tests[0]['source_data']['field_config_instance'] = [
[
'id' => '1',
'field_id' => 1,
'field_name' => 'field_test',
'entity_type' => 'taxonomy_term',
'bundle' => 'tags',
'data' => 'a:0:{}',
'deleted' => 0,
],
[
'id' => 2,
'field_id' => 2,
'field_name' => 'name_field',
'entity_type' => 'taxonomy_term',
'bundle' => 'tags',
'data' => 'a:0:{}',
'deleted' => 0,
],
[
'id' => 3,
'field_id' => 3,
'field_name' => 'description_field',
'entity_type' => 'taxonomy_term',
'bundle' => 'tags',
'data' => 'a:0:{}',
'deleted' => 0,
],
];
$tests[0]['source_data']['field_data_field_test'] = [
[
'entity_type' => 'taxonomy_term',
'bundle' => 'tags',
'deleted' => 0,
'entity_id' => 1,
'revision_id' => 1,
'language' => 'en',
'delta' => 0,
'field_test_value' => 'English field',
'field_test_format' => 'filtered_html',
],
[
'entity_type' => 'taxonomy_term',
'bundle' => 'tags',
'deleted' => 0,
'entity_id' => 1,
'revision_id' => 1,
'language' => 'fr',
'delta' => 0,
'field_test_value' => 'French field',
'field_test_format' => 'filtered_html',
],