function MakeUniqueEntityField::exists

Same name in other branches
  1. 9 core/modules/migrate/src/Plugin/migrate/process/MakeUniqueEntityField.php \Drupal\migrate\Plugin\migrate\process\MakeUniqueEntityField::exists()
  2. 8.9.x core/modules/migrate/src/Plugin/migrate/process/MakeUniqueEntityField.php \Drupal\migrate\Plugin\migrate\process\MakeUniqueEntityField::exists()
  3. 10 core/modules/migrate/src/Plugin/migrate/process/MakeUniqueEntityField.php \Drupal\migrate\Plugin\migrate\process\MakeUniqueEntityField::exists()

Overrides MakeUniqueBase::exists

File

core/modules/migrate/src/Plugin/migrate/process/MakeUniqueEntityField.php, line 123

Class

MakeUniqueEntityField
Ensures the source value is made unique against an entity field.

Namespace

Drupal\migrate\Plugin\migrate\process

Code

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();
    }
}

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