You are here

public function EntityUuidMapper::mapFromIds in Commerce Core 8.2

Maps the given entity IDs to entity UUIDs.

Parameters

string $entity_type_id: The entity type ID.

array $ids: THe entity IDs.

Return value

array The entity UUIDs.

Overrides EntityUuidMapperInterface::mapFromIds

File

src/EntityUuidMapper.php, line 82

Class

EntityUuidMapper

Namespace

Drupal\commerce

Code

public function mapFromIds($entity_type_id, array $ids) {

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

  // Map the remaining IDs from the database.
  if (!empty($ids)) {
    $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_ids = $this->database
      ->select($entity_type
      ->getBaseTable(), 't')
      ->fields('t', [
      $uuid_key,
      $id_key,
    ])
      ->condition($id_key, $ids, 'IN')
      ->execute()
      ->fetchAllKeyed(0, 1);
    foreach ($loaded_ids as $uuid => $id) {
      $uuids[$id] = $uuid;
      $this->map[$entity_type_id][$uuid] = $id;
    }
  }
  return $uuids;
}