-
-
Save sumitk/5beac7f7b5df4900bdd7e8ab3d0a9a30 to your computer and use it in GitHub Desktop.
Revisions
-
filipengberg renamed this gist
Mar 16, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
filipengberg created this gist
Mar 16, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ <?php use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Session\AccountInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Drupal\Core\Access\AccessResult; use Drupal\Core\Url; /** * Implements HOOK_entity_access * Hide entities in $check_types * if they don't match the current language. * If viewing an untranslated node, * redirect to front page in current language */ function hide_untranslated_entity_access(EntityInterface $entity, $operation, AccountInterface $account) { $type = $entity->getEntityTypeId(); $check_types = ['node', 'inline_entity', 'paragraph']; if($operation != 'view' || !in_array($type, $check_types)) { return AccessResult::neutral(); } $language = Drupal::languageManager()->getCurrentLanguage(); if ($entity->language()->getId() == $language->getId()) { return AccessResult::neutral(); } // If viewing a node that is not translated // to current language, redirect user to frontpage if ($type == 'node') { $node = Drupal::routeMatch()->getParameter('node'); if (isset($node) && $node->id() == $entity->id()) { $url = Url::fromRoute('<front>', [], ['language' => $language]); $response = new RedirectResponse($url->toString()); $response->send(); } } return AccessResult::forbidden(); }