Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I modify theme_menu_link for a specific menu?

I would like to add pipes ("|") between menu items for one of the menus on my Drupal 7 site (theme name is "thompson"). I figured the best way to do this is to create a function in my template.php file called thompson_menu_link. I did that and it is successfully modifying the menus, but it's changing all the menus. Is there a way I can do this for just one menu on my site?

Currently, I used the admin pages to add my footer menu (url path: menu-footer-menu) to the Footer block. Should I call it a different way?

like image 283
Brett F Avatar asked Aug 23 '11 17:08

Brett F


2 Answers

Apparently Drupal core provides the ability to theme menu links by menu name. For the Main menu the following theme function should work

THEMENAME_menu_link__main_menu()

Alternatively you could use the Menu Block module to create menu blocks. Among other things the module creates additional theme suggestions. From the module's README:

Menu block uses Drupal core's menu theme functions. However, it also provides theme hook suggestions that can be used to override any of the theme functions called by it. ...

  • theme_menu_link() can be overridden by creating one of:
    • [theme]_menu_link__[menu name]()
    • [theme]_menu_link__menu_block()
    • [theme]_menu_link__menu_block__[menu name]()
    • [theme]_menu_link__menu_block__[block id number]()
like image 65
daniels Avatar answered Nov 14 '22 13:11

daniels


I've messed with the thompson_menu_link() function a bit. I don't like how I did it, but it got the job done. Basically, it reads in the menu name, and uses a conditional to return the <li> element with a pipe afterward. Here's the whole block:

function thompson_menu_link(array $variables) {
  $element  = $variables['element'];
  $menuName = $variables['element']["#original_link"]["menu_name"];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }

  $output = l($element['#title'], $element['#href'], $element['#localized_options']);

  if ($menuName == "menu-footer-menu" && !in_array("last",$element['#attributes']['class']) {
    $finalString = '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>|\n";
  }
  else {
    $finalString = '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
  }

  return $finalString;
}
like image 2
Brett F Avatar answered Nov 14 '22 12:11

Brett F