Commit a007373c authored by Gábor Hojtsy's avatar Gábor Hojtsy

Issue #2961114 by quietone, heddn, Gábor Hojtsy, maxocub, masipila: Migrate D6...

Issue #2961114 by quietone, heddn, Gábor Hojtsy, maxocub, masipila: Migrate D6 i18n CCK field option translations
parent 4f89e9a9
id: d6_field_instance_option_translation
label: Field instance option configuration translation
migration_tags:
- Drupal 6
- Configuration
- Multilingual
source:
plugin: d6_field_instance_option_translation
skip_count: true
constants:
entity_type: node
property: settings
process:
# We skip field types that don't exist because they weren't migrated by the
# field migration.
field_type_exists:
-
plugin: migration_lookup
migration: d6_field
source:
- objectid
-
plugin: extract
index:
- 1
-
plugin: skip_on_empty
method: row
# Use the process from d6_field to determine the field type.
type:
plugin: field_type
source:
- type
- widget_type
map:
userreference:
userreference_select: entity_reference
userreference_buttons: entity_reference
userreference_autocomplete: entity_reference
nodereference:
nodereference_select: entity_reference
number_integer:
number: integer
optionwidgets_select: list_integer
optionwidgets_buttons: list_integer
optionwidgets_onoff: boolean
number_decimal:
number: decimal
optionwidgets_select: list_float
optionwidgets_buttons: list_float
optionwidgets_onoff: boolean
number_float:
number: float
optionwidgets_select: list_float
optionwidgets_buttons: list_float
optionwidgets_onoff: boolean
email:
email_textfield: email
filefield:
imagefield_widget: image
filefield_widget: file
fr_phone:
phone_textfield: telephone
be_phone:
phone_textfield: telephone
it_phone:
phone_textfield: telephone
el_phone:
phone_textfield: telephone
ch_phone:
phone_textfield: telephone
ca_phone:
phone_textfield: telephone
cr_phone:
phone_textfield: telephone
pa_phone:
phone_textfield: telephone
gb_phone:
phone_textfield: telephone
ru_phone:
phone_textfield: telephone
ua_phone:
phone_textfield: telephone
es_phone:
phone_textfield: telephone
au_phone:
phone_textfield: telephone
cs_phone:
phone_textfield: telephone
hu_phone:
phone_textfield: telephone
pl_phone:
phone_textfield: telephone
nl_phone:
phone_textfield: telephone
se_phone:
phone_textfield: telephone
za_phone:
phone_textfield: telephone
il_phone:
phone_textfield: telephone
nz_phone:
phone_textfield: telephone
br_phone:
phone_textfield: telephone
cl_phone:
phone_textfield: telephone
cn_phone:
phone_textfield: telephone
hk_phone:
phone_textfield: telephone
mo_phone:
phone_textfield: telephone
ph_phone:
phone_textfield: telephone
sg_phone:
phone_textfield: telephone
jo_phone:
phone_textfield: telephone
eg_phone:
phone_textfield: telephone
pk_phone:
phone_textfield: telephone
int_phone:
phone_textfield: telephone
boolean_type:
-
plugin: static_map
source: '@type'
map:
boolean: boolean
default_value: false
-
plugin: skip_on_empty
method: row
bundle:
-
plugin: migration_lookup
migration: d6_node_type
source: type_name
-
plugin: skip_on_empty
method: row
langcode:
plugin: skip_on_empty
source: language
method: row
field_name: objectid
entity_type: 'constants/entity_type'
results:
plugin: d6_field_instance_option_translation
source:
- '@type'
- global_settings
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:
- d6_node_type
- d6_field_instance
- d6_field_option_translation
id: d6_field_option_translation
label: Field option configuration translation
migration_tags:
- Drupal 6
- Configuration
- Multilingual
source:
plugin: d6_field_option_translation
skip_count: true
constants:
entity_type: node
allowed_values: settings
process:
entity_type: 'constants/entity_type'
status: active
langcode:
plugin: skip_on_empty
source: language
method: row
field_name: objectid
# Use the process from d6_field to determine the field type.
type:
plugin: field_type
source:
- type
- widget_type
map:
userreference:
userreference_select: entity_reference
userreference_buttons: entity_reference
userreference_autocomplete: entity_reference
nodereference:
nodereference_select: entity_reference
number_integer:
number: integer
optionwidgets_select: list_integer
optionwidgets_buttons: list_integer
optionwidgets_onoff: boolean
number_decimal:
number: decimal
optionwidgets_select: list_float
optionwidgets_buttons: list_float
optionwidgets_onoff: boolean
number_float:
number: float
optionwidgets_select: list_float
optionwidgets_buttons: list_float
optionwidgets_onoff: boolean
email:
email_textfield: email
filefield:
imagefield_widget: image
filefield_widget: file
fr_phone:
phone_textfield: telephone
be_phone:
phone_textfield: telephone
it_phone:
phone_textfield: telephone
el_phone:
phone_textfield: telephone
ch_phone:
phone_textfield: telephone
ca_phone:
phone_textfield: telephone
cr_phone:
phone_textfield: telephone
pa_phone:
phone_textfield: telephone
gb_phone:
phone_textfield: telephone
ru_phone:
phone_textfield: telephone
ua_phone:
phone_textfield: telephone
es_phone:
phone_textfield: telephone
au_phone:
phone_textfield: telephone
cs_phone:
phone_textfield: telephone
hu_phone:
phone_textfield: telephone
pl_phone:
phone_textfield: telephone
nl_phone:
phone_textfield: telephone
se_phone:
phone_textfield: telephone
za_phone:
phone_textfield: telephone
il_phone:
phone_textfield: telephone
nz_phone:
phone_textfield: telephone
br_phone:
phone_textfield: telephone
cl_phone:
phone_textfield: telephone
cn_phone:
phone_textfield: telephone
hk_phone:
phone_textfield: telephone
mo_phone:
phone_textfield: telephone
ph_phone:
phone_textfield: telephone
sg_phone:
phone_textfield: telephone
jo_phone:
phone_textfield: telephone
eg_phone:
phone_textfield: telephone
pk_phone:
phone_textfield: telephone
int_phone:
phone_textfield: telephone
results:
plugin: d6_field_option_translation
source:
- '@type'
- global_settings
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:
- d6_field
<?php
namespace Drupal\field\Plugin\migrate\process\d6;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* Determines the settings property and translation for boolean fields.
*
* @MigrateProcessPlugin(
* id = "d6_field_instance_option_translation",
* handle_multiples = TRUE
* )
*/
class FieldInstanceOptionTranslation extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
list($field_type, $global_settings) = $value;
$option_key = 0;
$translation = '';
if (isset($global_settings['allowed_values'])) {
$list = explode("\n", $global_settings['allowed_values']);
$list = array_map('trim', $list);
$list = array_filter($list, 'strlen');
switch ($field_type) {
case 'boolean';
$option = preg_replace('/^option_/', '', $row->getSourceProperty('property'));
for ($i = 0; $i < 2; $i++) {
$value = $list[$i];
$tmp = explode("|", $value);
$original_option_key = isset($tmp[0]) ? $tmp[0] : NULL;
$option_key = ($i === 0) ? 'off_label' : 'on_label';
// Find property with name matching the original option.
if ($option == $original_option_key) {
$translation = $row->getSourceProperty('translation');
break;
}
}
break;
default:
}
}
return ['settings.' . $option_key, $translation];
}
}
<?php
namespace Drupal\field\Plugin\migrate\process\d6;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* Determines the allowed values translation for select lists.
*
* @MigrateProcessPlugin(
* id = "d6_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($field_type, $global_settings) = $value;
$allowed_values = '';
$i = 0;
if (isset($global_settings['allowed_values'])) {
$list = explode("\n", $global_settings['allowed_values']);
$list = array_map('trim', $list);
$list = array_filter($list, 'strlen');
switch ($field_type) {
case 'list_string':
case 'list_integer':
case 'list_float':
// Remove the prefix used in the i18n_strings table for field options
// to get the option value.
$option = preg_replace('/^option_/', '', $row->getSourceProperty('property'));
$i = 0;
foreach ($list as $allowed_value) {
// Get the key for this allowed value which may be a key|label pair
// or or just key.
$value = explode("|", $allowed_value);
if (isset($value[0]) && ($value[0] == $option)) {
$allowed_values = ['label' => $row->getSourceProperty('translation')];
break;
}
$i++;
}
break;
default:
}
}
return ["settings.allowed_values.$i", $allowed_values];
}
}
<?php
namespace Drupal\field\Plugin\migrate\source\d6;
/**
* Gets field instance option label translations.
*
* @MigrateSource(
* id = "d6_field_instance_option_translation",
* source_module = "i18ncck"
* )
*/
class FieldInstanceOptionTranslation extends FieldOptionTranslation {
/**
* {@inheritdoc}
*/
public function query() {
$query = parent::query();
$query->join('content_node_field_instance', 'cnfi', 'cnf.field_name = cnfi.field_name');
$query->addField('cnfi', 'type_name');
return $query;
}
/**
* {@inheritdoc}
*/
public function fields() {
$fields = [
'type_name' => $this->t('Type (article, page, ....)'),
];
return parent::fields() + $fields;
}
}
<?php
namespace Drupal\field\Plugin\migrate\source\d6;
/**
* Gets field option label translations.
*
* @MigrateSource(
* id = "d6_field_option_translation",
* source_module = "i18ncck"
* )
*/
class FieldOptionTranslation extends Field {
/**
* {@inheritdoc}
*/
public function query() {
// Get the fields that have field options translations.
$query = $this->select('i18n_strings', 'i18n')
->fields('i18n')
->fields('lt', [
'translation',
'language',
'plid',
'plural',
'i18n_status',
])
->condition('i18n.type', 'field')
->condition('property', 'option\_%', 'LIKE')
->isNotNull('translation');
$query->leftJoin('locales_target', 'lt', 'lt.lid = i18n.lid');
$query->leftjoin('content_node_field', 'cnf', 'cnf.field_name = i18n.objectid');
$query->addField('cnf', 'field_name');
$query->addField('cnf', 'global_settings');
// Minimise changes to the d6_field_option_translation.yml, which is copied
// from d6_field.yml, by ensuring the 'type' property is from
// content_node_field table.
$query->addField('cnf', 'type');
$query->addField('i18n', 'type', 'i18n_type');
return $query;
}
/**
* {@inheritdoc}
*/
public function fields() {
$fields = [
'property' => $this->t('Option ID.'),
'objectid' => $this->t('Object ID'),
'objectindex' => $this->t('Integer value of Object ID'),
'format' => $this->t('The input format used by this string'),
'lid' => $this->t('Source string ID'),
'language' => $this->t('Language code'),
'translation' => $this->t('Translation of the option'),
'plid' => $this->t('Parent lid'),
'plural' => $this->t('Plural index number in case of plural strings'),
];
return parent::fields() + $fields;
}
/**
* {@inheritdoc}
*/
/**
* {@inheritdoc}
*/
public function getIds() {
return parent::getIds() +
[
'language' => ['type' => 'string'],
'property' => ['type' => 'string'],
];
}
}
<?php
namespace Drupal\Tests\field\Kernel\Migrate\d6;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Migrate field instance option translations.
*
* @group migrate_drupal_6
*/
class MigrateFieldInstanceOptionTranslationTest extends MigrateDrupal6TestBase {
/**
* {@inheritdoc}
*/
public static $modules =
[
'config_translation',
'language',
'locale',
'menu_ui',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig(['node']);
$this->executeMigrations([
'language',
'd6_node_type',
'd6_field',
'd6_field_instance',
'd6_field_option_translation',
'd6_field_instance_option_translation',
]);
}
/**
* Tests migration of file variables to file.settings.yml.
*/
public function testFieldInstanceOptionTranslation() {
$language_manager = $this->container->get('language_manager');
/** @var \Drupal\language\Config\LanguageConfigOverride $config_translation */
$config_translation = $language_manager->getLanguageConfigOverride('fr', 'field.field.node.story.field_test_float_single_checkbox');
$option_translation = ['on_label' => 'fr - 1.234'];
$this->assertSame($option_translation, $config_translation->get('settings'));
$config_translation = $language_manager->getLanguageConfigOverride('zu', 'field.field.node.story.field_test_float_single_checkbox');
$option_translation = ['on_label' => 'zu - 1.234'];
$this->assertSame($option_translation, $config_translation->get('settings'));
$config_translation = $language_manager->getLanguageConfigOverride('fr', 'field.field.node.story.field_test_text_single_checkbox');
$option_translation = [
'off_label' => 'fr - Hello',
'on_label' => 'fr - Goodbye',
];
$this->assertSame($option_translation, $config_translation->get('settings'));
$config_translation = $language_manager->getLanguageConfigOverride('fr', 'field.field.node.story.field_test_text_single_checkbox2');
$option_translation = [
'off_label' => 'fr - Off',
'on_label' => 'fr - Hello',
];
$this->assertSame($option_translation, $config_translation->get('settings'));
$config_translation = $language_manager->getLanguageConfigOverride('zu', 'field.field.node.story.field_test_text_single_checkbox2');
$option_translation = ['on_label' => 'zu - Hello'];
$this->assertSame($option_translation, $config_translation->get('settings'));
}
}
<?php
namespace Drupal\Tests\field\Kernel\Migrate\d6;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Migrate field option translations.
*
* @group migrate_drupal_6
*/
class MigrateFieldOptionTranslationTest extends MigrateDrupal6TestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'config_translation',
'language',
'locale',
'menu_ui',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->executeMigrations([
'language',
'd6_field',
'd6_field_option_translation',
]);
}
/**
* Tests the Drupal 6 field to Drupal 8 migration.
*/
public function testFieldOptionTranslation() {
$language_manager = $this->container->get('language_manager');
// Test a select list with allowed values of key only.
/** @var \Drupal\language\Config\LanguageConfigOverride $config_translation */
$config_translation = $language_manager->getLanguageConfigOverride('fr', 'field.storage.node.field_test_integer_selectlist');
$allowed_values = [
1 => [
'label' => 'fr - 2341',
],
3 => [
'label' => 'fr - 4123',
],
];
$this->assertSame($allowed_values, $config_translation->get('settings.allowed_values'));
$config_translation = $language_manager->getLanguageConfigOverride('zu', 'field.storage.node.field_test_integer_selectlist');
$allowed_values = [
1 => [
'label' => 'zu - 2341',
],
];
$this->assertSame($allowed_values, $config_translation->get('settings.allowed_values'));
// Test a select list with allowed values of key|label.