By work77 on
I'm working on my first theme, and I need to zebra strip just the upper-most main nav menu items. This following function from includes/menu.inc is what I need to modify. Modifying it there works great, but to do things the drupal way, I'm a bit confused. Do I understand correctly that I just need to place my replacement function into the template.php code, change the function name, and clear the cache (...Performance/Clear Cache)? It didn't work for me. My theme is named aic. So I renamed the function to 'aic_menu_tree_output'. Modified the code. Cleared the cache. And it doesn't work. My changes are ignored. Any suggestions? Thanks.
function menu_tree_output($tree) {
$output = '';
$items = array();
// Pull out just the menu items we are going to render so that we
// get an accurate count for the first/last classes.
foreach ($tree as $data) {
if (!$data['link']['hidden']) {
$items[] = $data;
}
}
$num_items = count($items);
foreach ($items as $i => $data) {
$extra_class = NULL;
if ($i == 0) {
$extra_class = 'first';
}
if ($i == $num_items - 1) {
$extra_class = 'last';
}
$link = theme('menu_item_link', $data['link']);
if ($data['below']) {
$output .= theme('menu_item', $link, $data['link']['has_children'], menu_tree_output($data['below']), $data['link']['in_active_trail'], $extra_class);
}
else {
$output .= theme('menu_item', $link, $data['link']['has_children'], '', $data['link']['in_active_trail'], $extra_class);
}
}
return $output ? theme('menu_tree', $output) : '';
}
Comments
-
bump. please.
-
double bump
...
That's because menu_tree_output() isn't a theme function. So you can't override it. Theme functions ("themeable functions") always start with "theme_". If a function doesn't, then it isn't a theme function (and therefore can't be overriden in your template.php).
So you need to call your aic_menu_tree_output() directly.
For example, put the following PHP code in a custom block:
=====
Note that Drupal isn't perfect. In a perfect world you should be able to just override theme_menu_item() and theme_menu_tree() (note that these two functions do start with "theme_") instead of re-inventing menu_tree_output(). However, only in Drupal 7 you'll be able to easily override these two functions for a specific menu ('navigation' only, say). Otherwise you'll alter the appearance of all menus.
=====
(Your plan was to use tables, right? I'm not good at CSS, but perhaps in the PHP side you could just add 'odd' and 'even' classes to menu items (and leave them as <li>), and in your CSS sheet color them differently for the 'navigation' menu only. I also meantioned adding the classes using jQuery instead of PHP, but that's only for those not comfortable with PHP (because this method has drawbacks; e.g. needlessly linking to jquery.js). BTW, do most browsers support the :nth-child() pseudo class?)
-
Awesome. That brought it all together for me. It works! I'm just curious how you knew that it all started with the menu_tree_page_data function. You mentioned that "drupal" calls that. How did you know that the call was made by drupal and not some other function, which was called by another function, from another and on and on? That's what I'm having a hard time with - tracing back to the root function call.
Thanks.