Skip to main content

Drupal 7 notes - putting submenus in different places from the parent menu while keeping your active-trail

Posted on: Sunday, 6 March 2011 @ 11:12am
Blatting about
More specifically

By default, when you set up a child menu, it appears directly under and slightly indented from the parent item.  Occasionally, there is a need (such as with the kiosk I'm currently working on for the Christmas Island Tourism Association) where you need an active-trail but the menus need to be in different regions.

Rather trying to do everything with blocks in regions I found it easier to dump code in templates.

<?php
      $main = menu_navigation_links('main-menu', '0');
      echo theme('links__system_main_menu', array(
        'links' => $main,
        'attributes' => array(
          'id' => 'main-menu',
          'class' => array('links')
        ),
      ));
  ?>

The breakdown:

$main = menu_navigation_links('main-menu', '0'); api call that grabs links from a menu called main-menu.  To get the name of the menu, go to /admin/structure/menu and either click on the "edit links" under Operations or just hover over it and look at the status bar.

Drupal edit menu

The '0' part tells it to only show the top level menu even if you click on a link with child items.  The lot is assigned to a variable called "main".

echo theme etc utilises the theme function, easiest way to assign ids and classes to your menus.  Uses about a hundred billion nested arrays.

'links' => $main says you want the links retrieved from the variable called $main which is what the links from your main menu have been assigned to, which is only going to show the top level items.

The attributes subarray is where you can set your id and classes.  Multiple classes needs to be done array style as the format suggests, 'class' => array('links', 'submenu') etc.

You do pretty much the same code to get the submenus, only rather than $main you would call it $sub or something to that effect, and instead of "0" it would be one of the sublevels, "1" or "2" (or more if you have some insane nesting going on).

The snippet should quite happily go in page.tpl.php and any node--type.tpl.php so you can use it to position submenus wherever you want on in a layout.  Coupled with if ($is_admin) or if ($logged_in) it may or may not help with loading times etc for those concerned about block caching, which seems to need to be off if you have permissions set on certain blocks.  I say "may or may not" as I haven't tested this theory yet :)

No comments yet

Carl Brown Thursday, 14 July 2011 @ 5:21pm [Permalink]

Hi, thanks for this snippet, it's really helpful. Do you know how you could get all menu items up to a certain depth, rather than only the menu items on the level defined in the function. For example, if you have a menu that was three levels deep:
- 1st level
-- 2nd level
-- 2nd level
--- 3rd level
-- 2nd level
- 1st level

How could you get all of the 'parent' and 'child' links, but not the 'grandchild' ones?

bek Thursday, 14 July 2011 @ 10:16pm [Permalink]

Add new comment

The content of this field is kept private and will not be shown publicly.