Closed (fixed)
Project:
Menu Block
Version:
8.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
28 Jun 2016 at 14:47 UTC
Updated:
7 Mar 2025 at 15:03 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
johnalbinComment #3
johnalbinComment #4
johnalbinWhoops. Looks like #2765411: PHP notice "Undefined index: menu_name" when menu tree is empty was the real reason that empty menu blocks were being rendered when there were no menu links.
Comment #5
caspervoogt commentedI just installed Menu Block 8.x-1.x-dev and my menu blocks are showing regardless of whether they contain menu links.
Also, if I add a menu block to a page that is not in any menu (and the menu contains links), the menu block shows up and contains no links. I am not getting "Undefined index: menu_name" or any other PHP errors. Here's my menu block config;
I had a look at the code, specifically src/Plugin/Block/MenuBlock.php's public function build(), which contains does a "return $build;" even if the menu contains no items. I think in any case it should check that $build["#items"] exists, and if not, hide the block.
Maybe something like this around line 200:
I tried this out but I am missing something .. this does hide the block, but my page still thinks there are blocks in that region. I'm not sure what else to change to get Drupal to know this region is empty. I played around with BlockBase::access and extending that, but got nowhere with that.. not sure what to do with it. Anyone have ideas?
Comment #6
genjohnson commented@caspervoogt, I'm experiencing the same behavior and found that it's an issue with how menu blocks are being handled in Drupal core: #2642816: Menu block is never empty, region keeps showing up
The menu_block module is extending core's menu blocks, which is why they are experiencing the same behavior.Turns out this is a much deeper issue. Take a look at [#953034].
Comment #7
genjohnson commentedAttached is a patch to check that there are items in the tree after the tree is manipulated, which for me has fixed the issue of the block title rendering when there are no links to display.
However, this does not address the issue in #2642816: Menu block is never empty, region keeps showing up.
Comment #8
joelpittetChanging the status to needs review so people with this issue can try it out
Comment #9
genjohnson commentedHere's a re-roll of #7 based on the latest dev (commit fbfb8fe, which happens to be 8.x-1.5). All that's changed are the line numbers, so I'm not providing an interdiff.
Comment #10
caspervoogt commentedThanks for the background info and patch, @genjohnson. I tried it out (added the patch into my composer.json file) and it seems to work well. I can't find fault with the configuration of the block now, nor its behavior. Curious what others think.
Comment #11
daveeporedia commentedI'm working with Drupal 8.5.4 and Menu block 8.x-1.5 and I have the same issue.
I've tried the menu_block-hide_block_if_no_links-2757215-9.patch patch but the modules still doesn't working, any suggestion?
Comment #12
robcarrPatch at #9 worked fine for me with Drupal 8.7.7. Seemed like a simple change to the logic. RTBC anyone?
Comment #13
stefdewa commentedI needed the almost accepted patch from #2809699: Add configuration options for dynamic block titles. So here's a patch that you can use after you apply the patch from the linked issue.
Comment #14
chris matthews commentedThe patch in #13 is working for Drupal 8.9.10 and menu_block 8.x-1.x-dev, changing status to RTBC.
Comment #15
stefdewa commentedRe-queued testing -> result is green.
Comment #16
juanolalla commentedThe purpose of this issue is to "Add option to show block if menu tree contains no links". In the patch it looks like we are not giving such an option, we are directly hiding it. I don't think this should be mandatory, what if we just want to show the active menu item as the title of the block and only menu link item?
Also, in the patch we are checking only if the menu tree is empty, not if the menu tree has enabled links. So if all the children were disabled, this wouldn't work.
Comment #17
joelpittetComment #18
gertlor commentedHere's a re-roll of #9 based on the latest version 8.x-1.7 . All that's changed are the line numbers
Comment #20
renatog commentedThe intention is incorrect in this case
Comment #21
karlshea@juanolalla
I'm trying to do exactly this, which doesn't work either. The block access check hides the whole block if there are no items.
Even if that access check is removed, the wrong title renders because there's no
$build['#theme']so the label manipulation won't happen.Comment #22
bernardm28 commentedThere are multiple ways this could be achieved.
That said one way I worked around was by checking whether the block had items on it.
Editors could choose a level that won't be visible as stated above but still pollute the layout page markup.
The 1.8 release made this evident and worse as it went live with blocks that were not rendering before. They were saved in the layout builder preview layout but since the live view did not render them they were ignored until now.
Comment #23
bernardm28 commentedComment #24
bernardm28 commentedSomething like that, for blocks that rendered empty would be great.
Comment #25
joelpittetComment #26
joelpittetRan out of time to test and commit this so moving to the next release plan
Comment #27
leoenriquezp commentedHere I leave a patch compatible with
8.x-1.11Comment #28
chaitanyadessai commentedComment #29
chaitanyadessai commentedComment #30
leoenriquezp commentedComment #31
joelpittetI think this is likely a duplicate of some of the other issues mentioned... can someone confirm?
Comment #32
joelpittetI am going to commit this, to get the feature in, it's been ages on the burner and obviously people want it.
Comment #34
joelpittetThanks, everyone, for your work on this and for your patience!
Comment #37
leoenriquezp commented