class MakeUniqueEntityField in Drupal 8
Same name and namespace in other branches
- 9 core/modules/migrate/src/Plugin/migrate/process/MakeUniqueEntityField.php \Drupal\migrate\Plugin\migrate\process\MakeUniqueEntityField
- 10 core/modules/migrate/src/Plugin/migrate/process/MakeUniqueEntityField.php \Drupal\migrate\Plugin\migrate\process\MakeUniqueEntityField
Ensures the source value is made unique against an entity field.
The make_unique process plugin is typically used to make the entity id unique, ensuring that migrated entity data is preserved.
The make_unique process plugin has two required configuration keys, entity_type and field. It's typically used with an entity destination, making sure that after saving the entity, the field value is unique. For example, if the value is foo and there is already an entity where the field value is foo, then the plugin will return foo1.
The optional configuration key postfix which will be added between the number and the original value, for example, foo_1 for postfix: _. Note that the value of postfix is ignored if the value is not changed, if it was already unique.
The optional configuration key migrated, if true, indicates that an entity will only be considered a duplicate if it was migrated by the current migration.
Available configuration keys
- entity_type: The entity type.
- field: The entity field for the given value.
- migrated: (optional) A boolean to indicate that making the field unique only occurs for migrated entities.
- start: (optional) The position at which to start reading.
- length: (optional) The number of characters to read.
- postfix: (optional) A string to insert before the numeric postfix.
Examples:
process:
format:
-
plugin: machine_name
source: name
-
plugin: make_unique_entity_field
entity_type: filter_format
field: format
This will create a format machine name out the human readable name and make sure it's unique.
process:
format:
-
plugin: machine_name
source: name
-
plugin: make_unique_entity_field
entity_type: filter_format
field: format
postfix: _
migrated: true
This will create a format machine name out the human readable name and make sure it's unique if the entity was migrated. The postfix character is inserted between the added number and the original value.
Plugin annotation
@MigrateProcessPlugin(
id = "make_unique_entity_field"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\migrate\Plugin\migrate\process\MakeUniqueBase
- class \Drupal\migrate\Plugin\migrate\process\MakeUniqueEntityField implements ContainerFactoryPluginInterface
- class \Drupal\migrate\Plugin\migrate\process\MakeUniqueBase
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of MakeUniqueEntityField
See also
\Drupal\migrate\Plugin\migrate\process\MakeUniqueBase
\Drupal\migrate\Plugin\MigrateProcessInterface
1 file declares its use of MakeUniqueEntityField
- MakeUniqueEntityFieldTest.php in core/
modules/ migrate/ tests/ src/ Unit/ process/ MakeUniqueEntityFieldTest.php
File
- core/
modules/ migrate/ src/ Plugin/ migrate/ process/ MakeUniqueEntityField.php, line 84
Namespace
Drupal\migrate\Plugin\migrate\processView source
class MakeUniqueEntityField extends MakeUniqueBase implements ContainerFactoryPluginInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The current migration.
*
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migration = $migration;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
protected function exists($value) {
// Plugins are cached so for every run we need a new query object.
$query = $this->entityTypeManager
->getStorage($this->configuration['entity_type'])
->getQuery()
->condition($this->configuration['field'], $value);
if (!empty($this->configuration['migrated'])) {
// Check if each entity is in the ID map.
$idMap = $this->migration
->getIdMap();
foreach ($query
->execute() as $id) {
$dest_id_values[$this->configuration['field']] = $id;
if ($idMap
->lookupSourceId($dest_id_values)) {
return TRUE;
}
}
return FALSE;
}
else {
// Just check if any such entity exists.
return $query
->count()
->execute();
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MakeUniqueBase:: |
public | function |
Creates a unique value based on the source value. Overrides ProcessPluginBase:: |
|
MakeUniqueEntityField:: |
protected | property | The entity type manager. | |
MakeUniqueEntityField:: |
protected | property | The current migration. | |
MakeUniqueEntityField:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
MakeUniqueEntityField:: |
protected | function |
This is a query checking the existence of some value. Overrides MakeUniqueBase:: |
|
MakeUniqueEntityField:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ProcessPluginBase:: |
public | function |
Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface:: |
3 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |