Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / EntityExists.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\migrate\Plugin\MigrationInterface;
9 use Drupal\migrate\MigrateExecutableInterface;
10 use Drupal\migrate\ProcessPluginBase;
11 use Drupal\migrate\Row;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * This plugin checks if a given entity exists.
16  *
17  * Example usage with configuration:
18  * @code
19  *   field_tags:
20  *     plugin: entity_exists
21  *     source: tid
22  *     entity_type: taxonomy_term
23  * @endcode
24  *
25  * @MigrateProcessPlugin(
26  *  id = "entity_exists"
27  * )
28  */
29 class EntityExists extends ProcessPluginBase implements ContainerFactoryPluginInterface {
30
31   /**
32    * The entity storage.
33    *
34    * @var \Drupal\Core\Entity\EntityStorageInterface
35    */
36   protected $storage;
37
38   /**
39    * EntityExists constructor.
40    *
41    * @param array $configuration
42    *   A configuration array containing information about the plugin instance.
43    * @param string $plugin_id
44    *   The plugin ID.
45    * @param mixed $plugin_definition
46    *   The plugin implementation definition.
47    * @param $storage
48    *   The entity storage.
49    */
50   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $storage) {
51     parent::__construct($configuration, $plugin_id, $plugin_definition);
52     $this->storage = $storage;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
59     return new static(
60       $configuration,
61       $plugin_id,
62       $plugin_definition,
63       $container->get('entity_type.manager')->getStorage($configuration['entity_type'])
64     );
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
71     if (is_array($value)) {
72       $value = reset($value);
73     }
74
75     $entity = $this->storage->load($value);
76     if ($entity instanceof EntityInterface) {
77       return $entity->id();
78     }
79     return FALSE;
80   }
81
82 }