Commit 8d3755c8 authored by alexpott's avatar alexpott

Issue #2509496 by chx, benjy, phenaproxima: Make migrate process plugins more...

Issue #2509496 by chx, benjy, phenaproxima: Make migrate process plugins more flexible/less boilerplate
parent fd2bc62e
......@@ -14,6 +14,12 @@
/**
* An interface for migrate process plugins.
*
* A process plugin can use any number of methods instead of (but not in
* addition to) transform with the same arguments and then the plugin
* configuration needs to provide the name of the method to be called via the
* "method" key. See \Drupal\migrate\Plugin\migrate\process\SkipOnEmpty and
* migrate.migration.d6_field_instance_widget_settings.yml for examples.
*
* @see \Drupal\migrate\Plugin\MigratePluginManager
* @see \Drupal\migrate\ProcessPluginBase
* @see \Drupal\migrate\Annotation\MigrateProcessPlugin
......
......@@ -7,28 +7,39 @@
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Drupal\migrate\MigrateSkipRowException;
/**
* If the source evaluates to empty, we skip the current row.
* If the source evaluates to empty, we skip processing or the whole row.
*
* @MigrateProcessPlugin(
* id = "skip_row_on_empty"
* id = "skip_on_empty"
* )
*/
class SkipRowOnEmpty extends ProcessPluginBase {
class SkipOnEmpty extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!$value) {
throw new MigrateSkipRowException();
}
return $value;
}
/**
* {@inheritdoc}
*/
public function process($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!$value) {
throw new MigrateSkipProcessException();
}
return $value;
}
}
<?php
/**
* @file
* Contains \Drupal\migrate\Plugin\migrate\process\d6\SkipProcessOnEmpty.
*/
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* If the value evaluates to false, skip further processing.
*
* @MigrateProcessPlugin(
* id = "skip_process_on_empty"
* )
*/
class SkipProcessOnEmpty extends ProcessPluginBase {
/**
* {@inheritdoc}
*
* Skip the rest of the processing on 0.
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!$value) {
throw new MigrateSkipProcessException();
}
return $value;
}
}
......@@ -27,6 +27,22 @@
*/
abstract class ProcessPluginBase extends PluginBase implements MigrateProcessInterface {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
// Do not call this method from children.
if (isset($this->configuration['method'])) {
if (method_exists($this, $this->configuration['method'])) {
return $this->{$this->configuration['method']}($value, $migrate_executable, $row, $destination_property);
}
throw new \BadMethodCallException(sprintf('The %s method does not exist in the %s plugin.', $this->configuration['method'], $this->pluginId));
}
else {
throw new \BadMethodCallException(sprintf('The "method" key in the plugin configuration must to be set for the %s plugin.', $this->pluginId));
}
}
/**
* {@inheritdoc}
*/
......
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\SkipOnEmptyTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\SkipOnEmpty;
/**
* Tests the skip on empty process plugin.
*
* @group migrate
* @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\SkipOnEmpty
*/
class SkipOnEmptyTest extends MigrateProcessTestCase {
/**
* @covers ::process
* @expectedException \Drupal\migrate\MigrateSkipProcessException
*/
public function testProcessSkipsOnEmpty() {
$configuration['method'] = 'process';
(new SkipOnEmpty($configuration, 'skip_on_empty', []))
->transform('', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* @covers ::process
*/
public function testProcessBypassesOnNonEmpty() {
$configuration['method'] = 'process';
$value = (new SkipOnEmpty($configuration, 'skip_on_empty', []))
->transform(' ', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, ' ');
}
/**
* @covers ::row
* @expectedException \Drupal\migrate\MigrateSkipRowException
*/
public function testRowSkipsOnEmpty() {
$configuration['method'] = 'row';
(new SkipOnEmpty($configuration, 'skip_on_empty', []))
->transform('', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* @covers ::row
*/
public function testRowBypassesOnNonEmpty() {
$configuration['method'] = 'row';
$value = (new SkipOnEmpty($configuration, 'skip_on_empty', []))
->transform(' ', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, ' ');
}
}
......@@ -8,7 +8,8 @@ process:
# Drupal 8 does not have a status but it doesn't matter; this is here to
# skip disabled blocks.
status:
plugin: skip_row_on_empty
plugin: skip_on_empty
method: row
source: status
id:
# We need something unique, so aggregator, aggregator_1 etc will do.
......
......@@ -10,7 +10,8 @@ process:
'book/weight': weight
'book/pid':
-
plugin: skip_process_on_empty
plugin: skip_on_empty
method: process
source: plid
-
plugin: migration
......
......@@ -22,7 +22,8 @@ process:
index:
- 1
-
plugin: skip_row_on_empty
plugin: skip_on_empty
method: row
entity_type: 'constants/entity_type'
bundle: type_name
view_mode:
......
......@@ -21,7 +21,8 @@ process:
index:
- 1
-
plugin: skip_row_on_empty
plugin: skip_on_empty
method: row
entity_type: 'constants/entity_type'
field_name: field_name
bundle: type_name
......
......@@ -23,7 +23,8 @@ process:
index:
- 1
-
plugin: skip_row_on_empty
plugin: skip_on_empty
method: row
bundle: type_name
form_mode: 'constants/form_mode'
field_name: field_name
......
......@@ -33,7 +33,8 @@ process:
enabled: enabled
parent:
-
plugin: skip_process_on_empty
plugin: skip_on_empty
method: process
source: plid
-
plugin: migration
......
......@@ -15,7 +15,8 @@ process:
weight: weight
parent:
-
plugin: skip_process_on_empty
plugin: skip_on_empty
method: process
source: parent
-
plugin: migration
......
......@@ -141,7 +141,10 @@ protected function processTextField($field_name, $field_data, MigrationInterface
'source' => $format_key,
'map' => [0 => NULL]
],
['plugin' => 'skip_process_on_empty'],
[
'plugin' => 'skip_on_empty',
'method' => 'process',
],
[
'plugin' => 'migration',
'migration' => 'd6_filter_format',
......
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