After updating to 2.5 from 2.4 menu blocks started being rendered where they hadn't before. The block content was empty. For example an empty block appeared on the front page, because I hadn't specified in the block visibility settings not to show it there.

Looking over the code, it isn't clear what would have caused this, and I thought maybe it was theme-specific. But I switched to another theme and the empty blocks still appeared. When I reverted to 2.4 the empty blocks stopped appearing.

I did test dev and still had the problem.

Comments

biggm’s picture

I'm seeing the same issue, it's specifically related to the "menu selected by page" functionality.

ricovandevin’s picture

Version: 7.x-2.x-dev » 7.x-2.5
Status: Active » Closed (works as designed)

We have encountered the same issue when updating Menu Block from 7.x-2.4 to 7.x-2.5. In the new version when there is no menu to be rendered inside the block $data['content'] is set to an empty array while in previous versions this was an empty string. This is an ok thing to do since an empty array will also lead to the block not being rendered.

function menu_tree_build(array &$config) {
  ...

  // Create a renderable tree.
  $data = array();
  $data['subject_array'] = $title;
  $data['subject'] = drupal_render($title);
  $data['content'] = array();
function _block_render_blocks($region_blocks) {
  ...

      if (isset($block->content) && $block->content) {
        // Normalize to the drupal_render() structure.

But other modules might interact through hook_block_view_alter() which can lead to undesired results. In our case this was the CDN module which adds a post render callback to $data['content'] if that is an array (without checking if it is an empty array). This will make the array non-empty resulting in the block being rendered.

/**
 * Implements hook_block_view_alter().
 */
function cdn_block_view_alter(&$data, $block) {
  if (isset($data['content'])) {
    // Blocks with render arrays.
    if (is_array($data['content'])) {
      $data['content']['#post_render'][] = 'cdn_post_render_html_alter';
    }

Since this undesired interaction can also happen when $data['content'] is an empty string I think this is not an issue that has to be solved in the Menu Block module. I'll mark this issue as a 'works as designed'. If anybody thinks this should be solved in Menu Block feel free to reopen.

ricovandevin’s picture

biggm’s picture

Thought I would update on our specific situation. We determined that one of our preprocessing functions was causing the block to render even though it was empty. It still was fixed by the patch later deployed, but if you're still having trouble that's a good first place to look.

osman’s picture

Status: Closed (works as designed) » Active

Sorry for re-opening this issue a year after it was closed.

However I believe if the menu block has no menu items to display it should be consider empty, thus the menu block should not be build in defined region.

I set up a menu block to be shown in sidebar_first region with following configuration:

Initial menu level: 2
Maximum number of menu levels to display: Unlimited
Expand all menu links: Un-checked
Fixed parent item: <Main Menu>

So, the pages not part of the defined menu, or simply out of the menu tree still generates an empty block:

<nav role="navigation" aria-labelledby="block-sag-sidebar-menu-extended-health-menu" id="block-sag-sidebar-menu-extended-health" class="block block-menu navigation menu--main-health">
  <h2 class="visually-hidden" id="block-sag-sidebar-menu-extended-health-menu">Health Main Menu (extended config)</h2>
</nav>

This simply breaks the layout. I think this should be consider a bug.

osman’s picture

Version: 7.x-2.5 » 8.x-1.x-dev
osman’s picture

JohnAlbin’s picture

Version: 8.x-1.x-dev » 7.x-2.x-dev
Status: Active » Closed (works as designed)

We already have an open issue for D8; setting this back to closed for D7.