Score:0

How to custom access for entity paragraphs_type?

cz flag

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

Chris4783 avatar
gb flag
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"?
florian_drupal avatar
cz flag
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');`
Chris4783 avatar
gb flag
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.
florian_drupal avatar
cz flag
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.
florian_drupal avatar
cz flag
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 ?
Chris4783 avatar
gb flag
Your function has to be called `my_module_entity_type_alter`, not `my_module_paragraph_alter`.
florian_drupal avatar
cz flag
Thanks I didn't see my mistake I fix it, but I have an error : ```The "paragraph" entity type did not specify a access handler. in Drupal\Core\Entity\EntityTypeManager->getHandler()```
Chris4783 avatar
gb flag
Seems like you have a problem with the name of the file or the namespace. Make sure you use the namespace of the file, put the file in the correct folder the and that the file is named correctly. Maybe this link can help you [https://www.prometsource.com/blog/how-override-entity-access-handlers-drupal](https://www.prometsource.com/blog/how-override-entity-access-handlers-drupal)
florian_drupal avatar
cz flag
I found the problem : it's custom render with hook_preprocess_node, without checking access when the paragraph is render with ``\Drupal::entityTypeManager()->getViewBuilder('paragraph')->view($paragraph, 'paragraph_type'); ``
Score:0
cz flag

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');
    }
  }
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.