Unverified Commit ae23684b authored by larowlan's avatar larowlan

Issue #3008029 by quietone, wilsonsp, mradcliffe, Gábor Hojtsy, xjm, mikelutz,...

Issue #3008029 by quietone, wilsonsp, mradcliffe, Gábor Hojtsy, xjm, mikelutz, larowlan: Migrate D7 i18n field option translations
parent 807afc16
id: d7_field_instance_option_translation
label: Field instance option configuration translation
migration_tags:
- Drupal 7
- Configuration
- Multilingual
source:
plugin: d7_field_option_translation
constants:
status: true
process:
langcode:
plugin: skip_on_empty
source: language
method: row
type:
plugin: process_field
source: type
method: getFieldType
entity_type: entity_type
field_name: field_name
# # The bundle needs to be statically mapped in order to support comment types
# # that might already exist before this migration is run. See
# # d7_comment_type.yml for more information.
bundle:
plugin: static_map
source: bundle
bypass: true
map:
comment_node_forum: comment_forum
results:
plugin: d7_field_instance_option_translation
source:
- '@type'
- data
translation:
-
plugin: extract
source: '@results'
index: [1]
-
plugin: skip_on_empty
method: row
property:
-
plugin: extract
source: '@results'
index: [0]
-
plugin: skip_on_empty
method: row
destination:
plugin: entity:field_config
translations: true
migration_dependencies:
required:
- d7_field_instance
id: d7_field_option_translation
label: Field option configuration translation
migration_tags:
- Drupal 7
- Configuration
- Multilingual
field_plugin_method: alterFieldMigration
source:
plugin: d7_field_option_translation
process:
langcode:
plugin: skip_on_empty
source: language
method: row
entity_type: entity_type
field_name: field_name
type:
plugin: process_field
source: type
method: getFieldType
results:
plugin: d7_field_option_translation
source:
- '@type'
- data
translation:
-
plugin: extract
source: '@results'
index: [1]
-
plugin: skip_on_empty
method: row
property:
-
plugin: extract
source: '@results'
index: [0]
-
plugin: skip_on_empty
method: row
destination:
plugin: entity:field_storage_config
translations: true
migration_dependencies:
required:
- d7_field
<?php
namespace Drupal\field\Plugin\migrate\process\d7;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* Determines the settings property and translation for boolean fields.
*
* @MigrateProcessPlugin(
* id = "d7_field_instance_option_translation",
* handle_multiples = TRUE
* )
*/
class FieldInstanceOptionTranslation extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
list($type, $data) = $value;
$data = unserialize($data);
$property = $row->getSourceProperty('property');
$option_key = ($property == 0) ? 'off_label' : 'on_label';
$translation = '';
if (isset($data['settings']['allowed_values'])) {
$allowed_values = $data['settings']['allowed_values'];
switch ($type) {
case 'boolean';
if (isset($allowed_values[$property])) {
$translation = $row->getSourceProperty('translation');
break;
}
break;
default:
}
}
return ['settings.' . $option_key, $translation];
}
}
<?php
namespace Drupal\field\Plugin\migrate\process\d7;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* Determines the allowed values translation for select lists.
*
* @MigrateProcessPlugin(
* id = "d7_field_option_translation",
* handle_multiples = TRUE
* )
*/
class FieldOptionTranslation extends ProcessPluginBase {
/**
* {@inheritdoc}
*
* Get the field default/mapped settings.
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
list($type, $data) = $value;
$data = unserialize($data);
$allowed_values = '';
$translation_key = $row->getSourceProperty('property');
$i = 0;
if (isset($data['settings']['allowed_values'])) {
$allowed_values = $data['settings']['allowed_values'];
switch ($type) {
case 'list_string':
case 'list_integer':
case 'list_float':
case 'list_text':
if (isset($allowed_values[$translation_key])) {
$allowed_values = ['label' => $row->getSourceProperty('translation')];
break;
}
break;
default:
}
}
return ["settings.allowed_values.$translation_key", $allowed_values];
}
}
<?php
namespace Drupal\field\Plugin\migrate\source\d7;
/**
* Gets field option label translations.
*
* @MigrateSource(
* id = "d7_field_option_translation",
* source_module = "i18n_field"
* )
*/
class FieldOptionTranslation extends Field {
/**
* {@inheritdoc}
*/
public function query() {
$query = parent::query();
$query->leftJoin('i18n_string', 'i18n', 'i18n.type = fc.field_name');
$query->leftJoin('locales_target', 'lt', 'lt.lid = i18n.lid');
$query->condition('i18n.textgroup', 'field')
->condition('objectid', '#allowed_values')
->isNotNull('language');
// Add all i18n and locales_target fields.
$query
->fields('i18n')
->fields('lt');
$query->addField('fc', 'type');
$query->addField('fci', 'bundle');
$query->addField('i18n', 'lid', 'i18n_lid');
$query->addField('i18n', 'type', 'i18n_type');
$query->orderBy('i18n.lid');
return $query;
}
/**
* {@inheritdoc}
*/
public function fields() {
$fields = [
'bundle' => $this->t('Entity bundle'),
'lid' => $this->t('Source string ID'),
'textgroup' => $this->t('A module defined group of translations'),
'context' => $this->t('Full string ID'),
'objectid' => $this->t('Object ID'),
'property' => $this->t('Object property for this string'),
'objectindex' => $this->t('Integer value of Object ID'),
'format' => $this->t('The input format used by this string'),
'translation' => $this->t('Translation of the option'),
'language' => $this->t('Language code'),
'plid' => $this->t('Parent lid'),
'plural' => $this->t('Plural index number in case of plural strings'),
'i18n_status' => $this->t('A boolean indicating whether this translation needs to be updated'),
];
return parent::fields() + $fields;
}
/**
* {@inheritdoc}
*/
public function getIds() {
return parent::getIds() +
[
'language' => ['type' => 'string'],
'property' => ['type' => 'string'],
];
}
}
<?php
namespace Drupal\Tests\field\Kernel\Migrate\d7;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
/**
* Migrate field option translations.
*
* @group migrate_drupal_7
*/
class MigrateFieldOptionTranslationTest extends MigrateDrupal7TestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'comment',
'config_translation',
'datetime',
'file',
'image',
'language',
'link',
'locale',
'menu_ui',
'node',
'system',
'taxonomy',
'telephone',
'text',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->executeMigrations([
'language',
'd7_field',
'd7_field_option_translation',
]);
}
/**
* Tests the Drupal 7 field option translation to Drupal 8 migration.
*/
public function testFieldOptionTranslation() {
$language_manager = $this->container->get('language_manager');
/** @var \Drupal\language\Config\LanguageConfigOverride $config_translation */
$config_translation = $language_manager->getLanguageConfigOverride('fr', 'field.storage.node.field_color');
$allowed_values = [
0 => [
'label' => 'Verte',
],
1 => [
'label' => 'Noire',
],
2 => [
'label' => 'Blanche',
],
];
$this->assertSame($allowed_values, $config_translation->get('settings.allowed_values'));
$config_translation = $language_manager->getLanguageConfigOverride('is', 'field.storage.node.field_color');
$allowed_values = [
0 => [
'label' => 'Grænn',
],
1 => [
'label' => 'Svartur',
],
2 => [
'label' => 'Hvítur',
],
];
$this->assertSame($allowed_values, $config_translation->get('settings.allowed_values'));
$config_translation = $language_manager->getLanguageConfigOverride('fr', 'field.storage.node.field_rating');
$allowed_values = [
1 => [
'label' => 'Haute',
],
2 => [
'label' => 'Moyenne',
],
3 => [
'label' => 'Faible',
],
];
$this->assertSame($allowed_values, $config_translation->get('settings.allowed_values'));
$config_translation = $language_manager->getLanguageConfigOverride('is', 'field.storage.node.field_rating');
$allowed_values = [
1 => [
'label' => 'Hár',
],
2 => [
'label' => 'Miðlungs',
],
3 => [
'label' => 'Lágt',
],
];
$this->assertSame($allowed_values, $config_translation->get('settings.allowed_values'));
}
}
......@@ -4991,6 +4991,15 @@
'data' => 'a:7:{s:5:"label";s:6:"Rating";s:6:"widget";a:5:{s:6:"weight";s:2:"12";s:4:"type";s:15:"options_buttons";s:6:"module";s:7:"options";s:6:"active";i:1;s:8:"settings";a:0:{}}s:8:"settings";a:2:{s:18:"user_register_form";b:0;s:23:"entity_translation_sync";b:0;}s:7:"display";a:1:{s:7:"default";a:5:{s:5:"label";s:5:"above";s:4:"type";s:12:"list_default";s:8:"settings";a:0:{}s:6:"module";s:4:"list";s:6:"weight";i:11;}}s:8:"required";i:0;s:11:"description";s:0:"";s:13:"default_value";N;}',
'deleted' => '0',
))
->values(array(
'id' => '78',
'field_id' => '6',
'field_name' => 'field_boolean',
'entity_type' => 'node',
'bundle' => 'blog',
'data' => 'a:6:{s:5:"label";s:7:"Boolean";s:6:"widget";a:4:{s:4:"type";s:13:"options_onoff";s:6:"weight";s:2:"14";s:8:"settings";a:1:{s:13:"display_label";i:0;}s:6:"module";s:7:"options";}s:8:"settings";a:2:{s:18:"user_register_form";b:0;s:23:"entity_translation_sync";b:0;}s:7:"display";a:1:{s:7:"default";a:5:{s:5:"label";s:5:"above";s:4:"type";s:12:"list_default";s:8:"settings";a:0:{}s:6:"module";s:4:"list";s:6:"weight";i:12;}}s:8:"required";b:0;s:11:"description";s:0:"";}',
'deleted' => '0',
))
->execute();
$connection->schema()->createTable('field_data_body', array(
'fields' => array(
......@@ -5494,7 +5503,17 @@
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'und',
'language' => 'fr',
'delta' => '0',
'field_boolean_value' => '1',
))
->values(array(
'entity_type' => 'node',
'bundle' => 'test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'is',
'delta' => '0',
'field_boolean_value' => '1',
))
......@@ -10595,7 +10614,27 @@
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'und',
'language' => 'en',
'delta' => '0',
'field_boolean_value' => '1',
))
->values(array(
'entity_type' => 'node',
'bundle' => 'test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'fr',
'delta' => '0',
'field_boolean_value' => '1',
))
->values(array(
'entity_type' => 'node',
'bundle' => 'test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'is',
'delta' => '0',
'field_boolean_value' => '1',
))
......@@ -18873,6 +18912,22 @@
'plural' => '0',
'i18n_status' => '0',
))
->values(array(
'lid' => '687',
'translation' => 'is - Off',
'language' => 'is',
'plid' => '0',
'plural' => '0',
'i18n_status' => '0',
))
->values(array(
'lid' => '688',
'translation' => 'is - 1',
'language' => 'is',
'plid' => '0',
'plural' => '0',
'i18n_status' => '0',
))
->values(array(
'lid' => '87',
'translation' => 'is - VocabTranslate',
......@@ -74,7 +74,7 @@ protected function getEntityCounts() {
'contact_form' => 3,
'contact_message' => 0,
'editor' => 2,
'field_config' => 72,
'field_config' => 73,
'field_storage_config' => 55,
'file' => 3,
'filter_format' => 7,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment