Drupal - Can I programmatically create a new node revision, without the new revision becoming the "current" one?

A standard Drupal install does not allow you to create a "Pending" revision. You have two options:

  1. Programatically create a new revision but programatically revert back to the original revision (which creates an even newer revision, but it has the original content)
  2. (Recommended) Use Workbench Moderation, Revisioning or Workflow which are well thought-out solutions for version control and/or access control.

For option 1: You can add this code as a new Rule or use it in a new module

<?php
  // Programatically load the existing revision and save it
  // Taken from http://api.drupal.org/api/drupal/modules!node!node.module/function/node_save/7
  // Load the revision
  $original_revision = node_load($nid);
  $original_revision->revision = 1;
  $original_revision->log = t('Copy of the revision from %date.', array('%date' => format_date($original_revision->revision_timestamp)));

  $new_revision = node_load($nid);
  // Make any changes to the new revision here...
  $new_revision->revision = 1;
  $new_revision->log = t('Summarize your changes here');

  // Save the new revision first
  node_save($new_revision);

  // Save the original one again so that it is still the current revision
  node_save($original_revision);

  watchdog('content', '@type: reverted %title revision %revision.', array('@type' => $node_revision->type, '%title' => $node_revision->title, '%revision' => $node_revision->vid));
  drupal_set_message(t('@type %title was saved with a new revision, but reverting to original revision from %revision-date.', array('@type' => node_type_get_name($node_revision), '%title' => $node_revision->title, '%revision-date' => format_date($node_revision->revision_timestamp))));
  drupal_goto('node/' . $node_revision->nid . '/revisions');
?>

For option 2: I would recommend Workbench over Revisioning or Workflow, but each is different depending on your needs. Workbench is kind of the successor to Revisioning, and Workflow is a lot more than just version control, so it may or may not be a good fit for your needs.

Here's a quick breakdown on the differences between Workbench and Workflow.


This is a guess, but I would give the Workbench Moderation sub-module in the Workbench module a shot. I have found this much easier to set up than Workflow.

The fact that you are creating nodes in code shouldn't matter, as long as the node_save() runs as a user with the proper settings for the content type (the moderation states are handled with the node API). This may mean, though, that you need to do some session shenanigans to get things done as the proper user when Feeds runs.

Tags:

Nodes

7