function 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]);
        }
      }
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!content_translation!content_translation.module/function/content_translation_language_fallback_candidates_entity_view_alter/8.1.x