In the Drupal 7 version of menu_block, if the requested menu tree contains no links, the block is not displayed.

There is a a feature request for the D7 version (#2327795: Display block if no child menu items), but we'd need a slightly different implementation for the D8 version.

Issue fork menu_block-2757215

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

JohnAlbin created an issue. See original summary.

JohnAlbin’s picture

Title: Add option to hide blocks if request menu tree contains no links » Add option to hide block if request menu tree contains no links
JohnAlbin’s picture

Title: Add option to hide block if request menu tree contains no links » Add option to hide block if menu tree contains no links
JohnAlbin’s picture

Title: Add option to hide block if menu tree contains no links » Add option to show block if menu tree contains no links
Issue summary: View changes

In Drupal 8, the block is displayed regardless if there are menu links or not.

Whoops. 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.

caspervoogt’s picture

I 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;

screenshot

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:

if($build["#items"]){
    return $build;
}
else{
  //not sure what to do here, maybe just "return array();"
}

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?

genjohnson’s picture

@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].

genjohnson’s picture

Attached 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.

joelpittet’s picture

Status: Active » Needs review

Changing the status to needs review so people with this issue can try it out

genjohnson’s picture

Here'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.

caspervoogt’s picture

Thanks 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.

DaveEporedia’s picture

I'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?

robcarr’s picture

Patch at #9 worked fine for me with Drupal 8.7.7. Seemed like a simple change to the logic. RTBC anyone?

Stefdewa’s picture

I 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.

Chris Matthews’s picture

Status: Needs review » Reviewed & tested by the community

The patch in #13 is working for Drupal 8.9.10 and menu_block 8.x-1.x-dev, changing status to RTBC.

Stefdewa’s picture

Re-queued testing -> result is green.

juanolalla’s picture

The 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.

joelpittet’s picture

Gertlor’s picture

Here's a re-roll of #9 based on the latest version 8.x-1.7 . All that's changed are the line numbers

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 18: menu_block-hide_block_if_no_links-2757215-18.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

renatog’s picture

FileSize
28.83 KB

The intention is incorrect in this case

KarlShea’s picture

@juanolalla

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?

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.

bernardm28’s picture

There 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.

{% if content["#items"]|length < 1%}
    <p>The are no children, keeps this block from vanishing to 0px and polluting layouts. </p>
  {% endif %}
bernardm28’s picture

Something like that, for blocks that rendered empty would be great.

joelpittet’s picture