EntityExists.php in Drupal 10
File
core/modules/migrate/src/Plugin/migrate/process/EntityExists.php
View source
<?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;
class EntityExists extends ProcessPluginBase implements ContainerFactoryPluginInterface {
protected $storage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->storage = $storage;
}
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']));
}
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;
}
}