Hi!

The site I'm developing has a couple of menus, styled differently from each other. The top nav HTML I'm working from looks like this:

<ul id="mainnav">
  <li><a href="#" class="selected">Om Myndigheten</a></li>
  <li><a href="#">Resurser &amp; Material</a></li>
  <li><a href="#">Projekt</a></li>
  <li><a href="#">Aktuellt</a></li>
</ul>
<ul id="teach_press">
  <li><a href="#">F&ouml;r L&auml;rare</a></li>
  <li><a href="#">F&ouml;r Press</a></li>
</ul>

So I need to put a different id on every menu. Is there a clever way to do this without hacking the core?

Comments

pommac’s picture

I poked around a bit and modified template.php to include this function:

function phptemplate_menu_tree($pid = 1) {
  if ($tree = menu_tree($pid)) {
    return "\n<ul class=\"menu\">\n". $tree ."\n</ul>\n";
  }
}

So at least now I know how to override the ul! The question is, what value can I use as id? I have full access to the css, so as long as it's something unique that stays constant it'll work for me. Any ideas?

pommac’s picture

Well, I'm coming a little closer and this is the best I've come up with so far:

function phptemplate_menu_tree($pid = 1) {
  if ($tree = menu_tree($pid)) {
    return "\n<ul id=\"menu-{$pid}\" class=\"menu\">\n". $tree ."\n</ul>\n";
  }
}

Thus I get id's looking like "menu-65" etc and I can use that. But I don't think the pid is constant, is it? It won't be 65 when I've moved my theme to the production server and my client adds the "real" menu, will it? One way to fix that might be to grab the name of the menu and shake it up a little. For instance, one of the menus is called "Main Navigation": I could use a lowercase variant of that without spaces, grabbing the first 8 characters or so. It would become "mainnav" and that's both usable and portable.

So how can I extract the name of the menu being rendered? Anyone know?

pommac’s picture

Well, I keep replying to my own comments, but hopefully someone might find this useful in the future :)

function phptemplate_menu_tree($pid = 1) {
  if ($tree = menu_tree($pid)) {
    $menu = menu_get_menu();
    $classname = str_replace(" ", "", strtolower($menu['visible'][$pid]['title']));
    return "\n<ul id=\"{$classname}\" class=\"menu\">\n". $tree ."\n</ul>\n";
  }
}

This function in template.php will return an easy-to-use id for the css!

jeffrey.dalton’s picture

Can't thank you enough for taking the time to come back and post this! Have been looking for this solution for about a week now. Peace

Jeffrey Dalton
Creative Director & Founding Partner
BKJ Digital
https://bkjdigital.com

ha5bro’s picture

You sir, are magnificent. Just happened across this looking for a solution to something else, worked like a charm. Go pommac!

[[[ ALL HAIL THE ALMIGHTY GOOGLEBOT ]]]

theohawse’s picture

This is great, however I think for anyone trying to use this may run into issues with things already being declared.
My site was already using another phptemplate_menu_tree snippet, so this one wouldn't work for me right away.
(Tried merging the two snippets but they didn't play well)

By changing the first line ->
from this: function phptemplate_menu_tree($pid = 1)
to this: function theme-name-goes-here_menu_tree($pid = 1)
-should help you out in a tricky situation.

You can use firebug to see implementation at:
-- http://www.simplespot.ca --

invi’s picture

Does this solution work in Drupal 6? I tried this piece of code and there was no output. Maybe it is a problem with $pid because I checked that this new function works but the if condition is not [I tried to echo some output and it didn't show and above the if there is the output]. Maybe someone could help me to adjust this function to D6?

Cheers

Invi

drupalgeek’s picture

Refer this link on how to override drupal 6 menus for theming. Here, you get an idea on how to override menu_tree, menu items and menu links.