content_translation_language_fallback_candidates_entity_view_alter

content_translation_language_fallback_candidates_entity_view_alter(&$candidates, $context)

Implements hook_language_fallback_candidates_OPERATION_alter().

Performs language fallback for inaccessible translations.

File

core/modules/content_translation/content_translation.module, line 328
Allows entities to be translated into different languages.

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]);
        }
      }
    }
  }
}
doc_Drupal
2016-10-29 08:58:39
Comments
Leave a Comment

Please login to continue.