Commit 28a69ab3 authored by heddn's avatar heddn Committed by heddn

Issue #2901617 by jigarius, heddn, vasi: Treating arrays as single or multiple values

parent 8a296bae
<?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;
}
}
<?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;
}
}
<?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());
}
}
<?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());
}
}
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