You are here

class MakeUniqueEntityField in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/migrate/src/Plugin/migrate/process/MakeUniqueEntityField.php \Drupal\migrate\Plugin\migrate\process\MakeUniqueEntityField
  2. 9 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

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()
      ->accessCheck(FALSE)
      ->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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
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
MessengerTrait::$messenger protected property The messenger. 18
MessengerTrait::messenger public function Gets the messenger. 18
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
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
PluginBase::getDerivativeId public function
PluginBase::getPluginDefinition public function 2
PluginBase::getPluginId public function
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
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. 1
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.