I have a problem with entity paragraphs_type, when I add a paragraphs_type in a node, with paragraph items inside, and I set it unpublish status, anonymous users can still see it. I try to custom access by different way :
/** * Implements hook_ENTITY_TYPE_access(). * ENTITY_TYPE : paragraphs_type */ function my_module_paragraphs_type_access( DrupalCoreEntityEntityInterface $entity, $operation, DrupalCoreSessionAccountInterface $account ) { echo '<pre>'; var_dump('my_module_paragraphs_type_access'); var_dump($operation); var_dump($account); exit(); //Hide paragraph for anonymous users if is not published if ($operation == 'view' && !$entity->isPublished() && ($account->isAnonymous() || !$account->hasPermission('view unpublished paragraphs')) ) { return DrupalCoreAccessAccessResult::forbidden(); } return DrupalCoreAccessAccessResult::allowed(); }
It’s not work, var_dump('my_module_paragraphs_type_access');
it’s never executed
Or a custom class that extends ParagraphsTypeAccessControlHandler :
<?php use DrupalCoreAccessAccessResult; use DrupalCoreEntityEntityInterface; use DrupalCoreSessionAccountInterface; use DrupalparagraphsParagraphsTypeAccessControlHandler; /* * Add custom paragraphs_type access */ class CustomParagraphsTypeAccessControlHandler extends ParagraphsTypeAccessControlHandler { /** * {@inheritdoc} */ protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) { switch ($operation) { case 'view unpublished paragraphs': if($account->isAnonymous()) return AccessResult::forbidden(); break; case 'view label': return AccessResult::allowedIfHasPermission($account, 'access content'); default: return parent::checkAccess($entity, $operation, $account); } } }
but still the same… Another plan to hide them to anonymous users ?
I found an this issue :https://www.drupal.org/project/paragraphs/issues/3095959#comment-13363535