Skip to content
Snippets Groups Projects
Commit 9eb96615 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2411233 by benjy, chx: Stub in migration process plugin does not do complete process

parent 490de6b3
No related branches found
No related tags found
No related merge requests found
Showing
with 62 additions and 107 deletions
......@@ -3,6 +3,11 @@
migrate.destination.*:
type: migrate_destination
label: 'Default destination'
mapping:
no_stub:
type: boolean
label: 'Whether stubbing is allowed.'
default: false
migrate.destination.config:
type: migrate_destination
......
......@@ -11,6 +11,7 @@
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Plugin\RequirementsInterface;
......@@ -274,8 +275,11 @@ protected function getProcessNormalized(array $process) {
/**
* {@inheritdoc}
*/
public function getDestinationPlugin() {
public function getDestinationPlugin($stub = FALSE) {
if (!isset($this->destinationPlugin)) {
if ($stub && !empty($this->destination['no_stub'])) {
throw new MigrateSkipRowException;
}
$this->destinationPlugin = \Drupal::service('plugin.manager.migrate.destination')->createInstance($this->destination['plugin'], $this->destination, $this);
}
return $this->destinationPlugin;
......
......@@ -125,7 +125,7 @@ public function getProcessPlugins(array $process = NULL);
* @return \Drupal\migrate\Plugin\MigrateDestinationInterface
* The destination plugin.
*/
public function getDestinationPlugin();
public function getDestinationPlugin($stub = FALSE);
/**
* Returns the initialized id_map plugin.
......
......@@ -114,10 +114,7 @@ protected function getEntity(Row $row, array $old_destination_id_values) {
$values = $row->getDestination();
// Stubs might not have the bundle specified.
if ($row->stub()) {
$bundle_key = $this->getKey('bundle');
if ($bundle_key && !isset($values[$bundle_key])) {
$values[$bundle_key] = reset($this->bundles);
}
$values = $this->processStubValues($values);
}
$entity = $this->storage->create($values);
$entity->enforceIsNew();
......@@ -137,6 +134,26 @@ protected function getEntityId(Row $row) {
return $row->getDestinationProperty($this->getKey('id'));
}
/**
* Process the stub values.
*
* @param array $values
* An array of destination values.
*
* @return array
* The processed stub values.
*/
protected function processStubValues(array $values) {
$values = array_intersect_key($values, $this->getIds());
$bundle_key = $this->getKey('bundle');
if ($bundle_key && !isset($values[$bundle_key])) {
$values[$bundle_key] = reset($this->bundles);
}
return $values;
}
/**
* Returns a specific entity key.
*
......
......@@ -11,6 +11,7 @@
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Plugin\MigratePluginManager;
......@@ -76,6 +77,7 @@ public function transform($value, MigrateExecutable $migrate_executable, Row $ro
$scalar = TRUE;
$value = array($value);
}
$this->skipOnEmpty($value);
$self = FALSE;
/** @var \Drupal\migrate\Entity\MigrationInterface[] $migrations */
$migrations = $this->migrationStorage->loadMultiple($migration_ids);
......@@ -100,7 +102,7 @@ public function transform($value, MigrateExecutable $migrate_executable, Row $ro
}
}
if (!$destination_ids && ($self && empty($this->configuration['no_stub']) || isset($this->configuration['stub_id']) || count($migrations) == 1)) {
if (!$destination_ids && ($self || isset($this->configuration['stub_id']) || count($migrations) == 1)) {
// If the lookup didn't succeed, figure out which migration will do the
// stubbing.
if ($self) {
......@@ -112,9 +114,9 @@ public function transform($value, MigrateExecutable $migrate_executable, Row $ro
else {
$migration = reset($migrations);
}
$destination_plugin = $migration->getDestinationPlugin();
$destination_plugin = $migration->getDestinationPlugin(TRUE);
// Only keep the process necessary to produce the destination ID.
$process = array_intersect_key($migration->get('process'), $destination_plugin->getIds());
$process = $migration->get('process');
// We already have the source id values but need to key them for the Row
// constructor.
$source_ids = $migration->getSourcePlugin()->getIds();
......@@ -122,7 +124,8 @@ public function transform($value, MigrateExecutable $migrate_executable, Row $ro
foreach (array_keys($source_ids) as $index => $source_id) {
$values[$source_id] = $source_id_values[$migration->id()][$index];
}
$stub_row = new Row($values, $source_ids);
$stub_row = new Row($values + $migration->get('source'), $source_ids);
$stub_row->stub(TRUE);
// Do a normal migration with the stub row.
$migrate_executable->processRow($stub_row, $process);
......@@ -146,4 +149,18 @@ public function transform($value, MigrateExecutable $migrate_executable, Row $ro
throw new MigrateSkipRowException();
}
/**
* Skip the migration process entirely if the value is FALSE.
*
* @param mixed $value
* The incoming value to transform.
*
* @throws \Drupal\migrate\MigrateSkipProcessException
*/
protected function skipOnEmpty($value) {
if (!array_filter($value)) {
throw new MigrateSkipProcessException();
}
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\MigrationTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\Migration;
/**
* Test the Migration process plugin.
*
* @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\Migration
*
* @group migrate
*/
class MigrationTest extends MigrateProcessTestCase {
/**
* {@inheritdoc}
*/
public function setUp() {
$this->plugin = new TestMigrationTest();
$this->migrationConfiguration = array('id' => 'test_migration');
parent::setUp();
}
/**
* Test the no_stub setting.
*
* @covers ::transform
*
* @expectedException \Drupal\migrate\MigrateSkipRowException
*/
public function testNoStub() {
$migration = $this->getMigration();
$this->plugin->migration = $migration;
$storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
$storage->expects($this->any())
->method('loadMultiple')
->willReturn(array($migration, $migration));
$this->plugin->setMigrationStorage($storage);
$this->plugin->setConfiguration(array(
'migration' => array('test_migration', 'test_migration2'),
'no_stub' => TRUE,
));
$this->plugin->transform('test', $this->migrateExecutable, $this->row, 'test');
}
}
class TestMigrationTest extends Migration {
public function __construct() {
}
public function setConfiguration($configuration) {
$this->configuration = $configuration;
}
public function setMigrationStorage($storage) {
$this->migrationStorage = $storage;
}
}
......@@ -9,29 +9,17 @@ source:
process:
cid: cid
pid:
-
plugin: skip_process_on_empty
source: pid
-
plugin: migration
migration: d6_comment
entity_id:
plugin: migration
migration: d6_node
source: nid
migration: d6_comment
source: pid
entity_id: nid
entity_type: 'constants/entity_type'
# field_name & comment_type is calculated in
# \Drupal\migrate_drupal\Plugin\migrate\source\d6\Comment::prepareRow()
field_name: field_name
comment_type: comment_type
subject: subject
uid:
-
plugin: skip_process_on_empty
source: uid
-
plugin: migration
migration: d6_user
uid: uid
name: name
mail: mail
homepage: homepage
......
......@@ -17,6 +17,7 @@ process:
'body/value': body
destination:
plugin: entity:block_content
no_stub: true
migration_dependencies:
required:
- d6_filter_format
......
......@@ -42,3 +42,4 @@ process:
default_value: true
destination:
plugin: entity:filter_format
no_stub: true
......@@ -7,10 +7,7 @@ source:
load:
plugin: drupal_entity
process:
uid:
plugin: migration
migration: d6_user
source: uid
uid: uid
destination:
plugin: entity:user
migration_dependencies:
......
......@@ -38,7 +38,6 @@ process:
-
plugin: migration
migration: d6_filter_format
no_stub: 1
user_picture:
plugin: d6_user_picture
source: uid
......
......@@ -151,7 +151,6 @@ protected function processTextField($field_name, $field_data, MigrationInterface
'plugin' => 'migration',
'migration' => 'd6_filter_format',
'source' => $format_key,
'no_stub' => 1,
],
];
......
......@@ -49,10 +49,8 @@ protected function setUp() {
}
$this->prepareMigrations(array(
'd6_custom_block' => array(
array(array(10), array(1)),
array(array(11), array(2)),
array(array(12), array(1)),
array(array(13), array(2)),
array(array(1), array(1)),
array(array(2), array(2)),
),
'd6_menu' => array(
array(array('menu1'), array('menu')),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment