Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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