Hey guys

I'm running into this bug:
My main menu block is never empty, and that is why my region "sidebar" keeps showing up.

How to reproduce:
- Create menu block (header) and show top level only.
- Create menu block (sidebar) and show 2nd to unlimited level.
- Place both blocks in a region.

If my menu has child items, the child items show up correctly in the sidebar.
But if my menu does not have child items, it is visually empty, but the sidebar region is still showing.

The sidebar keeps showing up because it thinks the sidebar-main-menu is not empty.

A {{ dump(page.sidebar_first) }} in my page twig file is telling me this:

array (size=4)
  'hoofdnavigatie_2' => 
    array (size=3)
      '#cache' => 
        array (size=4)
          'keys' => 
            array (size=3)
              ...
          'contexts' => 
            array (size=2)
              ...
          'tags' => 
            array (size=3)
              ...
          'max-age' => int -1
      '#weight' => int -6
      '#lazy_builder' => 
        array (size=2)
          0 => string 'Drupal\block\BlockViewBuilder::lazyBuilder' (length=42)
          1 => 
            array (size=3)
              ...
  '#sorted' => boolean true
  '#theme_wrappers' => 
    array (size=1)
      0 => string 'region' (length=6)
  '#region' => string 'sidebar_first' (length=13)
CommentFileSizeAuthor
#11 MenuBlock.PNG29.03 KBacrosman
#11 EmptyBlock.PNG21.84 KBacrosman

Comments

yvesvanlaer created an issue. See original summary.

nkraft’s picture

I'm experiencing this exact same issue. In ver 8.0.1 AND 8.0.2

Region markup generates, even if menu for that page is empty. In Durpal 7, if the menu was empty for that node, the region did not generated. That felt like the correct functionality.

dawehner’s picture

chi’s picture

Can it be caused by wrong visibility determination in a theme?

dawehner’s picture

@Chi
Indeed this might be possible.

michiellucas’s picture

Does someone have a workaround for this moment?

chi’s picture

yvesvanlaer’s picture

This can indeed be prevented by doing the render in my page.html.twig.
Just like proposed here: https://www.drupal.org/node/953034#comment-9086523

But with twig debug: TRUE it still gives me the debug output, although it is not visible.
If I turn this off, it is working correctly.
Unfortunately it is not convenient when I'm in developers-mode :-).

So this is still an issue.

{% set sidebar_first = page.sidebar_first|render %}

{% if sidebar_first %}
<div id="sidebar-first"">
  <aside class="section" role="complementary">
    {{ page.sidebar_first }}
  </aside>
</div>
{% endif %}
nanobyt3’s picture

I am still facing this issue with Drupal 8.1.0 :(
region|render workaround works only with Twig debug disabled.

Daimos’s picture

Still same problem with Drupal 8.2.5, anyone have solution?

acrosman’s picture

Version: 8.0.1 » 8.4.x-dev
StatusFileSize
new21.84 KB
new29.03 KB

This still appears to be an issue in 8.3.x and 8.4.x-dev.

  1. Install core with Standard profile
  2. Create six basic pages arranged on the main navigation menu so that 1, 3, and 5 are on menu root, 2 and 4 are subs of 1 and 3 respectively (page 6 intentionally not on menu).
  3. Create a menu block with Initial visibility level set to 2, and levels to display unlimited. Place this block in Featured Top (the only empty default region, and helpfully with a grey background.
  4. View all 6 pages, and you will see the menu correctly when it should be there, and an empty grey bar on remaining pages.

Menu block appearing correctly.
Block with no content

maseyuk’s picture

I'm getting this too with 8.3.2

jnettik’s picture

Is everyone using the Menu Block module in contrib? I'm currently using Drupal 8.3.3 and ran into this issue, however was also using Menu Block for additional functionality. I disabled that module and the core block functionality worked as expected. Seems to be a known issue with Menu Blocks: https://www.drupal.org/node/2757215.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

brandonratz’s picture

As @acrosman mentioned, simply create a menu block with a visibility lower on the tree than available menu items and visit a page with the region. The output is an empty string and therefore rendered as such. This issue persists in 8.3.5 (my current version)

Workaround:
Add a condition to your twig file

{% if page.region_name.block_machine_name|render is not same as('') %}
  {{ page.region_name }}
{% endif %}

This is very undesirable as you'd have to repeat this for each instance of a menu block.

Note: not using menu_block module

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

aaronmchale’s picture

If like me you were experiencing this issue while using the Book Navigation Block with the setting "Show block only on book pages", and it is the only Block in the region, then a workaround is to set the Content Types in the Book Navigation Block Settings to only the Content Types that the menu is actually used on. Doing that seems to have solved the problem for me!

danielveza’s picture

I'm seeing this issue too. We do have menu block attached but judging from the investigation I've done and reading through the last comments I don't think it's related.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

gapple’s picture

This issue predates Big Pipe in core, so probably a different cause, but region markup may still be output if blocks output placeholders that are then replaced with an empty string by Big Pipe. One solution in that case is to use the block's access method to control block visibility, instead of returning an empty string from the block's render method (#953034-305: [meta] Themes improperly check renderable arrays when determining visibility)

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

redzeuf’s picture

This issue seems to be global with block behaviour, not just the menu block. It concerne all empty blocks displayed in regions.
There is few related discussions about this topic:
The main I find is https://www.drupal.org/node/953034
https://drupal.stackexchange.com/questions/175389/how-do-i-properly-dete...
https://drupal.stackexchange.com/questions/198052/hide-region-when-block...
https://www.drupal.org/forum/support/module-development-and-code-questio...

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

guillaumeduveau’s picture

#17 works for me for book navigation block. Thanks!

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

trackleft2’s picture

FYI I believe this works to hide regions if a block is empty. (you have to put this code in a .module file in a custom module.)

<?php

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

/**
 * Implements hook_block_access().
 */
function mymodule_block_access(Block $block, $operation, AccountInterface $account) {
  if ($operation === 'view') {
    $block = Block::load($block->id());
    $block_content = \Drupal::entityTypeManager()
      ->getViewBuilder('block')
      ->view($block);
    $markup = \Drupal::service('renderer')->renderRoot($block_content);
    return AccessResult::forbiddenIf($markup === '')->addCacheableDependency($block);
  }
  return AccessResult::neutral();
}

Probably not the greatest solution, because you have to render the block to check if it is empty before it renders again to be output in your theme.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Active » Closed (cannot reproduce)
Issue tags: +Bug Smash Initiative

Closing as cannot reproduce

Tested on Drupal 9.5
Created an empty menu
Placed that menu in a block region
Verified nothing was rendering.

If you still feel this is an issue please reopen with the steps you are seeing them in.

lance lancelot’s picture

Create a menu with submenu items.
Create a menu_block.
- Initial visibility level: 2
- Number of levels to display: unlimited
Place menu_block in sidebar_first.

On pages where there are no submenu items available (i.e. homepage) sidebar_first is shown.

The problem exists in 9.5.9.

Patches that did not work:
- https://www.drupal.org/project/menu_block/issues/3271218 (both patches)
- https://raw.githubusercontent.com/stef-van-looveren/drupal-8-patches/mas...

This one is working, but keep in mind that if twig_debug = true, the sidebar_left is shown:
https://www.drupal.org/project/menu_block/issues/3007225

lance lancelot’s picture

Status: Closed (cannot reproduce) » Needs review
smustgrave’s picture

Status: Needs review » Needs work

There is no patch to review.

Needs an issue summary update and will need tests

smustgrave’s picture

Also can you test with just core modules. If this is triggered by menu block could be bug there

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.