You are here

public static function AdminHelper::_latest_revision in Search and Replace Scanner 8

Get the latest revision.

File

src/AdminHelper.php, line 79

Class

AdminHelper
Shared logic for use in the mdoule.

Namespace

Drupal\scanner

Code

public static function _latest_revision($nid, &$vid, $langcode) {

  // Change record below might be helpful for future improvements.
  // @see https://www.drupal.org/node/2942013
  $lang = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  if (!isset($langcode)) {
    $langcode = $lang;
  }
  if ($lang != $langcode) {
    $lang = $langcode;
  }
  $latestRevisionResult = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->getQuery()
    ->latestRevision()
    ->condition('nid', $nid, '=')
    ->execute();
  if (count($latestRevisionResult)) {
    $node_revision_id = key($latestRevisionResult);
    if ($node_revision_id == $vid) {

      // There is no pending revision, the current revision is the latest.
      return FALSE;
    }
    $vid = $node_revision_id;
    $latestRevision = \Drupal::entityTypeManager()
      ->getStorage('node')
      ->loadRevision($node_revision_id);
    if ($latestRevision
      ->language()
      ->getId() != $lang && $latestRevision
      ->hasTranslation($lang)) {
      $latestRevision = $latestRevision
        ->getTranslation($lang);
    }
    return $latestRevision;
  }
  return FALSE;
}