0

I'm trying to output a menu with all of its submenus into a template file, currently I'm using the code below. The problem is that it only displays the top level links. If I pass the second $level parameter into menu_navigation_links it'll then only display that one level, not any of the others above or below.

How can I display all the different menu levels & still have the ability to add classes to the as below? The title isn't really that important as I can 'hard code' that tag into the template file.

print theme('links__menu_top_page_navigation', array('links' => menu_navigation_links('menu-top-page-navigation'), 'attributes' => array('id' => 'main-menu', 'class' => array('main-menu', 'list-menu')), 'heading' => array('text' => t('Main menu'),'level' => 'h2','class' => array('main-menu-title'))));

The closest thing I've found so far is to use menu_tree_all_data() & menu_tree_all_output() but I cannot see how to edit the class using that.

  • Is performance an issue? Are you OK with contrib modules for the job? – Mołot Jun 23 '15 at 13:59
  • I'd prefer to avoid using contrib modules as there are quite a number installed on the site. – Neil Nand Jun 23 '15 at 14:00
  • In that case I don't have a fast answer for you, sorry. – Mołot Jun 23 '15 at 14:02
  • No worries, if you know of any other ways I'd welcome them when you have the time. – Neil Nand Jun 23 '15 at 14:03
  • do you have a variable depth to the menu? or can you just reprint that line for each level (depending on the exact layout you are looking for)? – Geoff Jun 24 '15 at 5:34
0

I've managed to find an answer in a Stack Overflow answer https://stackoverflow.com/a/30570027/1094862

For completeness I've copied the relevant code below:

$main_menu_tree = menu_tree_all_data('menu-name', null, 3);
render_menu_tree($main_menu_tree);

function render_menu_tree($menu_tree) {
  print '<ul>';
  foreach ($menu_tree as $link) {
    print '<li>';
    $link_path = '#';
    $link_title = $link['link']['link_title'];
    if($link['link']['link_path']) {
        $link_path = drupal_get_path_alias($link['link']['link_path']);
    }
    print '<a href="' . $link_path . '">' . $link_title . '</a>';
    if(count($link['below']) > 0) {
      render_menu_tree($link['below']);
    }
    print '</li>';
  }
  print '</ul>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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