Created
March 1, 2018 23:40
-
-
Save ksenzee/c30e97934c49fa03bd09da9c2c9842f6 to your computer and use it in GitHub Desktop.
How to keep Drupal paragraphs from "bleeding through" into languages where no translation exists - see https://www.mediacurrent.com/blog/deep-dive-translating-paragraphs
This file contains 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 characters
<?php | |
/** | |
* Implements hook_ENTITY_TYPE_access() for paragraph entities. | |
*/ | |
function mysite_paragraph_access(\Drupal\paragraphs\Entity\Paragraph $entity, $operation, $account) { | |
// Show paragraphs only if they exist in the current language; don't fall | |
// back to the default language. | |
if ($operation == 'view') { | |
$current_language = \Drupal::languageManager()->getCurrentLanguage()->getId(); | |
$available = $entity->getTranslationLanguages(); | |
if (!isset($available[$current_language])) { | |
return \Drupal\Core\Access\AccessResult::forbidden(); | |
} | |
} | |
return \Drupal\Core\Access\AccessResult::neutral(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment