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. unpublished paragraph type 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,
  \Drupal\Core\Session\AccountInterface $account
) {
  echo '<pre>';
  //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 :


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':
          return AccessResult::forbidden();
      case 'view label':
        return AccessResult::allowedIfHasPermission($account, 'access content');
        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

  • Is the permission "View unpublished paragraphs" for anonymous users unchecked? The hook "hook_ENTITY_TYPE_access" must be named "my_module_paragraph_access". Have you altered the entity type for the AccessControllHandler with the hook "my_module_entity_type_alter"?
    – Chris4783
    May 11 at 13:32
  • Yes the permission "View unpublished paragraphs" is unchecked, I m confused with entity types paragraph and paragraphs_types they are not the same entity. To pass my new AccessControllHandler I use hook_entity_type_build with that : $entity_types['paragraphs_type']->setHandlerClass('access', 'Drupal\my_module\Access\CustomParagraphsTypeAccessControlHandler'); May 11 at 15:01
  • The paragraphs_types entity type is the bundle of the paragraph, eg WYSIWYG. The paragraph type is a paragraph of a specific bundle, eg WYSIWYG. So you need to alter the access to the paragraph and not to the paragraphs_type. Use the hook_entity_type_alter hook to alter the registered Access Control Handler. hook_entity_type_build just 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.
    – Chris4783
    May 12 at 5:08
  • thank for reply, I did not activate the module Paragraphs Type in my case, I will try your solution with the hook_entity_type_alter I did not understand the dependence of entities, now it is clearer. May 12 at 8:08
  • I try something : 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 ? 2 days ago


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.