| 7 menu.inc | theme_menu_link(array $variables) |
| 8 menu.inc | theme_menu_link(array $variables) |
Returns HTML for a menu link and submenu.
Parameters
$variables: An associative array containing:
- element: Structured array data for a menu link.
Related topics
File
- includes/
menu.inc, line 1625 - API for the Drupal menu system.
Code
function theme_menu_link(array $variables) {
$element = $variables['element'];
$sub_menu = '';
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}
Comments
Add unique class (mlid) to all menu items.
PermalinkI needed to style a few random menu links. Main and secondary menus has a great class .menu-123 (.menu-mlid) to do that. So here how you can port the same behavior to any/all menu items:
<?php/**
* Add unique class (mlid) to all menu items.
*/
function superseven_menu_link(array $variables) {
$element = $variables['element'];
$sub_menu = '';
$element['#attributes']['class'][] = 'menu-' . $element['#original_link']['mlid'];if (
$element['#below']) {$sub_menu = drupal_render($element['#below']);
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}
?>
Wouldn't this be more concise and flexible?
PermalinkOr is this the wrong way to do it?
<?phpfunction superseven_menu_link(array $variables) {
$variables['element']['#attributes']['class'][] = 'menu-' . $variables['element']['#original_link']['mlid'];
return
theme_menu_link($variables);}
?>
i've found this works
Permalinkflush caches:
hook_menu_link_alter($variables){var_dump($variables);
}
I've added the mlid into the list elements as an ID using this..
Permalink/**
* Add unique ID (mlid) to all menu items.
*/
function THEMENAME_menu_link(array $variables) {
$element = $variables['element'];
$sub_menu = '';
$element['#attributes']['id'][] = 'menu-' . $element['#original_link']['mlid'];
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
return '
\n";
}
Analog from Drupal 6
Permalinkhttp://api.drupal.org/api/drupal/includes--menu.inc/function/theme_menu_...
Also with menu title as class
Permalink<?php/**
* Add unique class (mlid) to all menu items.
* with Menu title as class
*/ function yourtheme_menu_link(array $variables) {
$element = $variables['element'];
$sub_menu = '';
$name_id = strtolower(strip_tags($element['#title']));
// remove colons and anything past colons
if (strpos($name_id, ':')) $name_id = substr ($name_id, 0, strpos($name_id, ':'));
//Preserve alphanumerics, everything else goes away
$pattern = '/[^a-z]+/ ';
$name_id = preg_replace($pattern, '', $name_id);
$element['#attributes']['class'][] = 'menu-' . $element['#original_link']['mlid'] . ' '.$name_id;
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}
?>
Thank you
PermalinkHey thanks it works great!
Definition of $element
PermalinkDo you know where I can get the $element array definition?
Thanks!
adding level deph as class
Permalink<?php
function [your_theme]_menu_link(array $variables) {
$element = $variables['element'];
$sub_menu = '';
$element['#attributes']['class'][] = 'level-' . $element['#original_link']['depth'];
if (
$element['#below']) {$sub_menu = drupal_render($element['#below']);
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}
?>
so that's how!
PermalinkI've spent half a day trying to get the depth when I've stumbled across your example with
$element['#original_link']['depth']. Thanks, that solved a huge problem on my part!How to add class to the menu himself
PermalinkI wanna add class to the whole menu: not to the "il", but to "ul".
How to do its?
theme_menu_tree
PermalinkCheck out theme_menu_tree
My Name is PJ
Use theme_links() to add class to <ul>
Permalinktazir1: @see attributes at theme_links()
Theme only a specific menu block.
PermalinkThis gave me some trouble so I thought I'd share.
We needed to style only a specific menu block (top nav) but not the sidebar nav that was generated off of the same menu.
So theme_menu_link__main_menu() did not work for us.
However, I was able to target only the block I needed using the menu block module and theme_menu_link__menu_block__[block-number]()
described under "Theme Menu functions".
Hopefully this helps some one else as well.
Here is how to replace ul
PermalinkHere is how to replace ul class .menu to .sf-menu. for the main menu.
add this to your template.php
function YOUR-THEME-NAME_menu_tree__main_menu($variables) {
return '
' . $variables['tree'] . '
';
}
page.tpl
Permalinkin bartik page.tpl.php main menu is rendered using theme_links and not theme_menu_link, this is why you need to implement theme_links__system_main_menu
Print out variables
PermalinkI found this helpful when customizing the link info.
<?php//print out variables for the link array
function theme_menu_link(array $variables) {
return print_r($variables['element']);
}
?>
Targeted Override
PermalinkI don't see it documented elsewhere that you can implement a
THEMENAME_menu_link__MENU_NAME()function as an override for a specific menu.See http://drupal.org/node/254940#theme-suggestions-for-menus.
Just add the menu name and your theme name
Permalinkwgsimon, to target specific menu, use the name of the menu from admin/structure/menu page (replace any hyphens with underscores) and append your theme name to the beginning of the function.
Make any menu specific changes to the body of the function and you are done!
Clear theme registry
PermalinkOnce you are ready to view your theme's override function in action, remember to clear your theme registry.
If you have Devel module installed, turn on "Rebuild the theme registry on every page load" option also helps.
I can not modify the menu template
PermalinkI can not modify the menu template.
I clear the cache site and I use devel module and the option "theme registry" without results.
With print_r I get this:
[# theme] => Array
(
[0] => menu_link__menu_block__5
[1] => menu_link__menu_block__menu_product_menu
[2] => menu_link__menu_block
[3] => menu_link__menu_product_menu
[4] => menu_link
)
In the name of my function, if I use "Mytheme_menu_link__menu_block__5" or "Mytheme_menu_link__menu_block__menu_product_menu" or "Mytheme_menu_link__menu_product_menu" that does not work.
Only the function name "Mytheme_menu_link" works.
Do you have an idea?
Thank you for your help.
Drupal 7.14
Superfish menu item
PermalinkDoes anyone knows how to do this for superfish menu item? It seems the theme_menu_link function is never called.
Override not working
PermalinkAs same for @kumkum29 and @richsky
No override code is working
themeName_menu_link__main-menu
or themeName_menu_link__main_menu
This patch is already there http://drupal.org/node/1001146
I want to implement it for superfish menu too.
Superfish
PermalinkI found that adding extra html or css to superfish is super easy.
Basically you edit your Superfish block at [site]/admin/structure/block/manage/superfish/1/configure.
Then in the Advanced HTML and Advanced CSS sections towards the bottom, you can add HTML before and/or after each menu level, and give specify names in the CSS section. In the HTML section, use the comma to separate the code that goes AFTER the specific level of HTML. It works great!
"Muting" Menu Links With a Fake URL Fragment
PermalinkI just got to "disable" some menu items' links (while keeping them showing) on my main menu by doing this:
<?phpfunction THEMENAME_menu_link(array $variables) {
$element = $variables['element'];
$sub_menu = '';
// Menu links with id's included in this array will get muted
$muted_mlids = array(577, 578, 579);
if (in_array($element['#original_link']['mlid'], $muted_mlids)) {
// Get it pointing to the current page
$element['#href'] = current_path();
// Ideally should just use an empty fragment, but Drupal ignores it if set like that
$element['#localized_options']['fragment'] = 'none';
}
if (
$element['#below']) {$sub_menu = drupal_render($element['#below']);
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}
?>
Injecting HTML
PermalinkIf you need to inject HTML into the title, set the HTML attribute like so for each element that needs parsing.
$variable['#localized_options']['html'] = 1;Here's the code that I used:
<?php/*
* Implementation of hook_menu_link().
*/
function THEME_menu_link(array $variables) {
foreach($variables as &$variable) {
$variable['#title'] = THEME_replace_words($variable['#title']);
$variable['#localized_options']['html'] = 1;
}
return
theme_menu_link($variables);}
?>
Amarjit
Doesn't seem to be working
PermalinkDoesn't seem to be working for primary and secondary links under zen subtheme. Or am I missing an obvious different hook I should be using for that?
Theming main_menu in a Zen subtheme
PermalinkHi,
It is working for me if I follow these steps (maybe could be done in a simplest way, but I don't know how).
to
themes/my_zen_subtheme/templates/page.tpl.php
function my_zen_subtheme_links__system_main_menu(array $variables) {// We can debug a little using kpr (similar to dpm)
kpr($variables);
// Changing title menu for one link (hardcoded, you should add logic to get the menu element you want)
$variables['links']['menu-1920']['title'] = "My new title";
return theme_links($variables);
}
It is useful to read the page.tpl.php line 141.
The structured data of $variables['element']
PermalinkI'm new to Drupal and find it profoundly frustrating that the input parameter for this function is glossed over as "structured array data for a menu link" as if it is not relevant to the reason one might seek this documentation. Here is what I found $variables['element'] to consist of (in this case, just an external link to facebook):
Array(
[element] => Array
(
[#theme] => menu_link__social_chicklets
[#attributes] => Array
(
[class] => Array
(
[0] => first
[1] => leaf
)
)
[#title] => facebook
[#href] => https://www.facebook.com/
[#localized_options] => Array
(
)
[#below] => Array
(
)
[#original_link] => Array
(
[menu_name] => social_chicklets
[mlid] => 397
[plid] => 0
[link_path] => https://www.facebook.com/RogerCPAReview
[router_path] =>
[link_title] => facebook
[options] => Array
(
)
[module] => menu
[hidden] => 0
[external] => 1
[has_children] => 0
[expanded] => 0
[weight] => 0
[depth] => 1
[customized] => 0
[p1] => 397
[p2] => 0
[p3] => 0
[p4] => 0
[p5] => 0
[p6] => 0
[p7] => 0
[p8] => 0
[p9] => 0
[updated] => 0
[load_functions] =>
[to_arg_functions] =>
[access_callback] =>
[access_arguments] =>
[page_callback] =>
[page_arguments] =>
[delivery_callback] =>
[tab_parent] =>
[tab_root] =>
[title] => facebook
[title_callback] =>
[title_arguments] =>
[theme_callback] =>
[theme_arguments] =>
[type] =>
[description] =>
[in_active_trail] =>
[access] => 1
[href] => https://www.facebook.com/
[localized_options] => Array
(
)
)
[#children] =>
)
[theme_hook_suggestions] => Array
(
)
)