欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

How to theme a menu block?

shiping1 的头像

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?

shareimprove this question
 
   
This is likely a duplicate of drupal.stackexchange.com/questions/2258/… –  tim.plunkett Jul 24 '11 at 1:43
1 
The other question is about adding a unique menu ID, while this question is more generic. –  kiamlaluno Jul 24 '11 at 13:47
   
Exact question on stackoverflow: stackoverflow.com/questions/11935477/… –  claws Feb 24 '14 at 8:11

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.

shareimprove this answer
 

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) {

  }
shareimprove this answer
 
   
MYTHEME_menu_link seems to only handle the individual links. I can't reach the ul. –  RoToRa Jul 18 '11 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 '11 at 13:43
来自 http://drupal.stackexchange.com/questions/7274/how-to-theme-a-menu-block
普通分类: