0

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,
  $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

9
  • 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
    Commented May 11, 2022 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'); Commented May 11, 2022 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
    Commented May 12, 2022 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. Commented May 12, 2022 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 ? Commented May 18, 2022 at 7:37

1 Answer 1

0

I found the problem, the paragraph is render programmatically with hook_preprocess_node, I fix it with checking access like that :

function mymodule_preprocess_node__my_node(&$variables) {

  $node = $variables['node'];

  $paragraphs = $node->get('field_para_blocks');

  /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
  foreach ($paragraphs->referencedEntities() as $paragraph) {

    $bundle = $paragraph->bundle();

    if ($paragraph->access('view')) {
      $bundle = $paragraph->bundle();
      $variables[$bundle] = \Drupal::entityTypeManager()
        ->getViewBuilder('paragraph')
        ->view($paragraph, 'my_paragraph_type');
    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.