diff --git a/drafty.api.php b/drafty.api.php index 7ede483..032be03 100644 --- a/drafty.api.php +++ b/drafty.api.php @@ -4,3 +4,27 @@ * API documentation for drafty. */ +/** + * Hook to allow preserving information attached to a revision before deleted. + * + * @param string $entity_type + * The entity type. + * @param int $entity_id + * The entity id. + * @param int $revision_id + * The revision id to be deleted. + * @param int $replaced_by + * The revision that is replacing the revision being deleted. + */ +function hook_drafty_predelete_revision($entity_type, $entity_id, $revision_id, $replaced_by) { + if ($entity_type == 'node') { + // Preserve the revision timestamp. + $revision = node_load($entity_id, $revision_id); + db_update('node_revision') + ->fields(array( + 'timestamp' => $revision->revision_timestamp, + )) + ->condition('vid', $replaced_by) + ->execute(); + } +} diff --git a/drafty.module b/drafty.module index 52dd772..ea1d154 100644 --- a/drafty.module +++ b/drafty.module @@ -116,6 +116,10 @@ function drafty_field_attach_load($entity_type, $entities, $age, $options) { */ function drafty_queue_delete_revision($drafty_queue_item, &$context = NULL) { try { + // Allow other modules to preserve information on the old revision before + // it's deleted. + module_invoke_all('drafty_predelete_revision', $drafty_queue_item['entity_type'], $drafty_queue_item['entity_id'], $drafty_queue_item['revision_id'], $drafty_queue_item['replaced_by']); + entity_revision_delete($drafty_queue_item['entity_type'], $drafty_queue_item['revision_id']); if (!empty($context) && isset($context['results'])){ // Document the successful batch operation for the batch-finished function to use. @@ -325,17 +329,19 @@ class Drafty { foreach ($this->revisionsToPublish as $type => $value) { foreach ($value as $id => $vid) { unset($this->revisionsToPublish[$type][$id]); - $this->publishRevision($type, $id, $vid); + $published_revision = $this->publishRevision($type, $id, $vid); // Now that the revision is deleted, there are two identical copies of // the revision in the system. The original 'draft' revision and the // newly saved published revision. if ($delete_old_revisions) { + list (, $replaced_by) = entity_extract_ids($type, $published_revision); // Deletion must be done on a different request thread, or any other // hooks called for the revision-to-be-deleted will fail. $drafty_queue_item = array( 'entity_type' => $type, 'entity_id' => $id, 'revision_id' => $vid, + 'replaced_by' => $replaced_by, ); if ($delete_with_cron) { if (empty($queue)) {