You are here

function entity_make_entity_local in Universally Unique IDentifier 7

Helper function to make an entity local (i.e. only local references).

Related topics

2 calls to entity_make_entity_local()
entity_uuid_save in ./uuid.entity.inc
Permanently saves an entity by its UUID.
_uuid_services_entity_access in uuid_services/uuid_services.module
Access callback.

File

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

Code

function entity_make_entity_local($entity_type, $entity) {
  $info = entity_get_info($entity_type);
  if (isset($info['uuid']) && $info['uuid'] == TRUE && !empty($info['entity keys']['uuid'])) {

    // Get the keys for local ID and UUID.
    $id_key = $info['entity keys']['id'];
    $uuid_key = $info['entity keys']['uuid'];

    // UUID entites must always provide a valid UUID when saving in order to do
    // the correct mapping between local and global IDs.
    if (empty($entity->{$uuid_key}) || !uuid_is_valid($entity->{$uuid_key})) {
      throw new UuidEntityException(t('Trying to save a @type entity with empty or invalid UUID.', array(
        '@type' => $info['label'],
      )));
    }

    // Fetch the local ID by its UUID.
    $ids = entity_get_id_by_uuid($entity_type, array(
      $entity->{$uuid_key},
    ));
    $id = reset($ids);

    // Set the correct local ID.
    if (empty($id)) {
      unset($entity->{$id_key});
      $entity->is_new = TRUE;
    }
    else {
      $entity->{$id_key} = $id;
      $entity->is_new = FALSE;
    }
    if (!empty($info['entity keys']['revision uuid'])) {

      // Get the keys for local revison ID and revision UUID.
      $vid_key = $info['entity keys']['revision'];
      $vuuid_key = $info['entity keys']['revision uuid'];
      $vid = NULL;

      // Fetch the local revision ID by its UUID.
      if (isset($entity->{$vuuid_key})) {

        // It's important to note that the revision UUID might be set here but
        // there might not exist a correspondant local revision ID in which case
        // we should unset the assigned revision ID to not confuse anyone with
        // revision IDs that might come from other environments.
        $vids = entity_get_id_by_uuid($entity_type, array(
          $entity->{$vuuid_key},
        ), TRUE);
        $vid = reset($vids);
      }
      if (empty($vid) && isset($entity->{$vid_key})) {
        unset($entity->{$vid_key});
      }
      elseif (!empty($vid)) {
        $entity->{$vid_key} = $vid;
      }

      // If the revision ID was unset before this (or just missing for some
      // reason) we fetch the current revision ID to build a better
      // representation of the node object we're working with.
      if ($entity_type == 'node' && !isset($entity->vid) && !$entity->is_new) {
        $entity->vid = db_select('node', 'n')
          ->condition('n.nid', $entity->nid)
          ->fields('n', array(
          'vid',
        ))
          ->execute()
          ->fetchField();
      }
    }

    // Let other modules transform UUID references to local ID references.
    $hook = 'entity_uuid_presave';
    foreach (module_implements($hook) as $module) {
      $function = $module . '_' . $hook;
      if (function_exists($function)) {
        $function($entity, $entity_type);
      }
    }
  }
  else {
    throw new UuidEntityException(t("Trying to operate on a @type entity, which doesn\\'t support UUIDs.", array(
      '@type' => $info['label'],
    )));
  }
}