[support] Menu question

sander-martijn sander at sander-martijn.com
Thu Aug 16 22:31:00 UTC 2007


I want to customize my menus, but since I want it to work with the 
html/css/javascript code I've already written and tested I want to get 
the system to output the menu in html as I want it.  Actually what I 
need is quite simple.  I don't really want to try to customize someone 
else's module and I'd rather not build one.  After some digging around I 
figured out that I could override the theme_menu_links method in my 
template.php file.  Now that's exactly what I need... I got really 
excited but I must have some things missing/misunderstood.


what I have in the menu system under primary links:
primary links
  - menu item 1
     - menu 1 subitem 1
     - menu 1 subitem 2
  - menu item 2
     - menu 2 subitem 1

etc - fairly standard

What I need to output is the following:

<ul id="nav1" class="nav">
  <li><a href="#">menu item 1</a>
    <ul>
      <li><a href="#">menu 1 subitem 1</a></li>
      <li><a href="#">menu 1 subitem 2</a></li>
    </ul>
  </li>
</ul>
<ul id="nav2" class="nav">
  <li>menu item 2
    <ul>
      <li><a href="#">menu 2 subitem 1</a></li>
    </ul>
  </li>
</ul>

etc. - also pretty straight forward.

SO - I put in page.tpl.php the following:
<?php print theme('menu_links', $primary_links); ?>

and put in template.php the following:
function tpg_menu_links($links){
  if (!count($links)) {
    return '';
  }
  $level_tmp = explode('-', key($links));
  $level = $level_tmp[0];
  $output = "<ul id=\"nav\" class=\"nav\">\n";
  foreach ($links as $index => $link) {
    $output .= "<li>". l($link['title'], $link['href'], 
$link['attributes'], $link['query'], $link['fragment']) ."</li>\n";
  }
  $output .= '</ul>';

  return $output;
}
?>

Which is basically a modified version of theme_menu_links in menu.inc

It works as a start, but there are a couple of issues. 
1. even though my class attribute in $output is hard coded, it's still 
being replaced by class="active" when you're on the page.  Not a 
disaster, i can always modify my css to do the same thing for 
class="active" as class="nav".
2. this is the bigger issue.  It's not outputting the subitems.  I'd be 
happy to add in the proper call in the foreach loop to either call 
another function that i also override or to load them directly in here 
if anyone can point me in the direction of what i need to call in order 
to load them.




More information about the support mailing list