Problem/Motivation

Version 6.x-2.2 of Menu Block module had a key feature that would fallback to showing items on the current level (i.e. siblings to the active menu item) when the active menu item doesn't have any children.

Neither Drupal 7 nor Drupal 8/9 versions of the Menu Block module have this feature.

This is a very common use case for creating a sidebar secondary nav that is only ever supposed to be one or two levels deep at any given time, but is setup to always show as much relevant information as possible.

Steps to reproduce

If a menu block is configured to:

  • show 1 level,
  • follow the active menu item,
  • set the initial visibility rule to show Children of active menu item.

then when the user clicks on any child link, the menu display is less than helpful and only contains a single item (the current page).

Proposed resolution

Add a third option to the "Initial visibility level" radios:

  • Active menu item
  • Children of active menu item
  • New option: Children of active menu item; active menu item if no children

Remaining tasks

User interface changes

Users will see a new option in the menu block configuration screen.

API changes

The follow_parent menu block setting variable will respond to a new option called child_or_active in code.

Data model changes

N/a

Issue fork menu_block-758184

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:

Comments

emdalton’s picture

Priority: Normal » Critical

Did this get disabled in 6.x-2.3? We upgraded due to teh XSS issue, and all of a sudden our sidebar menus all vanished beyond the first level. We used to be able to see same-level items on pages with no children. For now, we're going to have to go back to an earlier version-- I can't go through and rebuild all our menus!

johnalbin’s picture

It sounds like you were wanting the behaviour that others thought was a bug. Is that correct? #345552: Inconsistent display of starting level set to children of active item

temp’s picture

Yes, and i will be happy if this being optionally switchable in new version.

Andreas Stenberg’s picture

I double that!

emdalton’s picture

Yes, exactly.

merovian’s picture

Here's the solution for this problem that works for me -- I found the difference in the file menu_block.follow.inc between ver 6.2.2 and 6.2.3

Just remove these lines 53 - 55 from the file menu_block.follow.inc:

if ($level == 'child') {
  $tree = array();
}

I can't remember how to write a patch file otherwise I would. Anyway, here's how I got this to work for my site.

temp’s picture

the best option would be an optional switch

pennykara’s picture

I second the optional switch, as it creates more consistency and thus improves user experience.

markhalliwell’s picture

I doubt this is the same issue, but I figured I'd share my idea with others that might encounter my issue. For me, if a top level menu item doesn't have any children the entire block doesn't show. Blocks need content, this is just how they work.

My menu block titles are all linked and I still need the block to show so you can click on the title regardless if you have no children. I use fgMenus on my site (http://www.juneauschools.org) to dynamically generate menus based on what clocks blocks are present in my menu region.

So I decided to modify menu_block a little to check and see if the content (menu) is empty and replace it with a UL tag with a class of "nomenu". I've also included my little recursive jQuery script that dynamically goes through each menu block and skips the "ul.nomenu" ones. Enjoy!

It would be nice if the title could always be shown or at least give the option, like they've said above.

/****************** menu_block.module (2010-05-18 Changes)
Line 322:
        Commented out:
          $data['content'] = menu_block_tree_output($tree, $config);
          
        Replaced with:
          $content = menu_block_tree_output($tree, $config);
          $data['content'] = empty($content) ? '<ul class="nomenu"></ul>' : $content;
          
        Reason:
          Need to still show menu links even if no children. <nomenu> for fgMenus
******************/

and jQuery:

	// Create fgMenus
	var themeArray    = new Array('blue','purple','orange','green','red');
	var themeLimit    = themeArray.length;
	var themeCurrent  = 0;
	$('.block-menu_block').each(function(i){
	  var content = $(this).find('div.content');
	  if (content.length != 0) {
	    if($(content).find('ul.nomenu').length == 0) {
  	    if (themeCurrent >= themeLimit) {
  	      themeCurrent = 0;
  	    }
  	    var menuLink = $(this).find('h2').find('a');
  	    var menuContent = $(this).find('div.content').find('div').html();
  	    var menuTitle = $(menuLink).text();
  	    menuLink.menu({
    			theme: themeArray[themeCurrent],
    			content: menuContent,
    			topLinkText: menuTitle
    		});
  	    themeCurrent++;
  	  }
	  }
	});

2010-06-08: Edited

markhalliwell’s picture

StatusFileSize
new25.15 KB

Just realized that I should probably upload the fgMenu JavaScript if have seriously modified. I have fixed a lot of bugs and added a new feature (themes). Please contact me if you have any questions.

theunraveler’s picture

StatusFileSize
new398 bytes

Here's a simple patch for the change suggested in #6. I agree that this should be an option, but I don't have time to write the functionality.

emdalton’s picture

So if I update to 6.2.3 and apply this patch, will I be able to see sibling menu items? What happens if another version of MenuBlock comes out?

emdalton’s picture

Has this patch been rolled in to dev yet?

temp’s picture

And little extension of my feathure request,

if i disable all child of some parent menu item - menu block module (and core menu module) process them as simple child items - but if i go to this parent-child level, same level items don't showing. path above can't solve this problem (but perfectly working with simple child items), please add optionaly support of parent-child items. Thanks!

Renee S’s picture

I support making this an option - I'm the person who filed the original "no children, don't show siblings either" issue (and still need it that way in some circumstances) - but now I'm working on a site with a different architecture, and have found a compelling use-case for the "show siblings" scenario myself.

I think there are good reasons to make this functionality selectable and transparent, and there's clearly a need for both!

@emdalton If you apply this patch it will get written over by the next version. I don't think it's the best idea to apply it unless you've got a good maintenance log or something, or your site will break. Making it an option with the default behaviour being the current way (no menu) would stop other sites from breaking on upgrade but allow us to use the new functionality going forward if we need it.

emdalton’s picture

Still not rolled into the current version? :( I'll apply the patch again, but I really would like to see this changed for future releases. Is a bounty needed?

tangent’s picture

I required the ability to show a menu block of "sibling menus" for some pages as well. I was able to achieve this, without patching, with the following method.

1. Create 2 menu blocks, identical.
2. For the menu block that should show siblings, instead of children, set "Starting level" to "Active menu item"
3. Check "Expand all children"
4. Set "Maximum depth" to 1 (unless you want the whole tree)
5. Optionally use some PHPfoo in the block configuration to alternate showing the appropriate menu block.

arbel’s picture

Version: 6.x-2.3 » 7.x-2.1

Hi,

I needed to display a menu block even if there are no children -> display only the title which is set as a link.

I had to hack the module:


 // Render the tree.
  $data = array();
  $title = menu_block_get_title($config['title_link'], $config);
  $data['subject_array'] = $title;
  $data['subject'] = drupal_render($title);
  $data['content']['#content'] = menu_block_tree_output($tree, $config);
 // if (!empty($data['content']['#content'])) {
    $data['content']['#theme'] = array(
      'menu_block_wrapper__' . str_replace('-', '_', $config['delta']),
      'menu_block_wrapper__' . str_replace('-', '_', $config['menu_name']),
      'menu_block_wrapper'
   );
    $data['content']['#config'] = $config;
    $data['content']['#delta'] = $config['delta'];  
 /* }
  else {
    $data['content'] = '';
  }*/

I simply commented out the check if there is no content, and it works great.

Is it possible to add this as an option to the block config - "[] Display menu block even if there are no menu items" so something along those lines.

Thanks

Idan

tangent’s picture

@arbel, yours is a separate issue. You should create a new issue for it.

arbel’s picture

oh..ok thought this was the same use case.

bak’s picture

Subscribe.

The "[] Display menu block even if there are no menu items" option is needed.
Without #18 hack I saw "Notice: Trying to get property of non-object in theme_blockreference_formatter_title() (line 683 of /sites/all/modules/blockreference/blockreference.module)." when I was creating a view with Menu Blocks for menus without children.

cabplan’s picture

I tried to use #18 hack and nothing changed with my nav menu that had no children, it still shows it as blank when I want it to remain as the same nav, if there are no children

dave reid’s picture

Priority: Critical » Normal
Issue summary: View changes
lukasss’s picture

This patch (#11) works for me ( version 7.x-2.3), thanks you

nelec’s picture

Hi.. I am dealing with the same issue, namley "Ability show items on same level if active menu level don't have children"
but working on a subsite I can't directly patch the Block Menu module.
Does anyone know an alternative to get the same result?
I thought about working on the subtheme template or adding php conditions to the block, but don't have any idea of where to start from..

zerolab’s picture

Version: 7.x-2.1 » 7.x-2.x-dev
Status: Active » Needs review
StatusFileSize
new1.13 KB

Attaching a patch against latest 7.x-2.x
This augments #11 with a new follow_parent option, rather than removing the current functionality that follows the children.

Cheers,
Dan

jojonaloha’s picture

We've been using #11 for over a year. I just tested #26 and it works as expected and I think is better for backwards compatibility.

For anybody using #11, if you switch to #26, you'll need to update your menu blocks config to maintain this behavior. I used an update script in a custom module:

/**
 * Update menu blocks that show children to fallback to siblings.
 */
function MYMODULE_update_7001() {
  if (module_exists('menu_block')) {
    // Find all menu blocks
    $deltas = variable_get('menu_block_ids', array());
    $exported = menu_block_get_exported_blocks();
    $deltas = array_unique(array_merge($deltas, array_keys($exported)));
    foreach ($deltas as $delta) {
      $var = 'menu_block_' . $delta . '_follow';
      if (variable_get($var, FALSE) === 'child') {
        variable_set($var, 'child_or_active');
      }
    }
  }
}
gmclelland’s picture

jojonaloha’s picture

I've looked at both patches #758184-26: Show items on same level if active menu level doesn't have children and #1620298-1: Add new starting level option "Children of active menu item, and active menu item if no children" and can confirm that they should be functionally identical (although I did not test the patch in the other issue). The only differences appear to be:

  • when they were created
  • name of the new option
  • comments in this patch to explain how the new option works
jwilson3’s picture

Version: 7.x-2.x-dev » 8.x-1.x-dev
StatusFileSize
new1.49 KB

In addition to #29 there were actually two other issues (now linked via reference in the sidebar) that provided patches that effectively do the same thing as this issue. I've closed all those as duplicates and will update this to the Drupal 8 version, and provide a patch.

For anyone still looking for the "best" patch for Drupal 7, #26 is probably your best shot.

Here is a Drupal 8 version that uses the underlying field value from #26 but the wording from #1620298-1: Add new starting level option "Children of active menu item, and active menu item if no children", which seems more inline with the field value and doesn't mention siblings, because a given active link may or may not have siblings.

jwilson3’s picture

There is a syntax error in the last patch.

ac’s picture

#31 works

ac’s picture

Status: Needs review » Reviewed & tested by the community
jwilson3’s picture

Title: Ability show items on same level if active menu level don't have children » Show items on same level if active menu level doesn't have children
Issue summary: View changes

Cleaned up issue title and summary

nvaken’s picture

This patch seems to work fine for us.

joelpittet’s picture

kevinquillen’s picture

Big addition indeed. Helps keep the menu from just vanishing.

kevinquillen’s picture

In addition - is there a way to prevent this behavior IF the item is level 0 and has no children? I don't want to show the top level navigation in that case.

nvaken’s picture

You might just be better off creating a second menu block in the same spot that only shows the root items if on that level? I think it might be a bit feature creep to implement those edge cases?

joelpittet’s picture

joelpittet’s picture

Ran out of time to test and commit this so moving to the next release

daniel korte’s picture

StatusFileSize
new1.46 KB

Reroll

chaitanyadessai made their first commit to this issue’s fork.

greenskin’s picture

We have a need for this feature. Additionally we needed the title/label to reflect similarly, where it displays the active item's title when it has children otherwise it displays the parent's title. This patch is a re-roll of #42 with a new block label option that will use the active item's title when the item has children, otherwise uses the parent title.

marthinal’s picture

joelpittet’s picture

Assigned: temp » Unassigned
Status: Reviewed & tested by the community » Needs work

@greenskin, it looks like you merged stuff from a different issue which conflates this issue, I feel like I have seen that elsewhere but haven't been able to spot it. Can someone ensure this is working with what the issue summary suggests on MR21 please?

I am hiding the patches as I would prefer an Merge Request.

daniel korte’s picture

Apologies for the noise. I tried to fix the merge request, but it did not go well so I created a new branch that is now mergeable.

jwilson3’s picture

Status: Needs work » Reviewed & tested by the community

I can confirm the MR #36 is a faithful reroll of the patch in #42 (without the extraneous code added in #45 and #46).

Thanks for the work @danielkorte

fabiansierra5191’s picture

I can confirm the MR #36 works as well, and it's a good feature for the module.