function content_translation_language_fallback_candidates_entity_view_alter

Same name and namespace in other branches
  1. 9 core/modules/content_translation/content_translation.module \content_translation_language_fallback_candidates_entity_view_alter()
  2. 8.9.x core/modules/content_translation/content_translation.module \content_translation_language_fallback_candidates_entity_view_alter()

Implements hook_language_fallback_candidates_OPERATION_alter().

Performs language fallback for inaccessible translations.

File

core/modules/content_translation/content_translation.module, line 432

Code

function content_translation_language_fallback_candidates_entity_view_alter(&$candidates, $context) {
  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $context['data'];
  $entity_type_id = $entity->getEntityTypeId();
  /** @var \Drupal\content_translation\ContentTranslationManagerInterface $manager */
  $manager = \Drupal::service('content_translation.manager');
  if ($manager->isEnabled($entity_type_id, $entity->bundle())) {
    /** @var \Drupal\content_translation\ContentTranslationHandlerInterface $handler */
    $handler = \Drupal::entityTypeManager()->getHandler($entity->getEntityTypeId(), 'translation');
    foreach ($entity->getTranslationLanguages() as $langcode => $language) {
      $metadata = $manager->getTranslationMetadata($entity->getTranslation($langcode));
      if (!$metadata->isPublished()) {
        $access = $handler->getTranslationAccess($entity, 'update');
        $entity->addCacheableDependency($access);
        if (!$access->isAllowed()) {
          // If the user has no translation update access, also check view
          // access for that translation, to allow other modules to allow access
          // to unpublished translations.
          $access = $entity->getTranslation($langcode)
            ->access('view', NULL, TRUE);
          $entity->addCacheableDependency($access);
          if (!$access->isAllowed()) {
            unset($candidates[$langcode]);
          }
        }
      }
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.