Create new revision of a node on edit

Last modified: April 13, 2008 - 08:25

This mini module shows how create a new revision of a node when it is updated. It checks only for a content type, but surely it can easily be extended to check for roles and such. It is quite handy to use, if you want to store certain content types in a wiki kind of way, where everyone may edit the nodes and you want to preserve the old content, if someone does a bad edit.

There are some additional debug messages that can be uncommented to see which other information can be used for the checks.


<?php
function nodeaction_action_info() {
  return array(
   
'nodeaction_create_revision_action' => array(
     
'description' => t('Create a new revision of a node'),
     
'type' => 'node',
     
'configurable' => FALSE,
     
'hooks' => array(
       
'nodeapi' => array('presave'),
        ),
      ),
  );
}

function
nodeaction_create_revision_action(&$object, $context = array()) {
    if (
$object->type == "page") {
       
drupal_set_message("A new revision of the node will be created");
       
$object->revision = 1; // alter the form data by "checking" the checkbox for a new revision
       
   
} else {
       
//drupal_set_message("wrong content type, no new revision created");
   
}
   
   
// debug messages for seeing what information are present if other checks are required
    //drupal_set_message(print_r($context, TRUE));
    //drupal_set_message(print_r($object, TRUE));
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.