Skip to content
Snippets Groups Projects
Commit dbf4fb86 authored by Gábor Hojtsy's avatar Gábor Hojtsy
Browse files

Issue #3008030 by quietone, masipila, maxocub: Migrate D7 i18n fields label and description

parent 96d00c90
No related branches found
No related tags found
No related merge requests found
id: d7_field_instance_label_description_translation
label: Field label and description translation
migration_tags:
- Drupal 7
- Configuration
- Multilingual
class: Drupal\migrate_drupal\Plugin\migrate\FieldMigration
field_plugin_method: alterFieldInstanceMigration
source:
plugin: d7_field_instance_label_description_translation
process:
langcode:
plugin: skip_on_empty
source: language
method: row
translation:
plugin: skip_on_empty
source: translation
method: row
entity_type_exists:
plugin: skip_on_empty
source: entity_type
method: row
objectid_exists:
plugin: skip_on_empty
source: objectid
method: row
type_exists:
plugin: skip_on_empty
source: type
method: row
exists:
-
plugin: migration_lookup
migration: d7_field_instance
source:
- entity_type
- objectid
- type
-
plugin: skip_on_empty
method: row
bundle: objectid
property:
plugin: static_map
source: property
bypass: true
map:
label: label
description: description
title_value: label
entity_type: entity_type
field_name: type
destination:
plugin: entity:field_config
migration_dependencies:
required:
- d7_field_instance
optional:
- d7_node_type
- d7_comment_type
- d7_taxonomy_vocabulary
<?php
namespace Drupal\field\Plugin\migrate\source\d7;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
/**
* Gets field label and description translations.
*
* @MigrateSource(
* id = "d7_field_instance_label_description_translation",
* source_module = "i18n_field"
* )
*/
class FieldLabelDescriptionTranslation extends DrupalSqlBase {
/**
* {@inheritdoc}
*/
public function query() {
// Get translations for field labels and descriptions.
$query = $this->select('i18n_string', 'i18n')
->fields('i18n')
->fields('lt', [
'lid',
'translation',
'language',
'plid',
'plural',
'i18n_status',
])
->fields('fci', [
'id',
'field_id',
'field_name',
'entity_type',
'bundle',
'data',
'deleted',
])
->condition('i18n.textgroup', 'field')
->isNotNull('language')
->isNotNull('translation');
$condition = $query->orConditionGroup()
->condition('textgroup', 'field')
->condition('objectid', '#allowed_values', '!=');
$query->condition($condition);
$query->leftJoin('locales_target', 'lt', 'lt.lid = i18n.lid');
$query->leftjoin('field_config_instance', 'fci', 'fci.bundle = i18n.objectid AND fci.field_name = i18n.type');
return $query;
}
/**
* {@inheritdoc}
*/
public function fields() {
return [
'lid' => $this->t('Locales target language ID.'),
'textgroup' => $this->t('A module defined group of translations'),
'context' => $this->t('Full string ID for quick search: type:objectid:property.'),
'objectid' => $this->t('Object ID'),
'type' => $this->t('Object type for this string'),
'property' => $this->t('Object property for this string'),
'objectindex' => $this->t('Integer value of Object ID'),
'format' => $this->t('The {filter_format}.format of the string'),
'translation' => $this->t('Translation'),
'language' => $this->t('Language code'),
'plid' => $this->t('Parent lid'),
'plural' => $this->t('Plural index number'),
'i18n_status' => $this->t('Translation needs update'),
'id' => $this->t('The field instance ID.'),
'field_id' => $this->t('The field ID.'),
'field_name' => $this->t('The field name.'),
'entity_type' => $this->t('The entity type.'),
'bundle' => $this->t('The entity bundle.'),
'data' => $this->t('The field instance data.'),
'deleted' => $this->t('Deleted'),
];
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['property']['type'] = 'string';
$ids['language']['type'] = 'string';
$ids['lid']['type'] = 'integer';
$ids['lid']['alias'] = 'lt';
return $ids;
}
}
......@@ -8,7 +8,7 @@
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Tests migration field label and description i18n translations.
* Tests migration of field label and description translations.
*
* @group migrate_drupal_6
* @group legacy
......@@ -57,7 +57,7 @@ public static function migrateDumpAlter(KernelTestBase $test) {
}
/**
* Tests migration of file variables to file.settings.yml.
* Tests migration of field label and description translations.
*/
public function testFieldInstanceLabelDescriptionTranslationMigration() {
$language_manager = $this->container->get('language_manager');
......
<?php
namespace Drupal\Tests\field\Kernel\Migrate\d7;
use Drupal\Core\Database\Database;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\migrate\Kernel\MigrateDumpAlterInterface;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
/**
* Tests migration field label and description i18n translations.
*
* @group migrate_drupal_7
* @group legacy
*/
class MigrateFieldInstanceLabelDescriptionTest extends MigrateDrupal7TestBase implements MigrateDumpAlterInterface {
/**
* {@inheritdoc}
*/
public static $modules = [
'comment',
'config_translation',
'datetime',
'field',
'file',
'image',
'language',
'link',
'locale',
'menu_ui',
// Required for translation migrations.
'migrate_drupal_multilingual',
'node',
'system',
'taxonomy',
'telephone',
'text',
];
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->installEntitySchema('node');
$this->installEntitySchema('comment');
$this->installEntitySchema('taxonomy_term');
$this->installConfig(static::$modules);
$this->executeMigrations([
'd7_node_type',
'd7_comment_type',
'd7_taxonomy_vocabulary',
'd7_field',
'd7_field_instance',
'd7_field_instance_widget_settings',
'language',
'd7_field_instance_label_description_translation',
]);
}
/**
* {@inheritdoc}
*/
public static function migrateDumpAlter(KernelTestBase $test) {
$db = Database::getConnection('default', 'migrate');
// Alter the database to test the migration is successful when a translated
// field is deleted but the translation data for that field remains in both
// the i18n_strings and locales_target tables.
$db->delete('field_config_instance')
->condition('field_name', 'field_image')
->condition('bundle', 'article')
->execute();
}
/**
* Tests migration of file variables to file.settings.yml.
*/
public function testFieldInstanceLabelDescriptionTranslationMigration() {
$language_manager = $this->container->get('language_manager');
// Check that the deleted field with translations was skipped.
$config_translation = $language_manager->getLanguageConfigOverride('fr', 'field.field.node.article.field_image');
$this->assertNull($config_translation->get('label'));
$this->assertNull($config_translation->get('description'));
// Tests fields on 'test_content_type' node type.
$config_translation = $language_manager->getLanguageConfigOverride('fr', 'field.field.node.test_content_type.field_email');
$this->assertNull($config_translation->get('label'));
$this->assertSame("fr - The email help text.", $config_translation->get('description'));
$config_translation = $language_manager->getLanguageConfigOverride('is', 'field.field.node.test_content_type.field_email');
$this->assertSame("is - Email", $config_translation->get('label'));
$this->assertSame("is - The email help text.", $config_translation->get('description'));
$config_translation = $language_manager->getLanguageConfigOverride('is', 'field.field.node.test_content_type.field_boolean');
$this->assertSame("is - Some helpful text.", $config_translation->get('description'));
// Tests fields on 'article' node type.
$config_translation = $language_manager->getLanguageConfigOverride('fr', 'field.field.node.article.body');
$this->assertSame("fr - Body", $config_translation->get('label'));
$config_translation = $language_manager->getLanguageConfigOverride('fr', 'field.field.node.article.field_link');
$this->assertSame("fr - Link", $config_translation->get('label'));
// Tests fields on 'test_vocabulary' vocabulary type.
$config_translation = $language_manager->getLanguageConfigOverride('is', 'field.field.taxonomy_term.test_vocabulary.field_term_reference');
$this->assertSame("is - Term Reference", $config_translation->get('label'));
}
}
<?php
namespace Drupal\Tests\field\Kernel\Plugin\migrate\source\d7;
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
/**
* Tests the field label and description translation source plugin.
*
* @covers \Drupal\field\Plugin\migrate\source\d7\FieldLabelDescriptionTranslation
* @group migrate_drupal
*/
class FieldInstanceLabelDescriptionTranslationTest extends MigrateSqlSourceTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['config_translation', 'migrate_drupal', 'field'];
/**
* {@inheritdoc}
*/
public function providerSource() {
$test = [];
// The source data.
$test[0]['source_data'] = [
'i18n_string' => [
[
'lid' => 10,
'textgroup' => 'field',
'objectid' => 'story',
'type' => 'field_image',
'property' => 'label',
],
[
'lid' => 11,
'textgroup' => 'field',
'objectid' => 'story',
'type' => 'field_image',
'property' => 'description',
],
[
'lid' => 12,
'textgroup' => 'field',
'objectid' => 'forum',
'type' => 'taxonomy_forums',
'property' => 'label',
],
],
'locales_target' => [
[
'lid' => 10,
'translation' => 'fr - story label',
'language' => 'fr',
'plid' => 0,
'plural' => 0,
'i18n_status' => 0,
],
[
'lid' => 11,
'translation' => 'fr - story description',
'language' => 'fr',
'plid' => 0,
'plural' => 0,
'i18n_status' => 0,
],
[
'lid' => 12,
'translation' => 'zu - term reference',
'language' => 'zu',
'plid' => 0,
'plural' => 0,
'i18n_status' => 0,
],
],
'field_config_instance' => [
[
'id' => '2',
'field_id' => '2',
'field_name' => 'field_image',
'entity_type' => 'node',
'bundle' => 'story',
'data' => 'a:0:{}',
'deleted' => '0',
],
[
'id' => '3',
'field_id' => '3',
'field_name' => 'field_image',
'entity_type' => 'node',
'bundle' => 'article',
'data' => 'a:0:{}',
'deleted' => '0',
],
[
'id' => '3',
'field_id' => '4',
'field_name' => 'field_term_reference',
'entity_type' => 'taxonomy_term',
'bundle' => 'trees',
'data' => 'a:0:{}',
'deleted' => '0',
],
],
];
$test[0]['expected_results'] = [
[
'property' => 'label',
'translation' => "fr - story label",
'language' => 'fr',
'lid' => '10',
],
[
'property' => 'description',
'translation' => 'fr - story description',
'language' => 'fr',
'lid' => '11',
],
[
'property' => 'label',
'translation' => 'zu - term reference',
'language' => 'zu',
'lid' => '12',
],
];
return $test;
}
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment