1

My site nav is all in a single menu: main-menu, eg:

  • Home
  • About
    • Director Name 1
    • Director Name 2
  • Contact

My site navigation bar comprises two horizontal strips: the upper one lists all top-level pages (ie: Home, About, Contact, Services). The lower one should be populated with the revelant sub-menu items for the current page. So if you're on the Home page, the bar will be empty. If you're on the About page, the bar will contain links to each of the Directors pages.

My top menu is populated by the following, unchanged from the code in the default page.tpl.php:

<?php print theme('links__system_main_menu', array('links' => $main_menu, 'attributes' => array('id' => 'main-menu', 'class' => array('links', 'inline', 'clearfix')))); ?>

To get the sub-menu items, however, I've had to add the following to my theme's template.php:

function mytheme_preprocess_page(&$vars){

    $vars['page_submenu'] = NULL;
    $main_menu_tree = menu_tree_page_data('main-menu', NULL, TRUE);
    foreach ($main_menu_tree as $menu_item) {
        if (count($menu_item['below']) > 0) {
            $submenu = $menu_item['below'];
        }
    }
    if ( !empty($submenu)){
        $vars['page_submenu'] = menu_tree_output($submenu);
    }
}

... basically, "get the main menu, find the menu item in it that has some "below" items, and return that "below" bit as a menu tree".

I can then just render that in the template:

print drupal_render($page_submenu);

This seems a bit kludgy. Returning only the submenu items for the current page would seem like a fairly common requirement - is there a better way to do it?

2
  • Hello and welcome. Please ask one question a time. If you make a post asking many question, it's not really suited for Q&A site, as it calls for discussion or full use case analysis, no simply for an answer. If you want to ask 2 questions, post them as 2 questions. If you want to know one thing, please edit this question to make it clear what are you asking.
    – Mołot
    Commented Nov 4, 2013 at 10:47
  • "Too broad" close vote retracted ;)
    – Mołot
    Commented Nov 4, 2013 at 12:36

1 Answer 1

1

You are right it's a pretty common requirement. If you are not afraid of slight performance hit, you may use Menu Block module. It is able to provide you with submenu relative to your current path:

So if you’re only using your theme’s Main menu links feature, you can add and configure a “Main menu (levels 2+)” block. That block would appear once you were on one of the Main menu’s pages and would show the menu tree for the 2nd level (and deeper) of your Main menu and would expand as you traversed down the tree. You can also limit the depth of the menu’s tree (e.g. “Main menu (levels 2-3)”) and/or expand all the child sub-menus (e.g. “Main menu (expanded levels 2+)”).

Pretty simple, eh?

Of course it is a pretty big module, and it puts additional block layout between your menu and your display, so if it is too much of a performance hit, you can try to borrow some of it's code - it's GPL after all.

2
  • Thanks for the reply - yes, I'd seen Menu Block suggested as a possible solution elsewhere. I was trying to avoid using it, both for the performance reasons you mention and because (as you can probably guess) I'm trying to get to grips with Drupal development and wanted to figure out for myself how it was done rather than simply click a few module config options. But if the accepted wisdom is "Want path-relative submenus? Use Menu Block." then I'm happy to accept that as the answer :)
    – Wintermute
    Commented Nov 4, 2013 at 12:55
  • @Wintermute Don't rush it ;) I never had any real performance issues with it, I only heard of them. On the other hand doing it manually is not bad idea. I'll be happy if my answer will be just an easy alternative for those who don't want to code themselves. Personally I always had more interesting things to do than re-writing what I could get, even at the slight cost of site speed.
    – Mołot
    Commented Nov 4, 2013 at 13:01

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.