0

我创建了一个模块来隐藏用户角色“小型企业,商业”的块。它们也是通用的“经过身份验证的”——我默认为基本成员。我想为基本成员显示一条消息,而其他成员角色则不需要。如果我加载模块,该块会隐藏“已验证”——即使我通过角色“商业”和“小型企业”指定

enter code here

```use Drupal\block\Entity\Block;
```use Drupal\Core\Session\AccountInterface;
```use Drupal\node\NodeInterface;
```use Drupal\Core\Access\AccessResult;

```function hideblock_block_access(Block $block, $operation, AccountInterface $account)
```{
    
    ```if ($operation == 'view')
    ```{
        ```if($block->id() == 'singlesearchcount')
        ```{
            ```$roles = \Drupal::currentUser()->getRoles();
            ```//anonymous
            ```//adminstrator
            ```if(in_array('small_business', 'commercial', $roles))
            ```{
                ```//Hide the block for specific user roles like editor
                ```return AccessResult::forbiddenIf(true)->addCacheableDependency($block);
            ```}
            ```//Now based on Node Pages, Hide the block
            ```$node = \Drupal::routeMatch()->getParameter('node');
            ```if (($node instanceof \Drupal\node\NodeInterface) && ($node->gettype() !=       ```'add-noshow-search-noshow') )
            ```{
                ```//Hiding the block if the node is not 'technologyarticle' type
                ```return AccessResult::forbiddenIf(true)->addCacheableDependency($block);
            ```}
            
4

0 回答 0