Could somebody clear up the differences between http://api.drupal.org/api/drupal/includes!menu.inc/function/menu_navigat... versus http://api.drupal.org/api/drupal/includes!menu.inc/function/menu_tree_ou... ?

When should one be used rather than the other? Why are two so similar functions both used?

I'm trying to do something fancy, override the theme of a specific menu item. This works correctly when menu_tree_output() builds its render array, as it has code to "Allow menu-specific theme overrides" But themes tend to render menu items using menu_navigation_links(), which breaks my fancy feature. That function does not allow menu-specific theme overrides and I wonder is it a bug that one of these functions supports it and not the other?

Comments

Anonymous’s picture

Status: Active » Postponed (maintainer needs more info)

You probably just want to use an implementation of one of the alter hooks available for menu. Maybe simply adding class items to them via hook_menu_alter?

Dave Cohen’s picture

Status: Postponed (maintainer needs more info) » Active

Right, I'm aware that drupal has multiple ways to tweak menu items. I'm using what is probably the trickiest, hook_translated_menu_link_alter(). I'm using that because I need to change the menu link based on what the current URL is. I learned this trick from devel.module and my code is very similar to what you'll see there.

In my hook_translated_menu_link_alter() I tried setting $item['menu_name'], because that's the only way to custom theme a menu link. menu_tree_output() will use theme('menu_link__' . $item['menu_name']) to theme the link. Whether you consider this technique a hack or not, I had my item markup just the way I wanted it, or so I thought.

It turns out that menu_tree_output will use that theme function to render the link. Whereas menu_navigation_links() will not. So when my item was in a menu render as a block in a siderbar, it was themed exactly as I wanted. But when the menu item is moved to the main menu o the user menu, it was not rendered as I wanted, because Drupal 7's default theme using menu_navigation_links() to display those menus.

So my support request is not "how do I alter a menu?" Rather, it is why does Drupal use two different ways to render menus? Is that a bug (i.e. should one be deprecated)? and When can a module developer expect which function to be used? I think at the very least the documentation for both functions should be edited to include a reference to the other function so that people such as myself will more quickly learn there's more than one way to render a menu.