2

I added the diff module that keeps all node Revisions (it seems that I can't limit the number of Revisions to keep). Is there a code I can use where I can set a limit of the number of Revisions to keep? If so, where to add it?

1 Answer 1

2

You can use the Node Revision Restrict module for this.

The Node Revision Restrict module allows you to restrict revisions of node for each content type. This module has an admin interface to specify node revision threshold for each content type available. The possible settings include node type, the number of count provided on configuration form for each content type and more. Users with specific permissions can delete any unwanted revisions. Note that this module does not and cannot delete the current revisions of a node.

You could either use the module, or incorporate some of its code. It is a short module. Most of the code relevant to your task can be found in the node_revision_restrict_node_presave function:

/**
 * Implements of hook_presave().
 */
function node_revision_restrict_node_presave($node) {
  $node_type = $node->type;
  $nid = $node->nid;
  $node_data = db_query('SELECT * FROM {node} WHERE nid = :nid', array(':nid' => $nid));
  foreach ($node_data as $node) {
    $revision_data = node_revision_list($node);
    foreach ($revision_data as $revision) {
      $revision_ids[] = $revision->vid;
    }
    $count_to_restrict_set_value = variable_get('restrict_node_revision_number_for_' . $node_type);
    if (isset($count_to_restrict_set_value)) {
      $node_to_restrict = array_slice($revision_ids, $count_to_restrict_set_value, count($revision_ids));
      foreach ($node_to_restrict as $revision_id) {
        node_revision_delete($revision_id);
      }
    }
  }
}

In general, I prefer using a contributed module, if it provides the functionality needed, over maintaining my own code, because I won't have to worry as much about security updates, and other debugging issues, down the road.

5
  • Thanks @Ajit. I am hoping that I can use a little snippet of code, rather than adding another module for this. Jan 21, 2014 at 11:17
  • I added the relevant code snippet.
    – Anders
    Jan 21, 2014 at 19:14
  • How does your module relate to the node_revision_restrict module?
    – Hoytman
    Jun 4, 2014 at 16:14
  • Hmmm - the code snippet came from that module, verbatim.
    – Anders
    Aug 6, 2014 at 0:43
  • and how to achieve it on drupal9?
    – Matoeil
    Feb 14, 2022 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.