On this page
Enabling Revisions on a Table With Existing Data
Last updated on
11 July 2017
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
Overview
Enabling revisions on an entity after the fact requires 3 basic steps:
- Your Entity Info must have the same info (revision table, entity keys->revision) as is required for enabling revisions on a new entity.
- You must insert a record into the revision table for every record in your base table.
- 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;
...
Help improve this page
Page status: No known problems
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion
Still on Drupal 7? Security support for Drupal 7 ended on 5 January 2025. Please visit our Drupal 7 End of Life resources page to review all of your options.