You are here

function deploy_services_diff_entity in Deploy - Content Staging 7.3

Same name and namespace in other branches
  1. 7.2 modules/deploy_services/deploy_services.services.inc \deploy_services_diff_entity()

Helper function to diff two revisions.

Parameters

string $type: The entity type. (Ex: node)

int $id: The ID of entity.

int $new_revision: The new revision key.

int $old_revision (optional): The old revision key.

Return value

array An array with the diff and the info of entity.

1 call to deploy_services_diff_entity()
deploy_services_diff_plan in modules/deploy_services/deploy_services.services.inc
Services targeted action to show diff of all entities of a plan

File

modules/deploy_services/deploy_services.services.inc, line 252
Deploy Services module services functions.

Code

function deploy_services_diff_entity($type, $id, $new_revision, $old_revision = NULL) {
  module_load_include('inc', 'diff', 'diff.pages');
  $entity_info = entity_get_info($type);
  $id_key = $entity_info['entity keys']['id'];
  $bundle_key = NULL;
  if (!empty($entity_info['entity keys']['bundle'])) {
    $bundle_key = $entity_info['entity keys']['bundle'];
  }
  $revision_key = NULL;
  if (!empty($entity_info['entity keys']['revision'])) {
    $revision_key = $entity_info['entity keys']['revision'];
  }
  if ($new_revision) {
    $new_entity = entity_revision_load($type, $new_revision);
  }
  else {
    $new_entity = entity_load_single($type, $id);
  }
  if (!$new_entity) {
    return NULL;
  }

  // Set up the latest revision before the $new_revision.
  if (isset($entity_info['revision table']) && !$old_revision) {
    $select = db_select($entity_info['revision table'], 'rt')
      ->condition($revision_key, $new_entity->{$revision_key}, '<')
      ->condition($id_key, $new_entity->{$id_key});
    $select
      ->addExpression("MAX({$revision_key})", 'max_rev');
    $old_revision = $select
      ->execute()
      ->fetchField();
  }
  if ($old_revision) {
    $old_entity = entity_revision_load($type, $old_revision);
  }
  elseif ('file' == $type) {

    // hack because file entities are broken!
    $old_entity = (object) array(
      'fid' => $id,
      'uid' => $new_entity->uid,
      'filename' => $new_entity->filename,
      'uri' => $new_entity->uri,
      'filemime' => $new_entity->filemime,
      'filesize' => 0,
      'status' => $new_entity->status,
      'timestamp' => 0,
      'uuid' => '',
    );
  }
  else {

    /*
    If the old entity can't be found create a new empty one.
    This a hack to make sure the diff shows new entities properly.
    */
    $entity_values = array(
      $id_key => $new_entity->{$id_key},
    );
    if ($bundle_key) {
      $entity_values[$bundle_key] = $new_entity->{$bundle_key};
    }
    $old_entity = entity_create($type, $entity_values);
    field_attach_load($type, array(
      $new_entity->{$id_key} => $old_entity,
    ), FIELD_LOAD_REVISION);
  }
  $context = array(
    'entity_type' => $type,
  );
  $uri = entity_uri($type, $new_entity);
  $uri['options']['absolute'] = TRUE;
  $diff = array();
  $info = array(
    'label' => entity_label($type, $new_entity),
    'old_label' => entity_label($type, $old_entity),
    'fields' => array(),
    'url' => url($uri['path'], $uri['options']),
  );
  $entity_diff = diff_compare_entities($old_entity, $new_entity, $context);
  foreach ($entity_diff as $field => $data) {
    if ('#sorted' == $field) {
      continue;
    }
    $context = $type;
    if (!empty($bundle_key)) {
      $context = $new_entity->{$bundle_key};
    }
    $info['fields'][$field] = field_info_instance($type, $field, $context);
    list($old_field, $new_field) = diff_extract_state($data);
    $diff[$field] = diff_get_rows($old_field, $new_field, FALSE);
  }
  if (empty($diff)) {
    $diff = NULL;
  }
  return array(
    'diff' => $diff,
    'info' => $info,
  );
}