diff --git a/core/modules/migrate/config/schema/migrate.destination.schema.yml b/core/modules/migrate/config/schema/migrate.destination.schema.yml index b295741..8945f7a 100644 --- a/core/modules/migrate/config/schema/migrate.destination.schema.yml +++ b/core/modules/migrate/config/schema/migrate.destination.schema.yml @@ -16,3 +16,11 @@ migrate.destination.config: config_name: type: string label: 'Configuration name' + +migrate.destination.i18n_config: + type: migrate_destination + label: 'Config' + mapping: + config_name: + type: string + label: 'Configuration name' diff --git a/core/modules/migrate/src/Plugin/migrate/destination/i18nConfig.php b/core/modules/migrate/src/Plugin/migrate/destination/i18nConfig.php new file mode 100644 index 0000000..7930156 --- /dev/null +++ b/core/modules/migrate/src/Plugin/migrate/destination/i18nConfig.php @@ -0,0 +1,132 @@ +config_translation = $config_factory->getEditable($configuration['config_name']); + $this->language_manager = $language_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $migration, + $container->get('config.factory'), + $container->get('language_manager') + ); + } + + /** + * {@inheritdoc} + */ + public function import(Row $row, array $old_destination_id_values = array()) { + // Sort by language + foreach ($row->getRawDestination() as $property => $lang_array) { + foreach ($lang_array as $langcode => $value) { + $sorted[$langcode][$property] = $value; + } + } + // Update config by language + foreach ($sorted as $langcode => $property) { + if (isset($property) || !empty($this->configuration['store null'])) { + $this->config_translation = $this->language_manager->getLanguageConfigOverride($langcode, $this->configuration['config_name']); + foreach ($property as $key => $value) { + $this->config_translation->set(str_replace(Row::PROPERTY_SEPARATOR, '.', $key), $value); + } + $this->config_translation->save(); + } + } + + return [$this->config_translation->getName()]; + } + + /** + * {@inheritdoc} + */ + public function fields(MigrationInterface $migration = NULL) { + // @todo Dynamically fetch fields using Config Schema API. + } + + /** + * {@inheritdoc} + */ + public function getIds() { + $ids['config_name']['type'] = 'string'; + return $ids; + } + + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + $provider = explode('.', $this->config_translation->getName(), 2)[0]; + $this->addDependency('module', $provider); + return $this->dependencies; + } + +} diff --git a/core/modules/migrate_drupal/config/schema/migrate_drupal.source.schema.yml b/core/modules/migrate_drupal/config/schema/migrate_drupal.source.schema.yml index 6152f9e..96f5e65 100644 --- a/core/modules/migrate_drupal/config/schema/migrate_drupal.source.schema.yml +++ b/core/modules/migrate_drupal/config/schema/migrate_drupal.source.schema.yml @@ -151,3 +151,42 @@ migrate_entity_constant: migrate.source.md_empty: type: migrate.source.empty label: 'Empty source for migrate_drupal migrations' + +migrate.source.i18n_variable: + type: migrate_source_sql + label: 'i18n Variable' + mapping: + variables: + type: sequence + label: 'Variables' + sequence: + type: string + label: 'Variable' + constants: + type: mapping + label: 'Constants' + mapping: + entity_type: + type: string + label: 'Entity type' + id: + type: string + label: 'ID' + label: + type: label + label: 'Label' + description: + type: text + label: 'Description' + path: + type: string + label: 'Path' + plugin: + type: string + label: 'Plugin' + status: + type: boolean + label: 'Status' + slash: + type: string + label: 'Slash' diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/source/d6/i18nVariable.php b/core/modules/migrate_drupal/src/Plugin/migrate/source/d6/i18nVariable.php new file mode 100644 index 0000000..4b4c721 --- /dev/null +++ b/core/modules/migrate_drupal/src/Plugin/migrate/source/d6/i18nVariable.php @@ -0,0 +1,103 @@ +variables = $this->configuration['variables']; + } + + /** + * {@inheritdoc} + */ + + protected function initializeIterator() { + return new \ArrayIterator(array($this->values())); + } + + /** + * Return the values of the variables specified in the plugin configuration. + * + * @return array + * An associative array where the keys are the variables specified in the + * plugin configuration and the values are an assiociative array of + * langauge and the values found in the source. + */ + + protected function values() { + // Create an ID field so we can record migration in the map table. + // Arbitrarily, use the first variable name. + $values['id'] = reset($this->variables); + $results = $this->prepareQuery()->execute(); + $x = []; + foreach ($results->fetchAll() as $result) { + $x[$result->name][$result->language] = unserialize($result->value); + } + return $values + $x; + } + + /** + * {@inheritdoc} + */ + public function count() { + return intval($this->query()->countQuery()->execute()->fetchField() > 0); + } + + /** + * {@inheritdoc} + */ + public function fields() { + return array_combine($this->variables, $this->variables); + } + + /** + * {@inheritdoc} + */ + public function query() { + return $this->getDatabase() + ->select('i18n_variable', 'v') + ->fields('v') + ->condition('name', $this->variables, 'IN'); + } + + /** + * {@inheritdoc} + */ + public function getIds() { + $ids['id']['type'] = 'string'; + return $ids; + } + +} diff --git a/core/modules/migrate_drupal/tests/src/Unit/source/d6/i18nVariableTest.php b/core/modules/migrate_drupal/tests/src/Unit/source/d6/i18nVariableTest.php new file mode 100644 index 0000000..2476505 --- /dev/null +++ b/core/modules/migrate_drupal/tests/src/Unit/source/d6/i18nVariableTest.php @@ -0,0 +1,67 @@ + 'test', + 'highWaterProperty' => array('field' => 'test'), + 'source' => [ + 'plugin' => 'i18n_variable', + 'variables' => [ + 'site_slogan', + 'site_name', + ], + ], + ]; + + /** + * Expected results from the source. + */ + protected $expectedResults = [ + [ + 'id' => 'site_slogan', + 'site_slogan' => [ + 'fr' => 'Migrate est génial', + 'mi' => 'Ko whakamataku heke', + ], + 'site_name' => [ + 'fr' => 'nom de site', + 'mi' => 'ingoa_pae', + ], + ] + ]; + + /** + * Database contents for tests. + */ + protected $databaseContents = [ + 'i18n_variable' => [ + array('name' => 'site_slogan', 'language' => 'mi', 'value' => 's:19:"Ko whakamataku heke";'), + array('name' => 'site_name', 'language' => 'mi', 'value' => 's:9:"ingoa_pae";'), + array('name' => 'site_slogan', 'language' => 'fr', 'value' => 's:19:"Migrate est génial";'), + array('name' => 'site_name', 'language' => 'fr', 'value' => 's:11:"nom de site";'), + ], + ]; + +}