In my module I store some node related data in a specific table of the database. Now I want to implement the revisioning system, so I create my_table_revision with the same information and a new column called revision_id.

But, How can I know when a new revsion of the node is created? and how can I know when a node revision is reverted?

It exists something like hook_revert() or hook_revision?

  • Why can't you use Drupal's built-in node revisioning system (and the already existing modules built on it to examine and revert revisions)? – Free Radical Jun 19 '14 at 12:53
  • Yes, I want to use the Drupal's built-in node reversioning system but I have a separate database table (that needs to be reversioned). – ilazgo Jun 19 '14 at 13:00
  • @ilaz That's not how it works. Revisions refer to an entity (of which 'node' is a type); if you want to use revisions with a custom table, you need to create a custom entity with Drupal's API that uses that table (or actually 2 tables, one for data, one for revisions), for storage – Clive Jun 19 '14 at 13:04

As Clive says, you need to create a custom entity (with the API) to hold the data. Then Drupal will create the necessary tables and take care of the versioning for you.

If you need to do special things (that is not already done by Drupal's built-in versioning), you can intercept both saving of new revisions and reversals with hook_entity_update.

AFAIK, there is no simple way to tell a new revision apart from a reversal. Technically, a reversal results in a new revision with a new version ID (vid) being created, where the content of the new revision just happens to be identical to some old revision.

If it is really important for you to tell them apart, I guess you could keep a table with SHA2 (or MD5) hash signature for each revision, and look up the signature in that table to identify a reversal.

For the sake of completeness, I'll also mention that I've observed than when someone do a reversal, a log message is generated and placed in {node_revision.log} (E.g. "Copy of the revision from <em class="placeholder">Thu, 2014-06-19 13:53</em>.". Otherwise, this field is filled in with a user-written change notice (or left blank). You may be able to use this to detect reversals, but IMHO this method is not reliable as the string can easily be spoofed by a user who knows about this.

| improve this answer | |
  • I don't use revisions very often...would a reversal result in a new revision ID, or does it use the one from the original revision? If the former then maybe that's the way to distinguish? – Clive Jun 19 '14 at 13:16
  • 1
    It generates a brand new vid for reversals. Only thing that distinguishes reversals from other saves is that it generates a log message on reversals (E.g. "Copy of the revision from <em class="placeholder">Thu, 2014-06-19 13:53</em>." while the log field in {node_revision} is empty in the case of an ordinary save of revised content. So I guess you could poll the log field to detect a reversal. – Free Radical Jun 19 '14 at 13:25
  • 1
    Just to correct my self: The log field is not necessarily empty. If the change is not a reversal, it contains whatever change notice the user types in - I am just in the habit of leaving it blank. – Free Radical Jun 19 '14 at 13:34

As Free Radical and Clive says the best way to implement a revisioning system is creating a custom entity. But if you can't create a custom entity and you have to use your own table, I answer here what I have done.

So, you have your table and you have to create a table_revision with the same fields and one mor field called revision.

With the hook_node_update you can control when a node is updated, even when a node is revisioned.

So in the $node argument of the hook_node_update you have three vid (revision ID): $node->vid, $node->old_vid and $node->original->vid. The $node->vid is the new vid, the $node->original->vid is the previous vid that the node had, and the $node->old_vid is the vid of the node where you revert.

If the $node->old_vid is the same value than $node->original->vid means that the node is edited and not reverted, and if it is not the same value, it means that the node is reverted to a previous revision.

If the node is edited, you have to insert the new values in the table_revision with the '$node->vidrevision ID. But if the node is reverted, you have to get the old values from thetable_revisionwithrevision_id = $node->old_vidand then copy this values in thetableandtable_revisionwith the$node->vidas newrevision_id`.

| improve this answer | |

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.