1. 8.5.x core/modules/migrate/src/Plugin/migrate/process/MakeUniqueEntityField.php MakeUniqueEntityField
  2. 8.3.x core/modules/migrate/src/Plugin/migrate/process/MakeUniqueEntityField.php MakeUniqueEntityField
  3. 8.4.x core/modules/migrate/src/Plugin/migrate/process/MakeUniqueEntityField.php MakeUniqueEntityField
  4. 8.6.x core/modules/migrate/src/Plugin/migrate/process/MakeUniqueEntityField.php 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

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\process
View 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

Contains filters are case sensitive
Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function Prevents serializing any properties.
DependencySerializationTrait::__wakeup public function
MakeUniqueBase::transform public function Creates a unique value based on the source value. Overrides ProcessPluginBase::transform
MakeUniqueEntityField::$entityTypeManager protected property The entity type manager.
MakeUniqueEntityField::$migration protected property The current migration.
MakeUniqueEntityField::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
MakeUniqueEntityField::exists protected function This is a query checking the existence of some value. Overrides MakeUniqueBase::exists
MakeUniqueEntityField::__construct public function Constructs a Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin.
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use.
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.