You are here

public function NodeRevisionDelete::getCandidatesRevisionsByNids in Node Revision Delete 8

Return the candidate revisions to be deleted if a group of nids.

All the nids must be of the same content type.

Parameters

array $nids: The nids.

Return value

array Array of vids.

Overrides NodeRevisionDeleteInterface::getCandidatesRevisionsByNids

File

src/NodeRevisionDelete.php, line 369

Class

NodeRevisionDelete
Class NodeRevisionDelete.

Namespace

Drupal\node_revision_delete

Code

public function getCandidatesRevisionsByNids(array $nids) {

  // @TODO check if the method can be improved.
  $candidate_revisions = [];

  // If we don't have nids returning an empty array.
  if (empty($nids)) {
    return $candidate_revisions;
  }

  // As all the nids must be of the same content type we just need to load
  // one.

  /** @var \Drupal\node\NodeInterface $node */
  $node = $this->entityTypeManager
    ->getStorage('node')
    ->load(current($nids));
  $content_type = $node
    ->getType();

  // Getting the content type config.
  $content_type_config = $this
    ->getContentTypeConfigWithRelativeTime($content_type);
  if (!empty($content_type_config)) {
    $sub_query = $this->connection
      ->select('node_field_data', 'n');
    $sub_query
      ->join('node_revision', 'r', 'r.nid = n.nid');
    $sub_query
      ->fields('r', [
      'vid',
      'revision_timestamp',
    ]);
    $sub_query
      ->condition('n.nid', $nids, 'IN');
    $sub_query
      ->condition('changed', $content_type_config['when_to_delete'], '<');
    if ($this->configFactory
      ->get($this->configurationFileName)
      ->get('delete_newer')) {
      $sub_query
        ->where('n.vid <> r.vid');
    }
    else {
      $sub_query
        ->where('n.vid > r.vid');
    }
    $sub_query
      ->groupBy('n.nid');
    $sub_query
      ->groupBy('r.vid');
    $sub_query
      ->groupBy('revision_timestamp');
    $sub_query
      ->orderBy('revision_timestamp', 'DESC');

    // We need to reduce in 1 because we don't want to count the default vid.
    // We excluded the default revision in the where call.
    $sub_query
      ->range($content_type_config['minimum_revisions_to_keep'] - 1, PHP_INT_MAX);

    // Allow other modules to alter candidates query.
    $sub_query
      ->addTag('node_revision_delete_candidate_revisions');
    $sub_query
      ->addTag('node_revision_delete_candidate_revisions_' . $content_type);
    $query = $this->connection
      ->select($sub_query, 't');
    $query
      ->fields('t', [
      'vid',
    ]);
    $query
      ->condition('revision_timestamp', $content_type_config['minimum_age_to_delete'], '<');
    $candidate_revisions = array_merge($candidate_revisions, $query
      ->execute()
      ->fetchCol());
  }
  return $candidate_revisions;
}