diff --git a/core/modules/migrate/src/Plugin/migrate/destination/Config.php b/core/modules/migrate/src/Plugin/migrate/destination/Config.php index 0dd9fd3..b3c07c9 100644 --- a/core/modules/migrate/src/Plugin/migrate/destination/Config.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/Config.php @@ -10,6 +10,7 @@ use Drupal\Component\Plugin\DependentPluginInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\DependencyTrait; +use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\migrate\Entity\MigrationInterface; use Drupal\migrate\Row; @@ -39,6 +40,13 @@ class Config extends DestinationBase implements ContainerFactoryPluginInterface, protected $config; /** + * The language manager. + * + * @var \Drupal\Core\Language\LanguageManagerInterface + */ + protected $language_manager; + + /** * Constructs a Config destination object. * * @param array $configuration @@ -51,10 +59,13 @@ class Config extends DestinationBase implements ContainerFactoryPluginInterface, * The migration entity. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The configuration factory. + * @param \Drupal\Core\Language\ConfigurableLanguageManagerInterface $language_manager + * The language manager. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, ConfigFactoryInterface $config_factory) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition, $migration); $this->config = $config_factory->getEditable($configuration['config_name']); + $this->language_manager = $language_manager; } /** @@ -66,7 +77,8 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $migration, - $container->get('config.factory') + $container->get('config.factory'), + $container->get('language_manager') ); } @@ -74,6 +86,10 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { + if ($row->hasDestinationProperty('langcode')) { + $this->config = $this->language_manager->getLanguageConfigOverride($row->getDestinationProperty('langcode'), $this->config->getName()); + } + foreach ($row->getRawDestination() as $key => $value) { if (isset($value) || !empty($this->configuration['store null'])) { $this->config->set(str_replace(Row::PROPERTY_SEPARATOR, '.', $key), $value); diff --git a/core/modules/migrate/tests/src/Unit/destination/ConfigTest.php b/core/modules/migrate/tests/src/Unit/destination/ConfigTest.php index 363e4f1..f5e1c33 100644 --- a/core/modules/migrate/tests/src/Unit/destination/ConfigTest.php +++ b/core/modules/migrate/tests/src/Unit/destination/ConfigTest.php @@ -49,9 +49,72 @@ public function testImport() { ->disableOriginalConstructor() ->getMock(); $row->expects($this->once()) + ->method('hasDestinationProperty') + ->will($this->returnValue(FALSE)); + $row->expects($this->any()) ->method('getRawDestination') ->will($this->returnValue($source)); - $destination = new Config(array('config_name' => 'd8_config'), 'd8_config', array('pluginId' => 'd8_config'), $migration, $config_factory); + $language_manager = $this->getMockBuilder('Drupal\language\ConfigurableLanguageManagerInterface') + ->disableOriginalConstructor() + ->getMock(); + $language_manager->expects($this->never()) + ->method('getLanguageConfigOverride') + ->with('fr', 'd8_config') + ->will($this->returnValue($config)); + $destination = new Config(array('config_name' => 'd8_config'), 'd8_config', array('pluginId' => 'd8_config'), $migration, $config_factory, $language_manager); + $destination_id = $destination->import($row); + $this->assertEquals($destination_id, ['d8_config']); + } + + /** + * Test the import method. + */ + public function testLanguageImport() { + $source = array( + 'langcode' => 'mi', + ); + $migration = $this->getMockBuilder('Drupal\migrate\Entity\Migration') + ->disableOriginalConstructor() + ->getMock(); + $config = $this->getMockBuilder('Drupal\Core\Config\Config') + ->disableOriginalConstructor() + ->getMock(); + foreach ($source as $key => $val) { + $config->expects($this->once()) + ->method('set') + ->with($this->equalTo($key), $this->equalTo($val)) + ->will($this->returnValue($config)); + } + $config->expects($this->once()) + ->method('save'); + $config->expects($this->any()) + ->method('getName') + ->willReturn('d8_config'); + $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface'); + $config_factory->expects($this->once()) + ->method('getEditable') + ->with('d8_config') + ->will($this->returnValue($config)); + $row = $this->getMockBuilder('Drupal\migrate\Row') + ->disableOriginalConstructor() + ->getMock(); + $row->expects($this->once()) + ->method('hasDestinationProperty') + ->will($this->returnValue($source)); + $row->expects($this->any()) + ->method('getRawDestination') + ->will($this->returnValue($source)); + $row->expects($this->any()) + ->method('getDestinationProperty') + ->will($this->returnValue($source['langcode'])); + $language_manager = $this->getMockBuilder('Drupal\language\ConfigurableLanguageManagerInterface') + ->disableOriginalConstructor() + ->getMock(); + $language_manager->expects($this->any()) + ->method('getLanguageConfigOverride') + ->with('mi', 'd8_config') + ->will($this->returnValue($config)); + $destination = new Config(array('config_name' => 'd8_config'), 'd8_config', array('pluginId' => 'd8_config'), $migration, $config_factory, $language_manager); $destination_id = $destination->import($row); $this->assertEquals($destination_id, ['d8_config']); } 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..f7abc3f --- /dev/null +++ b/core/modules/migrate_drupal/src/Plugin/migrate/source/d6/i18nVariable.php @@ -0,0 +1,105 @@ +variables = $this->configuration['variables']; + } + + /** + * {@inheritdoc} + */ + protected function initializeIterator() { + return new \ArrayIterator($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 the values found in the source. + * A key/value pair is added for the language code. Only those values are + * returned that are actually in the database. + */ + protected function values() { + $values = []; + $result = $this->prepareQuery()->execute()->FetchAllAssoc('language'); + foreach ($result as $i18nvariable) { + $values[]['language'] = $i18nvariable->language; + } + $result = $this->prepareQuery()->execute()->FetchAll(); + foreach ($result as $i18nvariable) { + foreach ($values as $key => $value) { + if ($values[$key]['language'] === $i18nvariable->language ) { + $values[$key][$i18nvariable->name] = unserialize($i18nvariable->value); + break; + } + } + } + return $values; + } + + /** + * {@inheritdoc} + */ + public function count() { + return $this->initializeIterator()->count(); + } + + /** + * {@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', (array) $this->configuration['variables'], 'IN'); + } + + /** + * {@inheritdoc} + */ + public function getIds() { + $ids['language']['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..b9bcb99 --- /dev/null +++ b/core/modules/migrate_drupal/tests/src/Unit/source/d6/i18nVariableTest.php @@ -0,0 +1,66 @@ + 'test', + 'highWaterProperty' => array('field' => 'test'), + 'source' => [ + 'plugin' => 'i18n_variable', + 'variables' => [ + 'site_slogan', + 'site_name', + ], + ], + ]; + + /** + * Expected results from the source. + */ + protected $expectedResults = [ + [ + 'language' => 'fr', + 'site_slogan' => 'Migrate est génial', + 'site_name' => 'nom de site', + ], + [ + 'language' => 'mi', + 'site_slogan' => 'Ko whakamataku heke', + 'site_name' => 'ingoa_pae', + ] + ]; + + /** + * Database contents for tests. + */ + protected $databaseContents = [ + 'i18n_variable' => [ + 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";'), + array('name' => 'site_slogan', 'language' => 'mi', 'value' => 's:19:"Ko whakamataku heke";'), + array('name' => 'site_name', 'language' => 'mi', 'value' => 's:9:"ingoa_pae";'), + ], + ]; + +}