My suggestion for saving taxonomy term selections on rollback...
urbanfalcon - August 17, 2005 - 15:31
This works for 4.5 and 4.6 I do believe...it was pretty straightfoward and so far seems to be holding up well. Unfortunately, I couldn't figure out how to do it with hooks - this is a straight edit to pre-existing functions in 4.5 and 4.6's build of node.module. For folks who are heavily using tax terms (especially multiple selections of them), this is as huge help. Also, since it starts saving a tax history at implementation time, it's backward-compatible but not backward-functional.
Here are the two functions I edited with all edits included (basically just one "if" statement for each):
/**
* Create and return a new revision of the given node.
*/
function node_revision_create($node) {
global $user;
// "Revision" is the name of the field used to indicate that we have to
// create a new revision of a node.
if ($node->nid && $node->revision) {
$prev = node_load(array('nid' => $node->nid));
$node->revisions = $prev->revisions;
//get term selections and save for rollbacks
if (function_exists('taxonomy_node_form')){
$selected = taxonomy_node_get_terms($node->nid);
foreach ($selected as $term) {
$term_selections[] = $term->tid;
}
$prev->taxonomy = $term_selections;
}
unset($prev->revisions);
$node->revisions[] = array('uid' => $user->uid, 'timestamp' => time(), 'node' => $prev, 'history' => $node->history);
}
return $node;
}
/**
* Roll back to the revision with the specified revision number.
*/
function node_revision_rollback($nid, $revision) {
global $user;
if (user_access('administer nodes') || $user->uid == 1) {
$node = node_load(array('nid' => $nid));
// Extract the specified revision:
$rev = $node->revisions[$revision]['node'];
// Inherit all the past revisions:
$rev->revisions = $node->revisions;
//get term selections and save for rollbacks
if (function_exists('taxonomy_node_form')){
$selected = taxonomy_node_get_terms($node->nid);
foreach ($selected as $term) {
$term_selections[taxonomy] = $term->tid;
}
//add taxonomy selections to the node tree
$node->taxonomy = $term_selections;
taxonomy_node_save($node->nid, $term_selections);
}
// Save the original/current node:
$rev->revisions[] = array('uid' => $user->uid, 'timestamp' => time(), 'node' => $node);
// Remove the specified revision:
unset($rev->revisions[$revision]);
// Save the node:
foreach ($node as $key => $value) {
$filter[] = $key;
}
node_save($rev, $filter);
drupal_set_message(t('Rolled back to revision %revision of %title', array('%revision' => "<em>#$revision</em>", '%title' => "<em>$node->title</em>")));
drupal_goto('node/'. $nid .'/revisions');
}
}
FYI for 4.7
This doesn't work for 4.7 due to the restructuring of the revisions handling methods (new table versus serialization). However, one plus is that 4.7 carries over current taxonomy selections to rollbacks (http://drupal.org/node/44129).