I'm creating a custom menu for a project built in Drupal 7 and I want to add some custom HTML before every UL in the main menu.

By adding the THEMENAME_menu_tree__main_menu($variables) function to my template.php I can output my custom HTML:

return '<div>My custom HTML</div><ul class="main-menu">' . $variables['tree'] . '</ul>';

The function is called for every level in the menu and the HTML printed in front of every UL. But now I also want to customize this HTML in different ways for each level, but I cannot find any information regarding the current menu level in the $variables parameter.

Is it at all possible to access information about current menu level in the body of the function? If so, how?

1 Answer 1


Looks like this question is way past its expiry date, but who knows this might help someone :)

I must confess, I have never used this hook function before (THEMENAME_menu_tree__main_menu). I had a quick look by implementing it in my current project, and you are only getting back HTML. I think you are going to have a tough time styling individual items in the HTML you are getting back.

In my current project I render my menu a region template file (region--header.tpl.php to be exact). Obviously this means I have a 'header' region defined in my mytheme.info file.

So in page.tpl.php I have

  if (isset($page['header'])) {
    print render($page['header']);

which basically defers rendering to region--header.tpl.php In this file I:

$menu = menu_tree_all_data('main-menu');
foreach ($menu as $menuItem) {
  // Extract info I want and render
  // $markup = ......

$print $markup

Basically I build my own HTML that will render the menu.

I have a couple of 'utility functions' to get the data out of $menuItem - let me know if you are interested.

As a side note - if you just want to figure out what level you are on - you could always call menu_get_active_trail(). You should be able to figure out what 'level' you are on with the info in that array.

Hope this helps.

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.