Community Documentation

theme_menu_tree

5 menu.inc theme_menu_tree($pid = 1)
6 menu.inc theme_menu_tree($tree)
7 menu.inc theme_menu_tree($variables)
8 menu.inc theme_menu_tree($variables)

Generate the HTML output for a menu tree

Related topics

File

includes/menu.inc, line 1142
API for the Drupal menu system.

Code

<?php
function theme_menu_tree($tree) {
  return '<ul class="menu">' . $tree . '</ul>';
}
?>

Comments

One Parameter menuname or menu Must be dynamic for menu_tree()

Hi,

I think menu_tree must pass some dynamic menu_id value to its theming function such as:-

<?php
function theme_menu_tree($tree,$menu_name="") {

  return
'<ul class="menu menu-$menu_name ">'. $tree .'</ul>';
}
?>

Or we will be able to override this in our template.php:-

<?php
function phptemplate_menu_tree($tree,$menu_name="") {
     return
'<ul class="menu menu-$menu_name ">'. $tree .'</ul>';
}
?>

In this way we can have different class associated with each custom menu.Currently only class menu is associated with all custom menus.

Thanks & Regards
-Inder Singh

There's nothing to stop you

There's nothing to stop you from additional arguments to the function signature in your theme function implementation. All arguments passed to theme() are provided to the implemented function.

i don't think this works,

i don't think this works, because he doesnt call the theme function himself.
"theme(menu_tree)" is called from "menu_tree_output", and there you can't give another param, cause it's core function.
i hope that sombody have a good idea, cause i have same problem.

peter

Only css

I did this with only scoping in css.

.left_block .menu{
background-color:#ff0000
}

Not exactly the same problem, but it may help someone.

This does not work in drupal 6

In Drupal 5, this function worked perfectly and simply returned a menu based on the unique menu id, regardless whether it was its own menu, or a sub menu of a larger menu.

In Drupal 6, this does not work at all. It simply returns the menu ID instead of the HTML formatted menu itself.

Anyone figured out the equivalent of this function for drupal 6?

did you have any succes with

did you have any succes with this? An menu id would also do for me, but i cant even get that.

Use theme() and menu_navigation_links() instead

The following works with Drupal 6.x:

<?php
print theme('links', menu_navigation_links('MENU-NAME'));
?>

Where MENU-NAME is the name specified for the menu (not the title!) with a prefixed "menu-". If you don't remember the menu name, it is the last part of the URL of the menu's edit page in the administration area. (The "menu-" prefix is already displayed there.)

Use theme() and menu_navigation_links() instead

The following works with Drupal 6.x:

<?php
print theme('links', menu_navigation_links('MENU-NAME'));
?>

Where MENU-NAME is the name specified for the menu (not the title!) with a prefixed "menu-". If you don't remember the menu name, it is the last part of the URL of the menu's edit page in the administration area. (The "menu-" prefix is already displayed there.)

Implementation for theming specific menu

First you have to create the basis link of the menu and put some information on it.
Here i will add 2 class to the menu :
- the mlid so that i can retrieve it easely : menu-mlid
- the name of the menu tree, exemple : secondary-links

<?php
function yourtheme_menu_item_link($link) {
  if (empty(
$link['localized_options'])) {
   
$link['localized_options'] = array();
  }
  if (empty(
$link['localized_options']['attributes']['class'])) {
   
$link['localized_options']['attributes']['class'] = 'menu-'. $link['mlid'];
  }
  else {
   
$link['localized_options']['attributes']['class'] .= ' menu-'. $link['mlid'];
  }
 
$link['localized_options']['attributes']['class'] .= ' ' . $link['menu_name'];
  return
l($link['title'], $link['href'], $link['localized_options']);
}
?>

Then i will add some class to the li html tag to only some specific menu item.
To do that, i'm getting the mlid with Php and then i use a IF to add the specific class

<?php
function yourtheme_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
 
$class = ($menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf'));
  if (!empty(
$extra_class)) {
   
$class .= ' ' . $extra_class;
  }
  if (
$in_active_trail) {
   
$class .= ' active-trail';
  }
 
// Find the mlid
 
$m = explode('menu-', $link);
 
$n = explode('"', $m[1]);
 
$mlid = $n[0];
 
 
// Menu Contact
 
if ($mlid == 10) {
    return
'<li class="contact">' . $link . $menu . "</li>\n";
  }
  return
'<li class="' . $class . '">' . $link . $menu . "</li>\n";
}
?>

And now if i wand to add specific class to the ul html tag, i'm doing the same as before.
Just by using the php function strpos.

<?php
function yourtheme_menu_tree($tree) {
 
// If it's secondary-links menu then add a <div> html
 
if (strpos($tree, 'secondary-links') !== FALSE) {
    return
'<div id="secondary-links"><ul>'.$tree.'</ul></div>';
  }
  return
"\n<ul class=\"menu\">\n" . $tree . "\n</ul>\n";
}
?>

It was helpfull for me so i hope you like this tips :)

You can get an extra

You can get an extra parameter by assigning a global variable scope in phptemplate_blocks function. An example can be seen here http://drupal.org/node/748018#comment-3950826

Real help

This is the best workaround for this dilemma, because it's as a very short and clear one and it is working: http://drupal.org/node/748018#comment-3981118

Fork

If you fork the application tree you can pass the menu name into this function. Begin by hooking theme_blocks in your template.php file. Change the function call block_list to block_list_alt, a copy of block_list that you've included in your module. Change the invocation of the menu module's block hook, menu_block, to menu_block_alt, a copy of menu_block that you've included in your module. Change the function call menu_tree to menu_tree_alt, a copy of menu_tree that you've included in your module. Change the function call menu_tree_output to menu_tree_output_alt, a copy of menu_tree_output that you've included in your module. Add $menu_name as a third parameter to the theme function call that hooks menu_tree. Finally, override menu_tree with theme_menu_tree in your template.php (e.g. framework_menu_theme file and add $menu_name as an optional second parameter.

Changing the menu module's block hook:

<?php
 
//Change the hook for the menu
 
if( $block->module == 'menu' )
  {
   
$hook = 'block_alt';
  }
  else
  {
   
$hook = 'block';
  }

 
//Invoke the module's block view
 
$array = module_invoke($block->module, $hook, 'view', $block->delta, $block->module . '-' . $block-delta );
?>

Adding a third parameter to the theme invocation of menu_tree:

<?php
 
return $output ? theme('menu_tree', $output, $menu_name) : '';
?>

Adding a second argument to a theme override of theme_menu_tree

<?php
 
//Reformat part of the menu generation
 
function framework_menu_tree( $tree, $name = '' )
  {
   
$html  = '<ul id="' . $name . '">' PHP_EOL;
   
$html .= $tree . PHP_EOL;
   
$html .= '</ul>' . PHP_EOL;

    return(
trim ( $html ) );
  }
?>

Login or register to post comments