首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何自定义实体paragraphs_type的访问?

如何自定义实体paragraphs_type的访问?
EN

Drupal用户
提问于 2022-05-11 09:40:00
回答 1查看 389关注 0票数 0

实体paragraphs_type有问题,当我在节点中添加一个paragraphs_type,其中包含段落项,并且设置它未发布状态时,匿名用户仍然可以看到它。

我试图通过不同的方式定制访问:

代码语言:javascript
复制
/**
 * 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 '';
  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();
}这不是工作,var_dump('my_module_paragraphs_type_access');,从来没有执行过或者扩展ParagraphsTypeAccessControlHandler的自定义类:isAnonymous())
          return AccessResult::forbidden();
        break;
      case 'view label':
        return AccessResult::allowedIfHasPermission($account, 'access content');
      default:
        return parent::checkAccess($entity, $operation, $account);
    }
  }
}但还是一样..。另一个把它们藏起来给匿名用户看的计划?我发现了一个问题:https://www.drupal.org/project/paragraphs/issues/3095959#comment-13363535
EN

回答 1

Drupal用户

发布于 2022-06-03 07:13:33

我发现了这个问题,这个段落是用hook_preprocess_node编程呈现的,我用这样的检查访问来修复它:

代码语言:javascript
复制
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');
    }
  }
票数 0
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://drupal.stackexchange.com/questions/311101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档