Enabling Revisions on a Table With Existing Data

Last updated on
11 July 2017

Overview

Enabling revisions on an entity after the fact requires 3 basic steps:

  1. Your Entity Info must have the same info (revision table, entity keys->revision) as is required for enabling revisions on a new entity.
  2. You must insert a record into the revision table for every record in your base table.
  3. You must update the column containing the revision key on your base table to match the pkey of a valid value in the revision table (failing to add default revision entries will result in a "Page not Found" error when trying to view your content).

The following example is for a custom Entity named "dh_properties", which has several meta-data columns. It comes from the project dH.

Example: inserting revision entries for custom entity "dh_properties".
Entity Info:

  $return['dh_properties'] = array(
    'label' => t('dH Properties'),
    'entity class' => 'dHProperties',
    'controller class' => 'dHPropertiesController',
    'base table' => 'dh_properties',
    'revision table' => 'dh_properties_revision',
    'fieldable' => TRUE,
    'entity keys' => array (
	    'name' => 'pid',
      'id' => 'pid',
      'label' => 'pid',
	    'bundle' => 'bundle',
	    'revision' => 'vid',
	    'revision propvalue' => 'propvalue',
    ),
  ...

Example Query: Default Revision insert query to insert an initial value in every entity that lacks a revision.

INSERT INTO dh_properties_revision (pid, varid, propname, propcode, propvalue, startdate, enddate, featureid, entity_type, bundle)
  SELECT pid, varid, propname, propcode, propvalue, startdate, enddate, featureid, entity_type, bundle
  FROM dh_properties
  WHERE pid NOT IN (SELECT pid FROM dh_properties_revision);

UPDATE dh_properties
SET vid = revision.vid
FROM (
       SELECT max(vid) AS vid, pid
       FROM dh_properties_revision
       GROUP BY pid
     ) AS revision
WHERE dh_properties.pid = revision.pid AND dh_properties.vid = 0;

Example Form: Triggering a revision to be saved for every form submittal.

function dh_properties_form($form, &$form_state, $dh_properties, $op = 'edit') {
  $dh_properties->is_new_revision = TRUE;
...