2

The Menu Block offers several hooks to override the markup it generates:

#theme:
  menu_link__menu_block__1
  menu_link__menu_block__main_menu
  menu_link__menu_block
  menu_link__main_menu
  menu_link

However, the variables don't seem to be passed by ref, and if I don't return any string, the links render out null. So, what should I do if I want to just append a custom class to the links and then render them just the default way?

I tried something like in template.php:

function MYTHEME_menu_link__menu_block($variables){
  $variables['element']['#attributes']['class'][] = 'my-class';
  return theme('menu_link__menu_block', $variables['element']['#original_link']);
}

but it causes fatal error.

Any help please?

0

You would need to copy the whole function. There is a good example on the theme_menu_link() page.

You could also check out the Menu attributes module which allows you to add classes to individual menu items.

  • Yes, I know about Menu attributes but I want to avoid that there's-a-module-for-that craziness as much as possible :) I prefer to learn how to squeeze the maximum out of template.php. So, is the decision up to module author whether to pass arguments by ref or by value? Or is it imposed by Drupal's architecture? – Artur Dec 11 '12 at 18:46
  • I believe that's imposed. I have used this approach many times in the past without any issues. – enzipher Dec 11 '12 at 20:52
0

If you need to add class to the anchor link then use this module. menu_attributes

If you need to add class in li then use theme_menu_link

function YOURTHEME_menu_link__main_menu($variables){}  

If you need to add class in ul then use theme_menu_tree/

function YOURTHEME_menu_tree__main_menu($variables){}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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