function content_translation_language_fallback_candidates_entity_view_alter

Same name and namespace in other branches
  1. 10 core/modules/content_translation/content_translation.module \content_translation_language_fallback_candidates_entity_view_alter()
  2. 9 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 431

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())) {
    $entity_type = $entity->getEntityType();
    $permission = $entity_type->getPermissionGranularity() == 'bundle' ? $permission = "translate {$entity->bundle()} {$entity_type_id}" : "translate {$entity_type_id}";
    $current_user = \Drupal::currentuser();
    if (!$current_user->hasPermission('translate any entity') && !$current_user->hasPermission($permission)) {
      foreach ($entity->getTranslationLanguages() as $langcode => $language) {
        $metadata = $manager->getTranslationMetadata($entity->getTranslation($langcode));
        if (!$metadata->isPublished()) {
          unset($candidates[$langcode]);
        }
      }
    }
  }
}

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