You are here

protected function EntityRepository::getLatestTranslationAffectedRevision in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityRepository.php \Drupal\Core\Entity\EntityRepository::getLatestTranslationAffectedRevision()

Returns the latest revision translation of the specified entity.

Parameters

\Drupal\Core\Entity\RevisionableInterface $entity: The default revision of the entity being converted.

string $langcode: The language of the revision translation to be loaded.

Return value

\Drupal\Core\Entity\RevisionableInterface The latest translation-affecting revision for the specified entity, or just the latest revision, if the specified entity is not translatable or does not have a matching translation yet.

1 call to EntityRepository::getLatestTranslationAffectedRevision()
EntityRepository::getActiveMultiple in core/lib/Drupal/Core/Entity/EntityRepository.php
Retrieves the active entity variants matching the specified context.

File

core/lib/Drupal/Core/Entity/EntityRepository.php, line 249

Class

EntityRepository
Provides several mechanisms for retrieving entities.

Namespace

Drupal\Core\Entity

Code

protected function getLatestTranslationAffectedRevision(RevisionableInterface $entity, $langcode) {
  $revision = NULL;
  $storage = $this->entityTypeManager
    ->getStorage($entity
    ->getEntityTypeId());
  if ($entity instanceof TranslatableRevisionableInterface && $entity
    ->isTranslatable()) {

    /** @var \Drupal\Core\Entity\TranslatableRevisionableStorageInterface $storage */
    $revision_id = $storage
      ->getLatestTranslationAffectedRevisionId($entity
      ->id(), $langcode);

    // If the latest translation-affecting revision was a default revision, it
    // is fine to load the latest revision instead, because in this case the
    // latest revision, regardless of it being default or pending, will always
    // contain the most up-to-date values for the specified translation. This
    // provides a BC behavior when the route is defined by a module always
    // expecting the latest revision to be loaded and to be the default
    // revision. In this particular case the latest revision is always going
    // to be the default revision, since pending revisions would not be
    // supported.
    $revision = $revision_id ? $this
      ->loadRevision($entity, $revision_id) : NULL;
    if (!$revision || $revision
      ->wasDefaultRevision() && !$revision
      ->isDefaultRevision()) {
      $revision = NULL;
    }
  }

  // Fall back to the latest revisions if no affected revision for the current
  // content language could be found. This is acceptable as it means the
  // entity is not translated. This is the correct logic also on monolingual
  // sites.
  if (!isset($revision)) {
    $revision_id = $storage
      ->getLatestRevisionId($entity
      ->id());
    $revision = $this
      ->loadRevision($entity, $revision_id);
  }
  return $revision;
}