diff --git a/src/Plugin/migrate/process/MultipleValues.php b/src/Plugin/migrate/process/MultipleValues.php new file mode 100644 index 0000000000000000000000000000000000000000..be6779a124fcf78a5bfe2684b81b1c152f3d8f9a --- /dev/null +++ b/src/Plugin/migrate/process/MultipleValues.php @@ -0,0 +1,58 @@ +<?php + +namespace Drupal\migrate_plus\Plugin\migrate\process; + +use Drupal\migrate\MigrateExecutableInterface; +use Drupal\migrate\ProcessPluginBase; +use Drupal\migrate\Row; + +/** + * Treat an array of values as a separate / individual values. + * + * @code + * process: + * field_authors: + * - + * plugin: explode + * delimiter: ', ' + * source: authors + * - + * plugin: single_value + * - + * plugin: callback + * callable: custom_sort_authors + * - + * plugin: multiple_values + * @endcode + * + * Assume the "authors" field contains comma separated author names. + * + * We split the names into multiple values and then use the "single_value" + * plugin to treat them as a single array of author names. After that, we + * pass the values through a custom sort. Callback multiple setting is false. To + * convert from a single value to multiple, use the "multiple_values" plugin. It + * will make the next plugin treat the values individually instead of an array + * of values. + * + * @MigrateProcessPlugin( + * id = "multiple_values", + * handle_multiples = TRUE + * ) + */ +class MultipleValues extends ProcessPluginBase { + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + return $value; + } + + /** + * {@inheritdoc} + */ + public function multiple() { + return TRUE; + } + +} diff --git a/src/Plugin/migrate/process/SingleValue.php b/src/Plugin/migrate/process/SingleValue.php new file mode 100644 index 0000000000000000000000000000000000000000..2583a9026cb709ab0078c6d48a722cb1d8c79b8e --- /dev/null +++ b/src/Plugin/migrate/process/SingleValue.php @@ -0,0 +1,45 @@ +<?php + +namespace Drupal\migrate_plus\Plugin\migrate\process; + +use Drupal\migrate\MigrateExecutableInterface; +use Drupal\migrate\ProcessPluginBase; +use Drupal\migrate\Row; + +/** + * Treat an array of values as a single value. + * + * @code + * process: + * field_authors: + * - + * plugin: explode + * delimiter: ', ' + * source: authors + * - + * plugin: single_value + * @endcode + * + * Assume the "authors" field contains comma separated author names. + * + * After the explode, we end up with each author name as an individual value. + * But if we want to perform a sort on all values using a callback, we will + * need to send all the values to a callable together as an array of author + * names. Calling the "single_value" plugin in such a case will combine all the + * values into a single array for the next plugin. + * + * @MigrateProcessPlugin( + * id = "single_value", + * handle_multiples = TRUE + * ) + */ +class SingleValue extends ProcessPluginBase { + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + return $value; + } + +} diff --git a/tests/src/Unit/process/MultipleValuesTest.php b/tests/src/Unit/process/MultipleValuesTest.php new file mode 100644 index 0000000000000000000000000000000000000000..60a520c0dfedfec7a11bf5200253f95b241919e1 --- /dev/null +++ b/tests/src/Unit/process/MultipleValuesTest.php @@ -0,0 +1,32 @@ +<?php + +namespace Drupal\Tests\migrate_plus\Unit\process; + +use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase; +use Drupal\migrate_plus\Plugin\migrate\process\MultipleValues; + +/** + * @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\MultipleValues + * @group migrate + */ +class MultipleValuesTest extends MigrateProcessTestCase { + + /** + * {@inheritdoc} + */ + protected function setUp() { + $this->plugin = new MultipleValues([], 'multiple_values', []); + parent::setUp(); + } + + /** + * Test input treated as multiple value output. + */ + public function testTreatAsMultiple() { + $value = ['v1', 'v2', 'v3']; + $output = $this->plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame($output, $value); + $this->assertTrue($this->plugin->multiple()); + } + +} diff --git a/tests/src/Unit/process/SingleValueTest.php b/tests/src/Unit/process/SingleValueTest.php new file mode 100644 index 0000000000000000000000000000000000000000..203b1c5cb3313fc205ad7f91d02cd2ee9f8e5b77 --- /dev/null +++ b/tests/src/Unit/process/SingleValueTest.php @@ -0,0 +1,32 @@ +<?php + +namespace Drupal\Tests\migrate_plus\Unit\process; + +use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase; +use Drupal\migrate_plus\Plugin\migrate\process\SingleValue; + +/** + * @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\SingleValue + * @group migrate + */ +class SingleValueTest extends MigrateProcessTestCase { + + /** + * {@inheritdoc} + */ + protected function setUp() { + $this->plugin = new SingleValue([], 'single_value', []); + parent::setUp(); + } + + /** + * Test input treated as single value output. + */ + public function testTreatAsSingle() { + $value = ['v1', 'v2', 'v3']; + $output = $this->plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame($output, $value); + $this->assertFalse($this->plugin->multiple()); + } + +}