class MakeUniqueEntityField

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

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 81

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

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
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
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin_id of the plugin instance. 2
ProcessPluginBase::$stopPipeline protected property Determines if processing of the pipeline is stopped.
ProcessPluginBase::isPipelineStopped public function Determines if the pipeline should stop processing. Overrides MigrateProcessInterface::isPipelineStopped
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
ProcessPluginBase::reset public function Resets the internal data of a plugin. Overrides MigrateProcessInterface::reset
ProcessPluginBase::stopPipeline protected function Stops pipeline processing after this plugin finishes.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.