You are here

public function NodeRevisionDelete::getPreviousRevisions in Node Revision Delete 8

Get all revision that are older than current deleted revision.

The revisions should have the same language as the current language of the page.

Parameters

int $nid: The node id.

int $currently_deleted_revision_id: The current revision.

Return value

array An array with the previous revisions.

Overrides NodeRevisionDeleteInterface::getPreviousRevisions

File

src/NodeRevisionDelete.php, line 242

Class

NodeRevisionDelete
Class NodeRevisionDelete.

Namespace

Drupal\node_revision_delete

Code

public function getPreviousRevisions($nid, $currently_deleted_revision_id) {

  // @TODO check if the method can be improved.
  // Getting the node storage.
  $node_storage = $this->entityTypeManager
    ->getStorage('node');

  // Getting the node.
  $node = $this->entityTypeManager
    ->getStorage('node')
    ->load($nid);

  // Get current language code from URL.
  $langcode = $this->languageManager
    ->getCurrentLanguage()
    ->getId();

  // Get all revisions of the current node, in all languages.
  $revision_ids = $node_storage
    ->revisionIds($node);

  // Creating an array with the keys equal to the value.
  $revision_ids = array_combine($revision_ids, $revision_ids);

  // Adding a placeholder for the deleted revision, as our custom submit
  // function is executed after the core delete the current revision.
  $revision_ids[$currently_deleted_revision_id] = $currently_deleted_revision_id;
  $revisions_before = [];
  if (count($revision_ids) > 1) {

    // Ordering the array.
    krsort($revision_ids);

    // Getting the prior revisions.
    $revision_ids = array_slice($revision_ids, array_search($currently_deleted_revision_id, array_keys($revision_ids)) + 1, NULL, TRUE);

    // Loop through the list of revision ids, select the ones that have.
    // Same language as the current language AND are older than the current
    // deleted revision.
    foreach ($revision_ids as $vid) {

      /** @var \Drupal\Core\Entity\RevisionableInterface $revision */
      $revision = $node_storage
        ->loadRevision($vid);

      // Only show revisions that are affected by the language
      // that is being displayed.
      if ($revision
        ->hasTranslation($langcode) && $revision
        ->getTranslation($langcode)
        ->isRevisionTranslationAffected()) {
        $revisions_before[] = $revision;
      }
    }
  }
  return $revisions_before;
}