1

A three level menu:

  Item 1
    Item A
        Item X
        Item Y
        Item Z
    Item B
        Item Xx
        Item Yx
        Item Zx
    Item C
        Item Xy

When viewing the "Item 1" node, I'd like to display the sub-menu items in the template: Item A, Item B, Item C

When viewing the "Item B" node, I'd like to display the sub-menu items in the template: Item Xx, Item Yx, Item Zx

While I would like to rely totally on the menu structure, the output will not be displayed as a traditional menu.

This code does what I need:

function subpages($nid=null) {

$nid = 'node/'.$nid;

$menuItems = menu_tree_page_data('main-menu'); 

foreach($menuItems as $key => $m) {
    if ($m['link']['in_active_trail'] && $menuItems[$key]['below'] && $m['link']['link_path']  == $nid) {  
        $menu = menu_tree_output($menuItems [$key]['below']);
        } else if ($m['link']['in_active_trail'] && count($menuItems[$key]['below']) > 0) {
            foreach ($menuItems[$key]['below'] as $k => $third) {
                if ($third['below'] && $third['link']['in_active_trail']) {
                    $menu = menu_tree_output($third['below']);
                //foreach ($third['below'] as $item) {
                //  echo $item['link']['link_title'].'<br>';
                //}
            }
        }
    }
}
return $menu;

}

2 Answers 2

4

You can do that with Menu Block module.

It provides configurable blocks of menu trees starting with any level of any menu. And more!

You can then first create a menu block in the block configuration page. Then configure the menu block to start with the 2nd or 3rd level according to which level of the menu you want to have and set the depth to 1 so that it only lists that level and not its children.

If you want to have sub-menu items follow the current menu item, i.e. it always shows the children of the current page, you should set under the menu block configuration, Advanced Options, and check Make the starting level follow the active menu item.

6
  • I did try with Menu Block, but found that because I'm using the same menu, and it is 3 levels deep, it does not do what I need it to do. It came very, very close though: When viewing the "Item 1" node displays the sub-menu items: Item A, Item B, Item C When viewing the "Item B" node, it displays the sub-menu items: Item Xx, Item Yx, Item Zx A module called "Submenu tree" does do what I want, but it does require manually setting in each node. When viewing the "Item Xx" node, it also displays Item Xx, Item Yx, Item Zx
    – jdu
    Jan 23, 2014 at 20:00
  • @jdu, I've added an extra information about how to configure menu block to follow the current menu item's level.
    – Elin Y.
    Jan 24, 2014 at 8:22
  • 2
    "2nd or 3rd level according to which level of the menu you want to have" ... that's the challenge. I need it to detect where in the menu the current page node is, and change that value. Sometimes I want the second level, sometimes I want the third level. All dependent on where in the tree the node is found.
    – jdu
    Jan 24, 2014 at 14:23
  • If you read the rest of my answer, you'll see that I also added the instructions how to set the starting menu level dynamically.
    – Elin Y.
    Jan 24, 2014 at 16:42
  • We were trying to show a menu of the child items when they exist, but if they don't exist, show the sibling items. This is so there is always a menu shown to the user and no context for the user is lost. But it seems menu_block doesn't support a menu_block that can do both of these use cases. To solve this, we created two different menu_blocks, and then some custom code (block hooks), that called the second menu_block_block_view() if the first, returned an empty $block['content']. Hope that can help someone else! :)
    – alexkb
    Jul 2, 2014 at 1:10
2

You can get a menu which display the children of the active menu item, without having to create a block for each level, with the Menu Block module alone. Here is how:

How?

This works for the 7.x-2.5 version of Menu Block, I cannot guarantee this will work for other versions. Currently, they are planning to apply this patch to 2.6, so the patch (Step 2) may not be required in the future

  1. Download the Menu Block module
  2. Apply this patch
  3. Enable the module
  4. Go to Structure > Blocks and click 'Add Menu Block'
  5. Configure like so:

    • Starting level: 1st level (primary)
    • Make the starting level follow the active menu item: checked
    • Starting level will be: Children of active menu item
    • Maximum depth: 1
    • Make the maximum depth relative to the starting level while following the active menu item: checked

    (To get the sibling of the active menu item, change 'Starting level will be' to 'Active menu item' instead.)

What does the patch do?

Before the patch, in 7.x-2.5, the maximum depth option is relative to the menu root. After the patch, you have an option to "Make the maximum depth relative to the starting level while following the active menu item".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.