5

I made an update to a site for global search and replace of text across nodes and was too far-reaching. For a specific content type I need to revert my Node update of ~500 nodes.

I found this solution in the VBO issue queue: https://www.drupal.org/node/1166630

But in evaluating this code a new revision is made by it is not set as the current revision of the node. If I visit the node revisions page and invoke node_revision_revert_confirm_submit a new current_revision is set for the node.

enter image description here Code I'm trying:

/**
 * Implementation of hook_action_info().
 */
/**
 * Implements hook_action_info().
 */
function revert_last_revision_action_info() {
  $action = array(
    'REVERT_LAST_REVISION' => array(
      'label'        => t('Revert to Last Revision'),
      'type'         => 'node',
      'configurable' => FALSE,
      'triggers'     => array('any'),
    ),
  );
  return $action;
}

/**
 * Action function for REVERT_LAST_REVISION.
 */
function REVERT_LAST_REVISION(&$entity, $context = array()) {
  $node = $entity;
  $revision_list = node_revision_list($node);
  unset($revision_list[$node->vid]);
  if (count($revision_list) > 0) {
    $last_revision = max(array_keys($revision_list));
    $node_last_revision = node_load($node->nid, $last_revision);
    $node_last_revision->revision = TRUE;
    $node_last_revision->log = "Reverted via bulk operation action."; // Log message
    dpm($node_last_revision, __FUNCTION__);
    node_save($node_last_revision);
  }
}

Using devel the $node arrays look the same so I'm confused as to why my revert revision code snippet is not making the new revision the current_revision. The only thing node_revision_revert_confirm_submit is doing is setting $node->revision = TRUE and calling node_save().

Anyone have any ideas?

EDIT: 11/16/2015

As others have mentioned perhaps this has to do with how VBO is initiating the action.

I've also tried the below code but the Node isn't saved.

/**
 * Implements hook_action_info().
 */
function revert_last_revision_action_info() {
  $action = array(
    'REVERT_LAST_REVISION' => array(
      'label'        => t('Revert to Last Revision'),
      'type'         => 'node',
      'configurable' => FALSE,
      'triggers'     => array('any'),
      'behavior'     => array('changes_property'),
      'vbo_configurable' => FALSE,
      'pass rows'        => TRUE,
    ),
  );
  return $action;
}

/**
 * Action function for REVERT_LAST_REVISION.
 */
function REVERT_LAST_REVISION(&$entity, $context = array()) {
  $revision_list = node_revision_list($entity);
  unset($revision_list[$entity->vid]);
  if (count($revision_list) > 0) {
    $last_revision = max(array_keys($revision_list));
    $entity = node_load($entity->nid, $last_revision, TRUE);
    $entity->revision = TRUE;
    $entity->log = "Reverted via bulk operation action.";
  }
}

2 Answers 2

5

After much debugging this appears to work. This code appears to revert to the most recent revision properly and does not cause a 2nd node save to occur:

/**
 * Implements hook_action_info().
 */
function revert_last_revision_action_info() {
  $action = array(
    'REVERT_LAST_REVISION' => array(
      'label'        => t('Revert to Last Revision'),
      'type'         => 'node',
      'configurable' => FALSE,
      'triggers'     => array('any'),
      'behavior'     => array('view_property'),
    ),
  );
  return $action;
}

/**
 * Action function for REVERT_LAST_REVISION.
 */
function REVERT_LAST_REVISION(&$entity, $context = array()) {
  $revision_list = node_revision_list($entity);
  unset($revision_list[$entity->vid]);
  if (count($revision_list) > 0) {
    $last_revision = max(array_keys($revision_list));
    $entity = entity_revision_load('node', $last_revision);
    $entity->revision = TRUE;
    $entity->log = "Reverted via bulk operation action.";
    entity_save('node', $entity);
  }
}

Does anyone know what is causing the final node save in ViewsBulkOperationsAction::execute to not fire? I can't tell if it's because of the action info behaviors not set as: save, pre-save, or changes_property values. Or perhaps it's the usage of entity_save here instead of a node_save ...

I've debugged this with a few nodes using Batch and single nodes.

0
2
+100

The difference is that the node is saved twice: Once in the custom action and once from the the Views Bulk Operations action. Because the $entity object hasn't been changed, entity_save() saves the previous version of the node, thus setting the node back to previous revision instead of the newly created one.

1
  • Hmmm ... I swear I've tried the code removing the "local" node and local save .... But then my node wasn't saved .... Grrrrr
    – tenken
    Commented Nov 14, 2015 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.