You are here

function revisioning_node_update in Revisioning 8

Same name and namespace in other branches
  1. 7 revisioning.module \revisioning_node_update()

Implements hook_node_update().

Note: $node->revision_moderation and $node->revision_condition may be set programmatically prior to calling node_save(). See also: revisioning_node_pre_save().

File

./revisioning.module, line 615
Allows content to be updated and reviewed before submitting it for publication, while the current live revision remains unchanged and publicly visible until the changes have been reviewed and found fit for publication by a moderator.

Code

function revisioning_node_update($node) {
  revisioning_update_taxonomy_index($node, variable_get('revisioning_in_views_show_unpublished_content_terms', TRUE));

  // Check whether the node has a current_status property and update the
  // node->status to allow node_save()'s call to node_access_acquire_grants()
  // to create the correct node_access entry for the current_status.
  if (isset($node->current_status)) {
    $node->status = $node->current_status;
  }
  if (!empty($node->revision_moderation) && (isset($node->revision_condition) || !empty($node->revision))) {

    // Enter when revision moderation is on and revision_condition=0,1
    // Have to do this due to D7's "sick" denormalisation of node revision data.
    // Resetting the fields duplicated from new {node_revision} back to their
    // originial values to match the current revision as opposed to the latest
    // revision. The latter is done by node_save() just before it calls this
    // function.
    // By resetting {node.vid} {node.vid} < {node_revision.vid}, which makes
    // the newly created revision a pending revision in Revisioning's books.
    // Note: cannot use $node->old_vid as set by node_save(), as this refers to
    // the revision edited, which may not be the current, which is what we are
    // after here.
    db_update('node')
      ->fields(array(
      'vid' => $node->current_revision_id,
      'status' => $node->current_status,
      'title' => $node->current_title,
      // In case Comment module enabled.
      'comment' => $node->current_comment,
      'promote' => $node->current_promote,
      'sticky' => $node->current_sticky,
    ))
      ->condition('nid', $node->nid)
      ->execute();
  }

  // Generate a 'post update' event in Rules.
  module_invoke_all('revisionapi', 'post update', $node);

  // Add new revision usage records to files to prevent them being deleted.
  $fields = field_info_instances('node', $node->type);
  foreach ($fields as $field_name => $value) {
    $field_info = field_info_field($field_name);
    if ($field_info['type'] == 'file' || $field_info['type'] == 'image') {

      // See #1996412.
      $file_fields[$field_name] = $value;
    }
  }

  // Create file revision entries for files created using older versions.
  $old_node = isset($node->original) ? $node->original : NULL;
  if (isset($old_node) && !empty($file_fields)) {
    foreach ($file_fields as $file_field) {
      $old_files = field_get_items('node', $old_node, $file_field['field_name'], $old_node->language);
      if (!empty($old_files)) {
        foreach ($old_files as $old_single_file) {
          $old_file = (object) $old_single_file;
          file_usage_add($old_file, 'revisioning', 'revision', $old_node->vid);
        }
      }
    }
  }
  if (!empty($file_fields)) {
    foreach ($file_fields as $file_field) {
      $files = field_get_items('node', $node, $file_field['field_name'], $node->language);
      if (!empty($files)) {
        foreach ($files as $single_file) {
          $file = (object) $single_file;
          file_usage_add($file, 'revisioning', 'revision', $node->vid);
        }
      }
    }
  }
}