diff --git a/src/Plugin/migrate/process/DefaultEntityValue.php b/src/Plugin/migrate/process/DefaultEntityValue.php new file mode 100644 index 0000000000000000000000000000000000000000..78ef4cadd4682870074038db276e237c1b305392 --- /dev/null +++ b/src/Plugin/migrate/process/DefaultEntityValue.php @@ -0,0 +1,46 @@ +<?php + +namespace Drupal\migrate_plus\Plugin\migrate\process; + +use Drupal\migrate\MigrateExecutableInterface; +use Drupal\migrate\Row; + +/** + * Returns EntityLookup for a given default value if input is empty. + * + * @see \Drupal\migrate_plus\Plugin\migrate\process\EntityLookup + * + * Example usage with full configuration: + * @code + * process: + * uid: + * - + * plugin: migration_lookup + * migration: users + * source: author + * - + * plugin: default_entity_value + * entity_type: user + * value_key: name + * ignore_case: true + * default_value: editorial + * @endcode + * + * @MigrateProcessPlugin( + * id = "default_entity_value", + * handle_multiples = TRUE + * ) + */ +class DefaultEntityValue extends EntityLookup { + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + if (!empty($value)) { + return $value; + } + return parent::transform($this->configuration['default_value'], $migrate_executable, $row, $destination_property); + } + +} diff --git a/tests/src/Kernel/Plugin/migrate/process/DefaultEntityValueTest.php b/tests/src/Kernel/Plugin/migrate/process/DefaultEntityValueTest.php new file mode 100644 index 0000000000000000000000000000000000000000..35022ba171b5a2a57041a5c69e43188dbcae22ef --- /dev/null +++ b/tests/src/Kernel/Plugin/migrate/process/DefaultEntityValueTest.php @@ -0,0 +1,76 @@ +<?php + +namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate\process; + +use Drupal\KernelTests\KernelTestBase; +use Drupal\migrate\MigrateExecutableInterface; +use Drupal\migrate\Plugin\MigrateDestinationInterface; +use Drupal\migrate\Plugin\MigrationInterface; +use Drupal\migrate\Row; +use Drupal\Tests\user\Traits\UserCreationTrait; + +/** + * Tests the default_entity_value plugin. + * + * @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\DefaultEntityValue + * @group migrate_plus + */ +class DefaultEntityValueTest extends KernelTestBase { + + use UserCreationTrait; + + /** + * {@inheritdoc} + */ + public static $modules = [ + 'migrate_plus', + 'migrate', + 'user', + 'system', + ]; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + $this->installSchema('system', ['sequences']); + $this->installEntitySchema('user'); + } + + /** + * Tests the lookup when the value is empty. + * + * @covers ::transform + */ + public function testDefaultEntityValue() { + // Create a user. + $editorial_user = $this->createUser([], 'editorial'); + $journalist_user = $this->createUser([], 'journalist'); + // Setup test migration objects. + $migration_prophecy = $this->prophesize(MigrationInterface::class); + $migrate_destination_prophecy = $this->prophesize(MigrateDestinationInterface::class); + $migrate_destination_prophecy->getPluginId()->willReturn('user'); + $migrate_destination = $migrate_destination_prophecy->reveal(); + $migration_prophecy->getDestinationPlugin()->willReturn($migrate_destination); + $migration_prophecy->getProcess()->willReturn([]); + $migration = $migration_prophecy->reveal(); + $configuration = [ + 'entity_type' => 'user', + 'value_key' => 'name', + 'ignore_case' => TRUE, + 'default_value' => 'editorial', + ]; + $plugin = \Drupal::service('plugin.manager.migrate.process') + ->createInstance('default_entity_value', $configuration, $migration); + $executable = $this->prophesize(MigrateExecutableInterface::class)->reveal(); + $row = new Row(); + // Check the case default value is not used. + $value = $plugin->transform($journalist_user->id(), $executable, $row, 'name'); + $this->assertSame($journalist_user->id(), $value); + // Check the default value is found. + $value = $plugin->transform('', $executable, $row, 'name'); + $this->assertSame($editorial_user->id(), $value); + } + +}