Page Title Block and Main Content Block ignore Visibility settings when inside a block group

Created on 20 October 2021, over 2 years ago
Updated 6 April 2023, about 1 year ago

Problem/Motivation

When the Page Title and/or Main Content Block is in a block group they are always displayed. They ignore any visibility settings from the respective block configuration. They still respect the visibility settings of the block group itself. Any other block or block type placed in a block group can have its own visibility settings as excepted. Only Page Title and Main Content blocks are affected.

Steps to reproduce

See above. Add a page title block or main content block to any block group and change the visibility setting for either block (page title or main content) and see that the visibility for the respective block does not change. The block always displays.

Proposed resolution

In file: src/Plugin/Block/BlockGroup.php , in function build() :
For the Main content block:
Replace

 // Special condition for Main block.
      if ($block->getPluginId() == 'system_main_block') {
        $renderedBlocks[$block->id()] = $this->mainContent;
      }

with this:

      // Special condition for Main block.
    if ($block->getPluginId() == 'system_main_block') {
        /** @var \Drupal\Core\Access\AccessResultInterface $accessResult */
        $accessResult = $block->access('view', NULL, TRUE);
        if ($accessResult->isAllowed()) {
          $renderedBlocks[$block->id()] = $this->mainContent;
        }
      }

And for the Page Title:
replace:

// Special condition for Title block.
      elseif ($block->getPluginId() == 'page_title_block') {
        $title = $this->titleResolver->getTitle($this->request, $this->routeMatch->getRouteObject());
        $renderedBlocks[$block->id()] = [
          '#type' => 'page_title',
          '#title' => $title,
          '#weight' => $block->getWeight(),
        ];
      }

with this:

// Special condition for Title block.
      elseif ($block->getPluginId() == 'page_title_block') {
        /** @var \Drupal\Core\Access\AccessResultInterface $accessResult */
        $accessResult = $block->access('view', NULL, TRUE);
        if ($accessResult->isAllowed()) {
          $title = $this->titleResolver->getTitle($this->request, $this->routeMatch->getRouteObject());
          $renderedBlocks[$block->id()] = [
            '#type' => 'page_title',
            '#title' => $title,
            '#weight' => $block->getWeight(),
          ];
        }
      }

Note:
This takes the if ($accessResult->isAllowed()) { check from the 'Any other block' else statement below and applies it to both Page Title and Main Content 'Special Condition' statements.

Remaining tasks

More testing or review is probably needed.

πŸ› Bug report
Status

Active

Version

1.5

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States devanbicher

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024