You are here

protected function EntityLookup::query in Migrate Plus 8.4

Same name and namespace in other branches
  1. 8.5 src/Plugin/migrate/process/EntityLookup.php \Drupal\migrate_plus\Plugin\migrate\process\EntityLookup::query()
  2. 8.2 src/Plugin/migrate/process/EntityLookup.php \Drupal\migrate_plus\Plugin\migrate\process\EntityLookup::query()
  3. 8.3 src/Plugin/migrate/process/EntityLookup.php \Drupal\migrate_plus\Plugin\migrate\process\EntityLookup::query()

Checks for the existence of some value.

Parameters

mixed $value: The value to query.

Return value

mixed|null Entity id if the queried entity exists. Otherwise NULL.

1 call to EntityLookup::query()
EntityLookup::transform in src/Plugin/migrate/process/EntityLookup.php
Performs the associated process.

File

src/Plugin/migrate/process/EntityLookup.php, line 272

Class

EntityLookup
This plugin looks for existing entities.

Namespace

Drupal\migrate_plus\Plugin\migrate\process

Code

protected function query($value) {

  // Entity queries typically are case-insensitive. Therefore, we need to
  // handle case sensitive filtering as a post-query step. By default, it
  // filters case insensitive. Change to true if that is not the desired
  // outcome.
  $ignoreCase = !empty($this->configuration['ignore_case']) ?: FALSE;
  $multiple = is_array($value);
  $query = $this->entityManager
    ->getStorage($this->lookupEntityType)
    ->getQuery()
    ->accessCheck($this->accessCheck)
    ->condition($this->lookupValueKey, $value, $multiple ? 'IN' : NULL);

  // Sqlite and possibly others returns data in a non-deterministic order.
  // Make it deterministic.
  if ($multiple) {
    $query
      ->sort($this->lookupValueKey, 'DESC');
  }
  if ($this->lookupBundleKey) {
    $query
      ->condition($this->lookupBundleKey, $this->lookupBundle);
  }
  $results = $query
    ->execute();
  if (empty($results)) {
    return NULL;
  }

  // By default do a case-sensitive comparison.
  if (!$ignoreCase) {

    // Returns the entity's identifier.
    foreach ($results as $k => $identifier) {
      $entity = $this->entityManager
        ->getStorage($this->lookupEntityType)
        ->load($identifier);
      $result_value = $entity instanceof ConfigEntityInterface ? $entity
        ->get($this->lookupValueKey) : $entity
        ->get($this->lookupValueKey)->value;
      if ($multiple && !in_array($result_value, $value, TRUE) || !$multiple && $result_value !== $value) {
        unset($results[$k]);
      }
    }
  }
  if ($multiple && !empty($this->destinationProperty)) {
    array_walk($results, function (&$value) {
      $value = [
        $this->destinationProperty => $value,
      ];
    });
  }
  return $multiple ? array_values($results) : reset($results);
}