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( Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $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 \Drupal\Core\Access\AccessResult::forbidden(); } return \Drupal\Core\Access\AccessResult::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 Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Session\AccountInterface; use Drupal\paragraphs\ParagraphsTypeAccessControlHandler; /* * 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
paragraphandparagraphs_typesthey are not the same entity. To pass my new AccessControllHandler I usehook_entity_type_buildwith that :$entity_types['paragraphs_type']->setHandlerClass('access', 'Drupal\my_module\Access\CustomParagraphsTypeAccessControlHandler');hook_entity_type_alterhook to alter the registered Access Control Handler.hook_entity_type_buildjust adds additional information and does not alter them. Then use$entity_types['paragraph']->setAccessClass. I tried your scenario with a fresh install and didn't have to adjust anything, even though I had the Paragraphs Type Permissions module enabled.hook_entity_type_alterI did not understand the dependence of entities, now it is clearer.function my_module_paragraph_alter(array &$entity_types) { /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */ echo '<pre>'; var_dump('hooked'); die(); if($entity_types['paragraph']) { $entity_types['paragraph']->setAccessClass('Drupal\my_module\Access\CustomParagraphAccessControlHandler'); } }but I never see my debug, something wrong ?