1

We have a headless Drupal 8 content repository which we access via the REST API. I'd like Drupal to track revisions automatically for all nodes, as soon as they are created. I'm only aware of the "create new revision" checkbox on the node edit form.

How might I set Drupal to create revisions automatically?

All nodes are created programmatically via the REST API.

7

The node type setting mentioned by the other answers only applies to the node form, it does not affect programatically saved nodes.

You could try to implement hook_node_presave() and call setNewRevision(TRUE) or maybe the same in hook_node_load(). Or override the rest plugin (assuming you use that and not your own controllers).

  • Well I didn't realise that! It makes sense now you mention it though. Thanks :) – Chapabu Sep 10 '16 at 23:59
2

You should be able to set this option to be checked by default. If you go to the edit page for your content bundle (i.e. admin/structure/types/manage/article), under Publishing options, you can enforce new revisions by default.

node bundle edit form screenshot

1

I have developed a solution that works for us, based on Berdir's advice. My biggest trouble was improper implementation of hooks. I didn't know that in D8 hooks must (still) go into the .module file. Here's what we do to patch nodes via a rest route and force new revisions.

Patch via rest like this:

function patchNode(csrfToken, id, package) {
                    $.ajax({
                        url: 'http://example.com/cms/node/' + id + '?_format=hal_json',
                        method: "PATCH",
                        data: JSON.stringify(package),
                        headers: {
                            "X-CSRF-Token": csrfToken,
                            "Accept": "application/hal+json",
                            "Content-Type": "application/hal+json"
                        },
                        success: function(data, status, xhr) {
                            console.log('patchNode successful.')

                        }
                    });
                }

Create a module to implement setNewRevision via hook_node_presave. Hooks should go in to the .module file for your module.

<?php

/**
 *
 * @file
 * Force Revisions
 *
 * creates a new revision of a node on every save event 
 *
 */

use Drupal\Core\Entity;
use Drupal\node\Entity\Node;
use Drupal\Core\Entity\EntityInterface;

function force_revisions_node_presave(EntityInterface $node) {
   $node->setNewRevision(TRUE);
}
1

For Drupal 8 you can do the following in your custom .module file

use Drupal\Core\Entity\EntityInterface;

/**
 * @param \Drupal\Core\Entity\EntityInterface $node
 *
 */
function my_module_node_presave(EntityInterface $node) {
  // Force creating of new revision
  $node->setNewRevision(TRUE);
  // Set data for the revision
  $node->setRevisionLogMessage('My custom revision message.');
  $node->setRevisionUserId(123);
  $node->setRevisionCreationTime($_SERVER['REQUEST_TIME']);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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