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)
Comment | File | Size | Author |
---|---|---|---|
#11 | MenuBlock.PNG | 29.03 KB | acrosman |
#11 | EmptyBlock.PNG | 21.84 KB | acrosman |
Comments
Comment #2
nkraftI'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.
Comment #3
dawehnerI guess we need something like #2443457: Views block displayed when no results returned here.
Comment #4
Chi CreditAttribution: Chi commentedCan it be caused by wrong visibility determination in a theme?
Comment #5
dawehner@Chi
Indeed this might be possible.
Comment #6
michiellucas CreditAttribution: michiellucas as a volunteer and commentedDoes someone have a workaround for this moment?
Comment #7
Chi CreditAttribution: Chi commented@michiellucas, I do
http://cgit.drupalcode.org/samara/tree/js/samara.js?h=8.x-1.x#n43
Comment #8
yvesvanlaer CreditAttribution: yvesvanlaer as a volunteer commentedThis 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.
Comment #9
nanobyt3 CreditAttribution: nanobyt3 commentedI am still facing this issue with Drupal 8.1.0 :(
region|render workaround works only with Twig debug disabled.
Comment #10
Daimos CreditAttribution: Daimos commentedStill same problem with Drupal 8.2.5, anyone have solution?
Comment #11
acrosmanThis still appears to be an issue in 8.3.x and 8.4.x-dev.
Comment #12
maseyuk CreditAttribution: maseyuk commentedI'm getting this too with 8.3.2
Comment #13
jnettikIs 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.
Comment #15
brandonratz CreditAttribution: brandonratz as a volunteer commentedAs @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
This is very undesirable as you'd have to repeat this for each instance of a menu block.
Note: not using menu_block module
Comment #17
AaronMcHaleIf 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!
Comment #18
DanielVezaI'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.
Comment #20
gappleThis 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)
Comment #22
redzeufThis 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...
Comment #24
GuillaumeDuveau#17 works for me for book navigation block. Thanks!
Comment #27
trackleft2 CreditAttribution: trackleft2 as a volunteer and at The University of Arizona commentedFYI 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.)
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.
Comment #31
smustgrave CreditAttribution: smustgrave at Mobomo commentedClosing 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.
Comment #32
Lance Lancelot CreditAttribution: Lance Lancelot commentedCreate 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
Comment #33
Lance Lancelot CreditAttribution: Lance Lancelot commentedComment #34
smustgrave CreditAttribution: smustgrave at Mobomo commentedThere is no patch to review.
Needs an issue summary update and will need tests
Comment #35
smustgrave CreditAttribution: smustgrave at Mobomo commentedAlso can you test with just core modules. If this is triggered by menu block could be bug there