You are here

public static function Nodes::rollbackImport in Hook Update Deploy Tools 7

Rolls back a revision or node creation.

Parameters

string $op: The crud op that was performed.

object $node: The node object to be rolled back.

int $rollback_to_vid: The revision id to roll back to.

1 call to Nodes::rollbackImport()
Nodes::processOne in src/Nodes.php
Validated Updates/Imports one page from the contents of an import file.

File

src/Nodes.php, line 275

Class

Nodes
Public method for changing nodes programatically.

Namespace

HookUpdateDeployTools

Code

public static function rollbackImport($op, $node, $rollback_to_vid) {
  if ($op === 'create') {

    // Op was a create, so delete the node if there was one created.
    if (!empty($node->nid)) {

      // The presence of nid indicates one was created, so delete it.
      node_delete($node->nid);
      $msg = "Node @nid created but failed validation and was deleted.";
      $variables = array(
        '@nid' => $node->nid,
      );
      Message::make($msg, $variables, WATCHDOG_INFO, 1);
    }
  }
  else {

    // Op was an update, so just delete the revision.
    $revision_list = node_revision_list($node);
    $revision_id_to_rollback = $node->vid;
    unset($revision_list[$revision_id_to_rollback]);
    if (count($revision_list) > 0) {
      $last_revision = max(array_keys($revision_list));
      $node_last_revision = node_load($node->nid, $rollback_to_vid);
      node_save($node_last_revision);
      node_revision_delete($revision_id_to_rollback);
      $msg = "Node @nid updated but failed validation, Revision @deleted deleted and rolled back to revision @rolled_to.";
      $variables = array(
        '@nid' => $node->nid,
        '@deleted' => $revision_id_to_rollback,
        '@rolled_to' => $rollback_to_vid,
      );
      Message::make($msg, $variables, WATCHDOG_INFO, 1);
    }
  }
}