drupal-8 - Drupal 9 显示用户已验证的块,但隐藏其他成员角色
问题描述
我创建了一个模块来隐藏用户角色“小型企业,商业”的块。它们也是通用的“经过身份验证的”——我默认为基本成员。我想为基本成员显示一条消息,而其他成员角色则不需要。如果我加载模块,该块会隐藏“已验证”——即使我通过角色“商业”和“小型企业”指定
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);
```}
解决方案
推荐阅读
- sql-server - 维护父子表的历史数据变化
- javascript - 如何使用 JOI 验证来验证嵌套 json 对象的某些字段
- vue.js - Is `async/await` available in Vue.js `mounted`?
- php - php - string to key-value (associative) array type conversion
- audio - 在 nim 中使用 sdl2/mixer 播放音频的时间不可靠
- web - Nginx 将所有内容重定向到 www https
- javascript - 当我在完整日历中滚动时如何制作固定的左栏?
- spring - Spring : Autowire
- python - 计算总和可被 k 整除的所有子数组
- php - button should display only if we support order's zipcode