Skip to content

Instantly share code, notes, and snippets.

@ksenzee
Created March 1, 2018 23:40
Show Gist options
  • Save ksenzee/c30e97934c49fa03bd09da9c2c9842f6 to your computer and use it in GitHub Desktop.
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
<?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