3 namespace Drupal\migrate\Plugin\migrate\process;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\migrate\Plugin\MigrationInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
11 * Ensures the source value is made unique against an entity field.
13 * The make_unique process plugin is typically used to make the entity id
14 * unique, ensuring that migrated entity data is preserved.
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.
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
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
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.
46 * plugin: machine_name
49 * plugin: make_unique_entity_field
50 * entity_type: filter_format
55 * This will create a format machine name out the human readable name and make
62 * plugin: machine_name
65 * plugin: make_unique_entity_field
66 * entity_type: filter_format
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.
77 * @see \Drupal\migrate\Plugin\migrate\process\MakeUniqueBase
78 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
80 * @MigrateProcessPlugin(
81 * id = "make_unique_entity_field"
84 class MakeUniqueEntityField extends MakeUniqueBase implements ContainerFactoryPluginInterface {
87 * The entity type manager.
89 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
91 protected $entityTypeManager;
94 * The current migration.
96 * @var \Drupal\migrate\Plugin\MigrationInterface
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;
112 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
118 $container->get('entity_type.manager')
125 protected function exists($value) {
126 // Plugins are cached so for every run we need a new query object.
129 ->getStorage($this->configuration['entity_type'])
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)) {
144 // Just check if any such entity exists.
145 return $query->count()->execute();