Commit 2a3217ce authored by alexpott's avatar alexpott

Issue #2681869 by mikeryan, benjy: Provide clean way to merge configuration into migration plugins

parent e12c6850
......@@ -292,7 +292,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
$this->destinationPluginManager = $destination_plugin_manager;
$this->idMapPluginManager = $idmap_plugin_manager;
foreach ($plugin_definition as $key => $value) {
foreach (NestedArray::mergeDeep($plugin_definition, $configuration) as $key => $value) {
$this->$key = $value;
}
}
......
......@@ -86,7 +86,7 @@ protected function getDiscovery() {
* {@inheritdoc}
*/
public function createInstance($plugin_id, array $configuration = array()) {
$instances = $this->createInstances([$plugin_id], $configuration);
$instances = $this->createInstances([$plugin_id], [$plugin_id => $configuration]);
return reset($instances);
}
......
<?php
namespace Drupal\Tests\migrate\Kernel\Plugin;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests the migration plugin manager.
*
* @coversDefaultClass \Drupal\migrate\Plugin\MigratePluginManager
* @group migrate
*/
class MigrationPluginConfigurationTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'migrate',
'migrate_drupal',
// Test with a simple migration.
'ban',
];
/**
* Test merging configuration into a plugin through the plugin manager.
*
* @dataProvider mergeProvider
*/
public function testConfigurationMerge($configuration, $expected) {
/** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
$migration = $this->container->get('plugin.manager.migration')->createInstance('d7_blocked_ips', $configuration);
$source_configuration = $migration->getSourceConfiguration();
$this->assertEquals($expected, $source_configuration);
}
/**
* Provide configuration data for testing.
*/
public function mergeProvider() {
return [
// Tests adding new configuration to a migration.
[
// New configuration.
[
'source' => [
'constants' => [
'added_setting' => 'Ban them all!',
],
],
],
// Expected final source configuration.
[
'plugin' => 'd7_blocked_ips',
'constants' => [
'added_setting' => 'Ban them all!',
],
],
],
// Tests overriding pre-existing configuration in a migration.
[
// New configuration.
[
'source' => [
'plugin' => 'a_different_plugin',
],
],
// Expected final source configuration.
[
'plugin' => 'a_different_plugin',
],
],
];
}
}
......@@ -105,19 +105,18 @@ public static function run($initial_ids, $operation, $config, &$context) {
static::$numProcessed = 0;
$migration_id = reset($context['sandbox']['migration_ids']);
/** @var \Drupal\migrate\Plugin\Migration $migration */
$migration = \Drupal::service('plugin.manager.migration')->createInstance($migration_id);
$definition = \Drupal::service('plugin.manager.migration')->getDefinition($migration_id);
$configuration = [];
// @TODO, remove this in https://www.drupal.org/node/2681869.
$destination = $migration->getDestinationConfiguration();
if ($destination['plugin'] === 'entity:file') {
// @todo Find a way to avoid this in https://www.drupal.org/node/2804611.
if ($definition['destination']['plugin'] === 'entity:file') {
// Make sure we have a single trailing slash.
$source_base_path = rtrim($config['source_base_path'], '/') . '/';
$source = $migration->getSourceConfiguration();
$source['constants']['source_base_path'] = $source_base_path;
$migration->set('source', $source);
$configuration['source']['constants']['source_base_path'] = rtrim($config['source_base_path'], '/') . '/';
}
/** @var \Drupal\migrate\Plugin\Migration $migration */
$migration = \Drupal::service('plugin.manager.migration')->createInstance($migration_id, $configuration);
if ($migration) {
static::$messages = new MigrateMessageCapture();
$executable = new MigrateExecutable($migration, static::$messages);
......
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