Enabling Revisions on a Table With Existing Data

Last updated on
5 January 2017


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 your 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 = foo.vid 
from (
  select max(vid) as vid, pid 
  from dh_properties_revision 
  group by pid
) as foo
where dh_properties.pid = foo.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;