You are here

function uuid_entity_presave in Universally Unique IDentifier 7

Implements hook_entity_presave().

This is where all UUID-enabled entities get their UUIDs.

Related topics

File

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

Code

function uuid_entity_presave($entity, $entity_type) {
  $info = entity_get_info($entity_type);
  if (isset($info['uuid']) && $info['uuid'] == TRUE && !empty($info['entity keys']['uuid'])) {
    $uuid_key = $info['entity keys']['uuid'];
    if (empty($entity->{$uuid_key})) {
      $entity->{$uuid_key} = uuid_generate();
    }
    if (!empty($info['entity keys']['revision uuid'])) {
      $vuuid_key = $info['entity keys']['revision uuid'];

      // If this entity comes from a remote environment and have a revision UUID
      // that exists locally we should not create a new revision. Because
      // otherwise revisions won't be tracked universally.
      // TODO: Move code dependent on the uuid_services module into it's own
      // implementation of hook_entity_presave().
      if (!empty($entity->uuid_services) && isset($entity->{$vuuid_key})) {
        $vuuid_exists = (bool) entity_get_id_by_uuid($entity_type, array(
          $entity->{$vuuid_key},
        ), TRUE);
        if ($vuuid_exists) {
          $entity->revision = FALSE;
        }
      }
      if (isset($entity->revision) && $entity->revision == TRUE && empty($entity->uuid_services) || empty($entity->{$vuuid_key})) {
        $entity->{$vuuid_key} = uuid_generate();
      }
    }
  }
}