You are here

function revisioning_node_presave in Revisioning 8

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

Implements hook_node_presave().

Called when saving, be it an edit or when creating a node.

Note that the following may be set programmatically on the $node object before calling node_save($node):

o $node->revision_operation, one of:

REVISIONING_NO_REVISION ($node->revision == $node->revision_moderation == FALSE)

REVISIONING_NEW_REVISION_NO_MODERATION ($node->revision == TRUE, $node->revision_moderation == FALSE)

REVISIONING_NEW_REVISION_WITH_MODERATION ($node->revision == $node->revision_moderation == TRUE)

o $node->revision_condition (applies only to NEW_REVISION_WITH_MODERATION):

REVISIONING_NEW_REVISION_EVERY_SAVE REVISIONING_NEW_REVISION_WHEN_NOT_PENDING

File

./revisioning.module, line 539
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_presave($node) {
  revisioning_set_node_revision_info($node);
  if (isset($node->revision_operation)) {
    $node->revision = $node->revision_operation > REVISIONING_NO_REVISION;
    $node->revision_moderation = $node->revision_operation == REVISIONING_NEW_REVISION_WITH_MODERATION;
  }
  if (!empty($node->revision_moderation) && revisioning_user_may_auto_publish($node)) {
    revisioning_set_status_message(t('Auto-publishing this revision.'));

    // Follow the default saving process making this revision current and
    // published, as opposed to pending.
    unset($node->revision_moderation);

    // This is not required for correct operation, as a revision becomes
    // pending based on vid > current_revision_id. But it looks less confusing,
    // when the "Published" box is in sync with the moderation radio buttons.
    $node->status = NODE_PUBLISHED;
    $node->auto_publish = TRUE;
  }
  if (!isset($node->nid)) {

    // New node, if moderated without Auto-publish, ignore the default Publish
    // tickbox.
    if (isset($node->revision_moderation) && $node->revision_moderation == TRUE) {
      $node->status = NODE_NOT_PUBLISHED;
    }

    // Set these for Rules, see [#1627400]
    $node->current_status = $node->status;
    $node->current_title = $node->title;
    $node->current_promote = $node->promote;
    $node->current_sticky = $node->sticky;
    if (isset($node->comment)) {

      // Comment module enabled.
      $node->current_comment = $node->comment;
    }
    return;
  }
  if (!empty($node->revision_moderation)) {

    // May want to do this for auto_publish too, to provide $node->current... to
    // other modules, as a courtesy.
    if (!isset($node->revision_condition) && !empty($node->revision) && !empty($node->is_pending) && variable_get('new_revisions_' . $node->type, REVISIONING_NEW_REVISION_WHEN_NOT_PENDING) == REVISIONING_NEW_REVISION_WHEN_NOT_PENDING) {
      revisioning_set_status_message(t('Updating existing draft, not creating new revision as this one is still pending.'));

      // To tell revisioning_node_update().
      $node->revision_condition = REVISIONING_NEW_REVISION_WHEN_NOT_PENDING;
    }
    if (isset($node->revision_condition)) {

      // Tell node_save() whether a new revision should be created.
      $node->revision = $node->revision_condition == REVISIONING_NEW_REVISION_EVERY_SAVE;
    }
    $result = db_query("SELECT status, title, comment, promote, sticky FROM {node_revision} WHERE vid = :vid", array(
      ':vid' => $node->current_revision_id,
    ));
    $current_revision = $result
      ->fetchObject();

    // Copy from {node_revision} the field values replicated on {node} before
    // handing back to node_save(). This is a side-effect of core D7's somewhat
    // "sick" table denormalisation.
    $node->current_status = $current_revision->status;
    $node->current_title = $current_revision->title;
    $node->current_promote = $current_revision->promote;
    $node->current_sticky = $current_revision->sticky;
    if (isset($current_revision->comment)) {

      // Comment module enabled.
      $node->current_comment = $current_revision->comment;
    }
  }
}