You are here

function entity_get_uuid_by_id in Universally Unique IDentifier 7

Helper function that retrieves UUIDs by their entity IDs.

@todo Limit the query.

Parameters

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

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

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

Return value

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

Related topics

4 calls to entity_get_uuid_by_id()
book_uuid_entities_features_export_entity_alter in ./uuid.core.inc
Implements hook_entity_uuid_load().
entity_property_id_to_uuid in ./uuid.entity.inc
Helper function to change entity properties from ID to UUID.
taxonomy_entity_uuid_load in ./uuid.core.inc
Implements hook_entity_uuid_load().
UUIDEntityTestCase::testEntityApiFunctions in ./uuid.test
Tests Entity API's UUID functions.

File

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

Code

function entity_get_uuid_by_id($entity_type, $ids, $revision = FALSE) {
  if (empty($ids)) {
    return array();
  }
  $cached_ids = array_flip(entity_uuid_id_cache($entity_type, $ids, $revision));
  if (count($cached_ids) == count($ids)) {
    return $cached_ids;
  }
  $ids = array_diff($ids, $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(
    $id_key,
    $uuid_key,
  ))
    ->condition($id_key, array_values($ids), 'IN')
    ->execute()
    ->fetchAllKeyed();
  $cache =& drupal_static('entity_uuid_id_cache', array());
  $cache[$entity_type][(int) $revision] += array_flip($result);
  return $result + $cached_ids;
}