You are here

function entity_get_id_by_uuid in Universally Unique IDentifier 7

Helper function that retrieves entity IDs by their UUIDs.

@todo Limit the query.

Parameters

string $entity_type: The entity type we should be dealing with.

array $uuids: List of UUIDs for which we should find their entity IDs. If $revision is TRUE this should be revision UUIDs instead.

bool $revision: If TRUE the revision IDs is returned instead.

Return value

array List of entity IDs keyed by their UUIDs. If $revision is TRUE revision IDs and UUIDs are returned instead.

Related topics

13 calls to entity_get_id_by_uuid()
book_entity_uuid_presave in ./uuid.core.inc
Implements hook_entity_uuid_presave().
entity_make_entity_local in ./uuid.entity.inc
Helper function to make an entity local (i.e. only local references).
entity_property_uuid_to_id in ./uuid.entity.inc
Helper function to change entity properties from UUID to ID.
entity_uuid_delete in ./uuid.entity.inc
Permanently delete the given entity by its UUID.
entity_uuid_load in ./uuid.entity.inc
Load entities by their UUID, that only should containing UUID references.

... See full list

File

./uuid.entity.inc, line 406
Entity related functions for UUID module.

Code

function entity_get_id_by_uuid($entity_type, $uuids, $revision = FALSE) {
  if (empty($uuids)) {
    return array();
  }
  $cached_ids = entity_uuid_id_cache($entity_type, $uuids, $revision);
  if (count($cached_ids) == count($uuids)) {
    return $cached_ids;
  }
  $uuids = array_diff($uuids, $cached_ids);
  $info = entity_get_info($entity_type);

  // Some contrib entities has no support for UUID, let's skip them.
  if (empty($info['uuid'])) {
    return array();
  }

  // Find out what entity keys to use.
  if (!$revision) {
    $table = $info['base table'];
    $id_key = $info['entity keys']['id'];
    $uuid_key = $info['entity keys']['uuid'];
  }
  elseif (isset($info['revision table'])) {
    $table = $info['revision table'];
    $id_key = $info['entity keys']['revision'];
    $uuid_key = $info['entity keys']['revision uuid'];
  }
  else {
    return array();
  }

  // Get all UUIDs in one query.
  $result = db_select($table, 't')
    ->fields('t', array(
    $uuid_key,
    $id_key,
  ))
    ->condition($uuid_key, array_values($uuids), 'IN')
    ->execute()
    ->fetchAllKeyed();
  $cache =& drupal_static('entity_uuid_id_cache', array());
  $cache[$entity_type][(int) $revision] += $result;
  return $result + $cached_ids;
}