Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / MakeUniqueEntityField.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\migrate\Plugin\MigrationInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Ensures the source value is made unique against an entity field.
12  *
13  * The make_unique process plugin is typically used to make the entity id
14  * unique, ensuring that migrated entity data is preserved.
15  *
16  * The make_unique process plugin has two required configuration keys,
17  * entity_type and field. It's typically used with an entity destination, making
18  * sure that after saving the entity, the field value is unique. For example,
19  * if the value is foo and there is already an entity where the field value is
20  * foo, then the plugin will return foo1.
21  *
22  * The optional configuration key postfix which will be added between the number
23  * and the original value, for example, foo_1 for postfix: _. Note that the
24  * value of postfix is ignored if the value is not changed, if it was already
25  * unique.
26  *
27  * The optional configuration key migrated, if true, indicates that an entity
28  * will only be considered a duplicate if it was migrated by the current
29  * migration.
30  *
31  * Available configuration keys
32  *   - entity_type: The entity type.
33  *   - field: The entity field for the given value.
34  *   - migrated: (optional) A boolean to indicate that making the field unique
35  *     only occurs for migrated entities.
36  *   - start: (optional) The position at which to start reading.
37  *   - length: (optional) The number of characters to read.
38  *   - postfix: (optional) A string to insert before the numeric postfix.
39  *
40  * Examples:
41  *
42  * @code
43  * process:
44  *   format:
45  *   -
46  *     plugin: machine_name
47  *     source: name
48  *   -
49  *     plugin: make_unique_entity_field
50  *     entity_type: filter_format
51  *     field: format
52  *
53  * @endcode
54  *
55  * This will create a format machine name out the human readable name and make
56  * sure it's unique.
57  *
58  * @code
59  * process:
60  *   format:
61  *   -
62  *     plugin: machine_name
63  *     source: name
64  *   -
65  *     plugin: make_unique_entity_field
66  *     entity_type: filter_format
67  *     field: format
68  *     postfix: _
69  *     migrated: true
70  *
71  * @endcode
72  *
73  * This will create a format machine name out the human readable name and make
74  * sure it's unique if the entity was migrated. The postfix character is
75  * inserted between the added number and the original value.
76  *
77  * @see \Drupal\migrate\Plugin\migrate\process\MakeUniqueBase
78  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
79  *
80  * @MigrateProcessPlugin(
81  *   id = "make_unique_entity_field"
82  * )
83  */
84 class MakeUniqueEntityField extends MakeUniqueBase implements ContainerFactoryPluginInterface {
85
86   /**
87    * The entity type manager.
88    *
89    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
90    */
91   protected $entityTypeManager;
92
93   /**
94    * The current migration.
95    *
96    * @var \Drupal\migrate\Plugin\MigrationInterface
97    */
98   protected $migration;
99
100   /**
101    * {@inheritdoc}
102    */
103   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityTypeManagerInterface $entity_type_manager) {
104     parent::__construct($configuration, $plugin_id, $plugin_definition);
105     $this->migration = $migration;
106     $this->entityTypeManager = $entity_type_manager;
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
113     return new static(
114       $configuration,
115       $plugin_id,
116       $plugin_definition,
117       $migration,
118       $container->get('entity_type.manager')
119     );
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   protected function exists($value) {
126     // Plugins are cached so for every run we need a new query object.
127     $query = $this
128       ->entityTypeManager
129       ->getStorage($this->configuration['entity_type'])
130       ->getQuery()
131       ->condition($this->configuration['field'], $value);
132     if (!empty($this->configuration['migrated'])) {
133       // Check if each entity is in the ID map.
134       $idMap = $this->migration->getIdMap();
135       foreach ($query->execute() as $id) {
136         $dest_id_values[$this->configuration['field']] = $id;
137         if ($idMap->lookupSourceId($dest_id_values)) {
138           return TRUE;
139         }
140       }
141       return FALSE;
142     }
143     else {
144       // Just check if any such entity exists.
145       return $query->count()->execute();
146     }
147   }
148
149 }