diff --git a/core/modules/config_translation/migration_templates/i18n_user_profile_field_instance.yml b/core/modules/config_translation/migration_templates/i18n_user_profile_field_instance.yml new file mode 100644 index 0000000..ca47dd2 --- /dev/null +++ b/core/modules/config_translation/migration_templates/i18n_user_profile_field_instance.yml @@ -0,0 +1,28 @@ +id: i18n_user_profile_field_instance +label: User profile field instance configuration +migration_tags: + - Drupal 6 +source: + plugin: d6_i18n_profile_field + constants: + entity_type: user + bundle: user +process: + langcode: language + entity_type: 'constants/entity_type' + bundle: 'constants/bundle' + field_name: name + property: + plugin: static_map + source: property + map: + title: label + options: options + explanation: description + translation: translation +destination: + plugin: entity:field_config +migration_dependencies: + required: + - user_profile_field + - user_profile_field_instance diff --git a/core/modules/config_translation/src/Plugin/migrate/source/d6/i18nProfileField.php b/core/modules/config_translation/src/Plugin/migrate/source/d6/i18nProfileField.php new file mode 100644 index 0000000..63220e9 --- /dev/null +++ b/core/modules/config_translation/src/Plugin/migrate/source/d6/i18nProfileField.php @@ -0,0 +1,79 @@ +fieldTable) || empty($this->valueTable)) { + if ($this->getModuleSchemaVersion('system') >= 7000) { + $this->fieldTable = 'profile_field'; + } + else { + $this->fieldTable = 'profile_fields'; + } + } + $query = $this->select($this->fieldTable, 'pf') + ->fields('pf', array('fid', 'name')) + ->fields('i18n', array('property')) + ->fields('lt', array('lid', 'translation', 'language')); + $query->leftJoin('i18n_strings', 'i18n', 'i18n.objectid = pf.name'); + $query->leftJoin('locales_target', 'lt', 'lt.lid = i18n.lid'); + return $query; + } + + /** + * {@inheritdoc} + */ + public function fields() { + return array( + 'fid' => $this->t('Profile field ID.'), + 'language' => $this->t('Language for this field.'), + 'translation' => $this->t('Translation of either the title or explanation.'), + ); + } + + /** + * {@inheritdoc} + */ + public function getIds() { + $ids['fid']['type'] = 'integer'; + $ids['language']['type'] = 'string'; + $ids['translation']['type'] = 'string'; + return $ids; + } + +} diff --git a/core/modules/config_translation/src/Tests/Migrate/d6/Migratei18nUserProfileFieldInstanceTest.php b/core/modules/config_translation/src/Tests/Migrate/d6/Migratei18nUserProfileFieldInstanceTest.php new file mode 100644 index 0000000..ca4b4c2 --- /dev/null +++ b/core/modules/config_translation/src/Tests/Migrate/d6/Migratei18nUserProfileFieldInstanceTest.php @@ -0,0 +1,68 @@ +executeMigrations([ + 'user_profile_field', + 'user_profile_field_instance', + 'i18n_user_profile_field_instance', + ]); + } + + /** + * Tests migration of translated user profile fields. + */ + public function testUserProfileFields() { + $config_translation = \Drupal::service('language_manager')->getLanguageConfigOverride('fr', 'field.field.user.user.profile_love_migrations'); + $this->assertIdentical("J'aime les migrations", $config_translation->get('label')); + $this->assertIdentical("Si vous cochez cette case, vous aimez les migrations.", $config_translation->get('description')); + $config_translation = \Drupal::service('language_manager')->getLanguageConfigOverride('fr', 'field.field.user.user.profile_color'); + $this->assertIdentical('fr - Favorite color', $config_translation->get('label')); + $this->assertIdentical('Inscrivez votre couleur préférée', $config_translation->get('description')); + $config_translation = \Drupal::service('language_manager')->getLanguageConfigOverride('fr', 'field.field.user.user.profile_biography'); + $this->assertIdentical('fr - Biography', $config_translation->get('label')); + $this->assertIdentical('fr - Tell people a little bit about yourself', $config_translation->get('description')); + $config_translation = \Drupal::service('language_manager')->getLanguageConfigOverride('fr', 'field.field.user.user.profile_sell_address'); + $this->assertIdentical('fr - Sell your email address?', $config_translation->get('label')); + $this->assertIdentical("fr - If you check this box, we'll sell your address to spammers to help line the pockets of our shareholders. Thanks!", $config_translation->get('description')); + $config_translation = \Drupal::service('language_manager')->getLanguageConfigOverride('fr', 'field.field.user.user.profile_sold_to'); + $this->assertIdentical('fr - Sales Category', $config_translation->get('label')); + $this->assertIdentical("fr - Select the sales categories to which this user's address was sold.", $config_translation->get('description')); + $this->assertIdentical('fr - Pill spammers Fitness spammers Back\slash Forward/slash Dot.in.the.middle', $config_translation->get('options')); + $config_translation = \Drupal::service('language_manager')->getLanguageConfigOverride('fr', 'field.field.user.user.profile_bands'); + $this->assertIdentical('Mes groupes préférés', $config_translation->get('label')); + $this->assertIdentical("fr - Enter your favorite bands. When you've saved your profile, you'll be able to find other people with the same favorites.", $config_translation->get('description')); + $config_translation = \Drupal::service('language_manager')->getLanguageConfigOverride('fr', 'field.field.user.user.profile_birthdate'); + $this->assertIdentical('fr - Birthdate', $config_translation->get('label')); + $this->assertIdentical('fr - Enter your birth date and we\'ll send you a coupon.', $config_translation->get('description')); + $config_translation = \Drupal::service('language_manager')->getLanguageConfigOverride('fr', 'field.field.user.user.profile_blog'); + $this->assertIdentical('fr - Blog', $config_translation->get('label')); + $this->assertIdentical('fr - Paste the full URL, including http://, of your personal blog.', $config_translation->get('description')); + } + +} diff --git a/core/modules/config_translation/tests/src/Unit/Plugin/migrate/source/d6/i18nProfileFieldTest.php b/core/modules/config_translation/tests/src/Unit/Plugin/migrate/source/d6/i18nProfileFieldTest.php new file mode 100644 index 0000000..69c5d4e --- /dev/null +++ b/core/modules/config_translation/tests/src/Unit/Plugin/migrate/source/d6/i18nProfileFieldTest.php @@ -0,0 +1,88 @@ + 'test', + 'source' => [ + 'plugin' => 'd6_i18n_profile_field', + ], + ]; + + protected $databaseContents = [ + 'profile_fields' => [ + [ + 'fid' => 42, + 'title' => 'I love migrations', + 'name' => 'profile_love_migrations', + ], + ], + 'i18n_strings' => [ + [ + 'lid' => 10, + 'objectid' => 'profile_love_migrations', + 'type' => 'field', + 'property' => 'title', + ], + [ + 'lid' => 11, + 'objectid' => 'profile_love_migrations', + 'type' => 'field', + 'property' => 'explanation' + ] + ], + 'locales_target' => [ + [ + 'lid' => 10, + 'translation' => "J'aime les migrations.", + 'language' => 'fr', + ], + [ + 'lid' => 11, + 'translation' => 'Si vous cochez cette case, vous aimez les migrations.', + 'language' => 'fr', + ], + ], + ]; + + protected $expectedResults = [ + [ + 'property' => 'title', + 'translation' => "J'aime les migrations.", + 'language' => 'fr', + 'fid' => '42', + 'name' => 'profile_love_migrations', + ], + [ + 'property' => 'explanation', + 'translation' => 'Si vous cochez cette case, vous aimez les migrations.', + 'language' => 'fr', + 'fid' => '42', + 'name' => 'profile_love_migrations', + ], + ]; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + } + +} diff --git a/core/modules/migrate/src/Plugin/migrate/destination/EntityConfigBase.php b/core/modules/migrate/src/Plugin/migrate/destination/EntityConfigBase.php index 382f32a..f17a3e5 100644 --- a/core/modules/migrate/src/Plugin/migrate/destination/EntityConfigBase.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityConfigBase.php @@ -9,9 +9,14 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Language\LanguageManagerInterface; +use Drupal\language\ConfigurableLanguageManagerInterface; +use Drupal\migrate\Entity\MigrationInterface; use Drupal\migrate\MigrateException; use Drupal\migrate\Plugin\MigrateIdMapInterface; use Drupal\migrate\Row; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Class for importing configuration entities. @@ -26,6 +31,53 @@ class EntityConfigBase extends Entity { /** + * The language manager. + * + * @var \Drupal\Core\Language\LanguageManagerInterface + */ + protected $languageManager; + + /** + /** + * Construct a new entity. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param MigrationInterface $migration + * The migration. + * @param EntityStorageInterface $storage + * The storage for this entity type. + * @param array $bundles + * The list of bundles this entity type has. + * @param LanguageManagerInterface $language_manager + * The language manager. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, LanguageManagerInterface $language_manager) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles); + $this->languageManager = $language_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) { + $entity_type_id = static::getEntityTypeId($plugin_id); + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $migration, + $container->get('entity.manager')->getStorage($entity_type_id), + array_keys($container->get('entity.manager')->getBundleInfo($entity_type_id)), + $container->get('language_manager') + ); + } + + /** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { @@ -75,10 +127,18 @@ public function getIds() { * The row object to update from. */ protected function updateEntity(EntityInterface $entity, Row $row) { - foreach ($row->getRawDestination() as $property => $value) { - $this->updateEntityProperty($entity, explode(Row::PROPERTY_SEPARATOR, $property), $value); + if ($row->hasDestinationProperty('langcode') && ($this->languageManager instanceof ConfigurableLanguageManagerInterface)) { + // Then this is a row with a translation string. + $config = $entity->getConfigDependencyName(); + $config_override = $this->languageManager->getLanguageConfigOverride($row->getDestinationProperty('langcode'), $config); + $config_override->set(str_replace(Row::PROPERTY_SEPARATOR, '.', $row->getDestinationProperty('property')), $row->getDestinationProperty('translation')); + $config_override->save(); + } + else { + foreach ($row->getRawDestination() as $property => $value) { + $this->updateEntityProperty($entity, explode(Row::PROPERTY_SEPARATOR, $property), $value); + } } - $this->setRollbackAction($row->getIdMap()); } diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/destination/EntityFieldStorageConfig.php b/core/modules/migrate_drupal/src/Plugin/migrate/destination/EntityFieldStorageConfig.php index 50b004c..f913dc6 100644 --- a/core/modules/migrate_drupal/src/Plugin/migrate/destination/EntityFieldStorageConfig.php +++ b/core/modules/migrate_drupal/src/Plugin/migrate/destination/EntityFieldStorageConfig.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Field\FieldTypePluginManagerInterface; +use Drupal\Core\Language\LanguageManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\migrate\Entity\MigrationInterface; use Drupal\migrate\Plugin\migrate\destination\EntityFieldStorageConfig as BaseEntityFieldStorageConfig; @@ -23,6 +24,13 @@ class EntityFieldStorageConfig extends BaseEntityFieldStorageConfig { /** + * The language manager. + * + * @var \Drupal\Core\Language\LanguageManagerInterface + */ + protected $languageManager; + + /** * The field type plugin manager. * * @var \Drupal\Core\Field\FieldTypePluginManagerInterface @@ -44,11 +52,14 @@ class EntityFieldStorageConfig extends BaseEntityFieldStorageConfig { * The storage for this entity type. * @param array $bundles * The list of bundles this entity type has. + * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager + * The language manager. * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_plugin_manager * The field type plugin manager. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, FieldTypePluginManagerInterface $field_type_plugin_manager) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles); + public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, LanguageManagerInterface $language_manager, FieldTypePluginManagerInterface $field_type_plugin_manager) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $language_manager); + $this->languageManager = $language_manager; $this->fieldTypePluginManager = $field_type_plugin_manager; } @@ -64,6 +75,7 @@ public static function create(ContainerInterface $container, array $configuratio $migration, $container->get('entity.manager')->getStorage($entity_type_id), array_keys($container->get('entity.manager')->getBundleInfo($entity_type_id)), + $container->get('language_manager'), $container->get('plugin.manager.field.field_type') ); }