Issue #2872793 by peaton, rakesh.gectcr, phenaproxima, heddn, vasi: Create Entity Exists Process Plugin
parent
1e04b3623f
commit
4cdb87fc8a
|
|
@ -0,0 +1,82 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\migrate\Plugin\migrate\process;
|
||||||
|
|
||||||
|
use Drupal\Core\Entity\EntityInterface;
|
||||||
|
use Drupal\Core\Entity\EntityStorageInterface;
|
||||||
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||||
|
use Drupal\migrate\Plugin\MigrationInterface;
|
||||||
|
use Drupal\migrate\MigrateExecutableInterface;
|
||||||
|
use Drupal\migrate\ProcessPluginBase;
|
||||||
|
use Drupal\migrate\Row;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This plugin checks if a given entity exists.
|
||||||
|
*
|
||||||
|
* Example usage with configuration:
|
||||||
|
* @code
|
||||||
|
* field_tags:
|
||||||
|
* plugin: entity_exists
|
||||||
|
* source: tid
|
||||||
|
* entity_type: taxonomy_term
|
||||||
|
* @endcode
|
||||||
|
*
|
||||||
|
* @MigrateProcessPlugin(
|
||||||
|
* id = "entity_exists"
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
class EntityExists extends ProcessPluginBase implements ContainerFactoryPluginInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The entity storage.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||||
|
*/
|
||||||
|
protected $storage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EntityExists constructor.
|
||||||
|
*
|
||||||
|
* @param array $configuration
|
||||||
|
* A configuration array containing information about the plugin instance.
|
||||||
|
* @param string $plugin_id
|
||||||
|
* The plugin ID.
|
||||||
|
* @param mixed $plugin_definition
|
||||||
|
* The plugin implementation definition.
|
||||||
|
* @param $storage
|
||||||
|
* The entity storage.
|
||||||
|
*/
|
||||||
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $storage) {
|
||||||
|
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||||
|
$this->storage = $storage;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
|
||||||
|
return new static(
|
||||||
|
$configuration,
|
||||||
|
$plugin_id,
|
||||||
|
$plugin_definition,
|
||||||
|
$container->get('entity_type.manager')->getStorage($configuration['entity_type'])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
|
||||||
|
if (is_array($value)) {
|
||||||
|
$value = reset($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
$entity = $this->storage->load($value);
|
||||||
|
if ($entity instanceof EntityInterface) {
|
||||||
|
return $entity->id();
|
||||||
|
}
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\Tests\migrate\Kernel\Plugin;
|
||||||
|
|
||||||
|
use Drupal\KernelTests\KernelTestBase;
|
||||||
|
use Drupal\migrate\MigrateExecutableInterface;
|
||||||
|
use Drupal\migrate\Row;
|
||||||
|
use Drupal\user\Entity\User;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the EntityExists process plugin.
|
||||||
|
*
|
||||||
|
* @group migrate
|
||||||
|
*/
|
||||||
|
class EntityExistsTest extends KernelTestBase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public static $modules = ['migrate', 'system', 'user'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
$this->installSchema('system', ['sequences']);
|
||||||
|
$this->installEntitySchema('user');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the EntityExists plugin.
|
||||||
|
*/
|
||||||
|
public function testEntityExists() {
|
||||||
|
$user = User::create([
|
||||||
|
'name' => $this->randomString(),
|
||||||
|
]);
|
||||||
|
$user->save();
|
||||||
|
$uid = $user->id();
|
||||||
|
|
||||||
|
$plugin = \Drupal::service('plugin.manager.migrate.process')
|
||||||
|
->createInstance('entity_exists', [
|
||||||
|
'entity_type' => 'user',
|
||||||
|
]);
|
||||||
|
$executable = $this->prophesize(MigrateExecutableInterface::class)->reveal();
|
||||||
|
$row = new Row();
|
||||||
|
|
||||||
|
// Ensure that the entity ID is returned if it really exists.
|
||||||
|
$value = $plugin->transform($uid, $executable, $row, 'buffalo');
|
||||||
|
$this->assertSame($uid, $value);
|
||||||
|
|
||||||
|
// Ensure that the plugin returns FALSE if the entity doesn't exist.
|
||||||
|
$value = $plugin->transform(420, $executable, $row, 'buffalo');
|
||||||
|
$this->assertFalse($value);
|
||||||
|
|
||||||
|
// Make sure the plugin can gracefully handle an array as input.
|
||||||
|
$value = $plugin->transform([$uid, 420], $executable, $row, 'buffalo');
|
||||||
|
$this->assertSame($uid, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue