function menu_navigation_links

You are here

7 menu.inc menu_navigation_links($menu_name, $level = 0)
6 menu.inc menu_navigation_links($menu_name, $level = 0)
8 menu.inc menu_navigation_links($menu_name, $level = 0)

Return an array of links for a navigation menu.

Parameters

$menu_name: The name of the menu.

$level: Optional, the depth of the menu to be returned.

Return value

An array of links of the specified menu and level.

Related topics

2 calls to menu_navigation_links()
menu_primary_links in includes/menu.inc
Return an array of links to be rendered as the Primary links.
menu_secondary_links in includes/menu.inc
Return an array of links to be rendered as the Secondary links.

File

includes/menu.inc, line 1264
API for the Drupal menu system.

Code

function menu_navigation_links($menu_name, $level = 0) {
  // Don't even bother querying the menu table if no menu is specified.
  if (empty($menu_name)) {
    return array();
  }

  // Get the menu hierarchy for the current page.
  $tree = menu_tree_page_data($menu_name);

  // Go down the active trail until the right level is reached.
  while ($level-- > 0 && $tree) {
    // Loop through the current level's items until we find one that is in trail.
    while ($item = array_shift($tree)) {
      if ($item['link']['in_active_trail']) {
        // If the item is in the active trail, we continue in the subtree.
        $tree = empty($item['below']) ? array() : $item['below'];
        break;
      }
    }
  }

  // Create a single level of links.
  $links = array();
  foreach ($tree as $item) {
    if (!$item['link']['hidden']) {
      $class = '';
      $l = $item['link']['localized_options'];
      $l['href'] = $item['link']['href'];
      $l['title'] = $item['link']['title'];
      if ($item['link']['in_active_trail']) {
        $class = ' active-trail';
      }
      // Keyed with the unique mlid to generate classes in theme_links().
      $links['menu-' . $item['link']['mlid'] . $class] = $l;
    }
  }
  return $links;
}

Comments

To clarify the purpose of this API (and please correct me if I am wrong)...

This prints out one level of the menu. If you specify a level other than '0' (or accept the default) it will give you that level for the child items of the current page only.

So basically, it might help if you want a block for child items relating to the current page (though Menu Blocks module is easier).

It is also useful if you want to pass one level of a menu through theme_links - as it sets it up right.

But it is not useful to get the entire menu, including sub-menus.

ps. please correct me if I am wrong!

I think you got it. Most themes will have the primary links and secondary links displaying only the first level of links.

This function is not used hardly at all in contrib. My guess is that it was written for core's menu_primary_links() and menu_secondary_links() with the idea that it would be extensible, but in practice isn't all that useful. :(

P.S. For showing L2 pages and below in a block, I have been using this PHP snippet:

<?php
// Get the Primary Links variable name
$active_menu = variable_get('menu_primary_links_source', 'primary-links');       
if (
$active_menu != "navigation") {
 
$tree = menu_tree_page_data($active_menu);
  foreach (
$tree as $key => $mi) {
    if (
$mi['link']['in_active_trail'] && $tree[$key]['below']) {
     
$menu = menu_tree_output($tree[$key]['below']);        
    }
  }
}
?>

But Menu Blocks does the same thing, and probably much nicer.

Hi, Every time I use this function it outputs the links with class as 'links'

<ul class="links">
<li> ...

Is it possible to change the class of the menu?