I've been trying to figure out how to customize a menu that is generated by the system. I currently have the menu set up as a block for the sake of simplicity, but I'd like to be able to incorporate it into the Drupal menu system.
I have set up a duplicate menu as the one I have in the block at the moment, and have been trying to configure the output of the HTML generated for the menu itself.
Rather than have the output be the default:
<li class="leaf"><a href="/page" title="Page Desc">Page title</a></li>
I'd like to have the href wrapped in a header tag, in this particular case, H2's would wrap the "a" tag. However, I DO NOT need or want this to apply to every generated menu, but only a single specific menu.
I can understand how to do this easily if I wanted to modify the theme_menu_item in my own template.php file, and apply the H2 wrapper to EVERY menu item in every menu, but I'd like to be able to reference this particular menu by ID or something similar to only theme this specific menu, and allow the normal functionality for other menus.
/**
* Generate the HTML output for a menu item and submenu.
*
* @ingroup themeable
*/
function theme_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';
}
return '<li class="'. $class .'">'. $link . $menu ."</li>\n";
}
Thanks for any help in advance!
Comments
There's no clean way to
There's no clean way to target specific menu items. It's possible to work around but it would be a major hack degrading performance.
This is one of those situations where regex is needed to scour the strings. eh.
This could be an easy fix in 7 for core.
⎋ joon park
should pass menu-id during theme_item_link
I didn't see one from the functionality... lame
It would be nice, and seem easy enough if the function passed more than just the pre-build html for the links. If it passed an array, or at least a property that defined the menu-id that was currently being operated on, it would make this situation easy... something similar to using the form_alter callback, you can simply check for the name of the form-id, and then operate appropriately.
Then someone could just use a simple if/else statement to determine the menu-id, and apply the appropriate format for each based on that menu id, and it's sub-items.
For now, I'll just go with my block items, as they look and render correctly, I just really wanted to manage them from the menu system for ease of change/management, etc. The new menu management with drag-drop is so nice, who wouldn't want to use it as much as possible!
Jake Strawn
Himerus Inc. - www.himerusinc.com
What about primary & secondary menus?
Would it be possible to do something like this for the Primary or Secondary menus?
The menu in question is currently not either of these, but could be if it were possible to separately modify the system menus in a different way that then custom menus.
Jake Strawn
Himerus Inc. - www.himerusinc.com
I believe that part was
I believe that part was fixed. There was a really old issue on theme_menu_item just for this reason before 6 was released. We didn't have enough reviewers though and it slipped through the cracks. That's what I remember of it anyways.
⎋ joon park
Primary menu can be?
So, it is possible to separately theme the primary/secondary menus?? I didn't quite get that from the reply...
I hope something like this can be considered for a future release... modifying the menus in this way would make the system completely perfect in my opinion. I use UL LI menus in a lot of different ways, and enjoy the way drupal compiles those menus...
If it is possible in the primary/secondary menus, could you point me in the right direction to re-theme the particular function in question?
I did find one function, but didn't see how to really manipulate this, and couldn't override the function in my own template.php:
Thanks for all your help...
getting mlid in theme_menu_item
Check out issue #323494: Menu item info in theme_menu_item and help test & improve...
Unique classes for menu links
Not directly related, but this definitely helped with getting unique identifiers for menu links for easier CSS selection: http://adaptivethemes.com/how-to-add-unique-classes-to-drupal-menus
Checkout this complete
Checkout this complete tutorial on drupal 6 theming of menus by overriding functions in template.php. Here, you get an idea on how to override menu_tree, menu items and menu links.
Check out...
Had a similar issue trying to assign unique css identifiers and img tags for each link item in a menu. Ended up using the $primary_links menu and using the instructions on this node to do it. I know it says 4.7/5, but it worked on my 6.x installation.
--Alec
Tandem