You are here

public function EntityUuidMapper::mapToIds in Commerce Core 8.2

Maps the given entity UUIDs to entity IDs.

Parameters

string $entity_type_id: The entity type ID.

array $uuids: THe entity UUIDs.

Return value

array The entity IDs.

Overrides EntityUuidMapperInterface::mapToIds

File

src/EntityUuidMapper.php, line 47

Class

EntityUuidMapper

Namespace

Drupal\commerce

Code

public function mapToIds($entity_type_id, array $uuids) {

  // Fetch known UUIDs from the static cache.
  $ids = [];
  foreach ($uuids as $index => $uuid) {
    if (isset($this->map[$entity_type_id][$uuid])) {
      $ids[$uuid] = $this->map[$entity_type_id][$uuid];
      unset($uuids[$index]);
    }
  }

  // Map the remaining UUIDs from the database.
  if (!empty($uuids)) {
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    $id_key = $entity_type
      ->getKey('id');
    $uuid_key = $entity_type
      ->getKey('uuid');

    // Query the storage directly to avoid the performance impact of loading
    // the full entities.
    $loaded_uuids = $this->database
      ->select($entity_type
      ->getBaseTable(), 't')
      ->fields('t', [
      $uuid_key,
      $id_key,
    ])
      ->condition($uuid_key, $uuids, 'IN')
      ->execute()
      ->fetchAllKeyed(1, 0);
    foreach ($loaded_uuids as $id => $uuid) {
      $ids[$uuid] = $id;
      $this->map[$entity_type_id][$uuid] = $id;
    }
  }
  return $ids;
}