Commit 0f1a598a authored by quiron's avatar quiron Committed by heddn
Browse files

Issue #2944437 by quiron, heddn: Provide an entity lookup plugin with support...

Issue #2944437 by quiron, heddn: Provide an entity lookup plugin with support for default lookup value
parent cd369d53
<?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);
}
}
<?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);
}
}
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