You are here

function drush_node_revision_delete_nrd_delete_prior_revisions_validate in Node Revision Delete 8

Same name and namespace in other branches
  1. 7.3 node_revision_delete.drush.inc \drush_node_revision_delete_nrd_delete_prior_revisions_validate()

Implements drush_hook_COMMAND_validate().

File

./node_revision_delete.drush.inc, line 415
Drush commands related to the Node Revision Delete module.

Code

function drush_node_revision_delete_nrd_delete_prior_revisions_validate() {
  $args = func_get_args();

  // Check for only 2 arguments.
  if (count($args) != 2) {
    return drush_set_error('NODE_REVISION_DELETE_INVALID_ARGUMENT', dt('This command use two arguments.'));
  }

  // Check if the first argument is a valid node id.
  $node = Node::load($args[0]);
  if (is_null($node)) {
    return drush_set_error('NODE_REVISION_DELETE_INVALID_ARGUMENT', dt('The first argument @nid is not a valid node id.', [
      '@nid' => $args[0],
    ]));
  }

  // Check if the second argument is a valid revision id.
  $revision = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadRevision($args[1]);
  if (is_null($revision)) {
    return drush_set_error('NODE_REVISION_DELETE_INVALID_ARGUMENT', dt('The second argument @vid is not a valid revision id.', [
      '@vid' => $args[1],
    ]));
  }

  // Check if the nid and the vid match the same node.
  if ($node
    ->id() !== $revision
    ->id()) {
    return drush_set_error('NODE_REVISION_DELETE_INVALID_ARGUMENT', dt('The revision id @vid doesn\'t belong to the node id @nid.', [
      '@nid' => $args[0],
      '@vid' => $args[1],
    ]));
  }

  // Check if exists prior revisions.
  if (!count(\Drupal::service('node_revision_delete')
    ->getPreviousRevisions($args[0], $args[1]))) {
    drush_log(dt('There is no revision prior to revision @vid.', [
      '@vid' => $args[1],
    ]), 'warning');
    return FALSE;
  }
}