11

I have created a custom menu (lets call it "mynav") and in my theme I've created a template specifically for it (block--menu--menu-mynav.tpl.php) which contains:

<div id="mynav" class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php print $content ?>
</div>

This works fine.

However I'd prefer to apply the id to the unordered list that is generated and contained in $content. I guess I need to create a function MYTHEME_links__system_mynav_menu like this:

function MYTHEME_links__system_mynav_menu($variables) {
  return "TEST"; // Generate HTML here (<ul id="mynav">...)
}

but it doesn't seem to have an affect.

What am I doing wrong?

3

2 Answers 2

25

In order to theme the unordered list, you need to call theme_menu_tree(). You can edit your template.php in order to call this.

function THEMENAME_menu_tree__MENUNAME($variables){
  return '<ul class="your-custom-class" id="your-custom-id">' . $variables['tree'] . '</ul>';
}

Then, if you want to theme your links, call theme_menu_link().

function THEMENAME_menu_link__MENUNAME($variables) {
  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

Now, the problem with Drupal 7 is that there is a major bug when it comes to displaying the active trail of custom menus.

See issue
Issue #520106 - No way to dynamically set active trail
Issue #942782 - Custom menus never receive an active trail

You are basically out of luck if you want some true custom menus. These issues are very long standing in the drupal community so I am not sure if they will be fixed anytime soon. I don't know if there is some PHP that can help with it. You can turn to some alternative modules in order to help ease functionaltiy, like Menu Attributes and Menu Block. They both can help get around the cruddy menu system in Drupal 7.

2

Inside your theme's template.php, this was the way to do things in Drupal 6

function MYTHEME_links__system_mynav_menu($variables) {
  return "TEST"; // Generate HTML here (<ul id="mynav">...)
}

In Drupal 7 you need to call

  function MYTHME_menu_link(array $variables) {

  }
2
  • MYTHEME_menu_link seems to only handle the individual links. I can't reach the ul.
    – RoToRa
    Jul 18, 2011 at 12:52
  • I'm looking into it, I don't know if you can anymore. I know with Drupal 7 you have more power over the links, but less power over the menus.
    – iStryker
    Jul 18, 2011 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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