This article explains how to modify Drupal system to enable identification of every element in Navigation menu.
This may be used to do some additional work in JavaScript or just add more personalization to menu items through CSS.
The code for the menu item with submenu looks by default like this:
<li class="expand">
<a href="/link_to_menu_item">Menu item name</a>
<ul class="menu">
<li class="leaf">
<a href="/link_to_submenu_item">Submenu item title</a>
</li>
<!-- more submenu items -->
</ul>
</li>
It is created by two functions defined in file includes/menu.inc – theme_menu_item (li) and theme_menu_item_link (a).
Because Drupal 5.x provides very functional theme coding support there is no need to change any of its PHP functions. It is just enough to edit appropriate theme file and in there define replacement functions.
Depending of template code functions may be found in themes/yourtheme/yourtheme.theme or themes/yourtheme/template.php and they will be called yourtheme_menu_item or phptemplate_menu_item. (Just change „yourtheme“ to your real theme name).
The best way to add unique information is to use an XHTML id attribute. It is useful to identify both – li and a tags. Let's say li id attribute will start with „li_“ and a's one – with „a_“.
function yourtheme_menu_item($mid, $children = '', $leaf = TRUE) {
$item = menu_get_item($mid);
$link_item = $item;
while ($link_item['type'] & MENU_LINKS_TO_PARENT) {
$link_item = menu_get_item($link_item['pid']);
}
$link = '';
return '<li id="li_' . str_replace('/','_',$link_item['path']) . '_' . str_replace(' ','_',$item['title']) . '" class="'. ($leaf ? 'leaf' : ($children ? 'expanded' : 'collapsed')) .'">'. menu_item_link($mid) . $children ."</li>\n";
}
function yourtheme_menu_item_link($item, $link_item) {
return l(
$item['title'],
$link_item['path'],
array(
'title' => !empty($item['description']) ? $item['description'] : '',
'id' => 'a_' . str_replace('/','_',$link_item['path']) . '_' . str_replace(' ','_',$item['title'])
),
isset($item['query']) ? $item['query'] : NULL);
}
The functions above will create such code:
<li id="li_node_5_Menu_item_name" class="expanded">
<a id="a_node_5_Menu_item_name" title="" href="/link_to_menu_item">Menu item name</a>
<ul class="menu">
<li id="li_link_to_submenu_item_Submenu_item_name" class="leaf">
<a id="a_link_to_submenu_item_Submenu_item_name" title="" href="/link_to_submenu_item">Submenu item name</a>
</li>
<!-- more submenu items -->
</ul>
</li>
Now it is possible to change elements style with simple CSS:
#li_node_5_Menu_item_name a { color: red }
#a_node_5_Menu_item_name { color: green }
This solution is not perfect – the str_replace functions should use more sophisticated method to remove not wanted characters. If you will create it – feel free to drop me a note. You may always use the $mid variable, but it's just a number and will create something like id=‚li_34‘.









Comments
I never thought that drupal is very easy to use and user friendly than the others. real estate toronto
Impressive article, thanks for the share makeup artist schools
I've been looking for this information for a couple of an hour, and now I finally found it.pmp questions free
J'ai besoin de mentir ma fin de votre acquisition et d'informer pour agir noèse emplois lecteurs de la plosive à la fin. Je voudrais ce genre pour faire semblant nouveaux postes et à des actifs mes pensées avec vous.
Canon Powershot SX30 It's good to prioritize plans and goals in life.
Cheap Nike Air Max shoes
Thanks for sharing. Amazing page indeed. truly great! significato sogni
It's good to prioritize plans and goals in life.
Penny Auctions
Inevitable Meaning and Definition – unable to be avoided, evaded, or escaped; certain;necessary: an inevitable conclusion.
Friction For Kids – What is friction? Friction is actually what goes on whenever any two matters stroke with each other.
Smudge Cells definition – immature leukocytes of any type that have undergone partial breakdown during preparation of a stained smear or tissue section, because of their greater fragility.
Dan Gable quotes – Various quotes from Dan Gable „I shoot in, I get 2 points. He shoots in, I get 2 points“
This is kinda a huge help for me. thanks. reverse home mortgages
Thanks for sharing. Amazing page indeed. truly great! average insurance
Seems I could use this on my next project based on Drupal. Thanks.
I bookmarked this link. Thank you for good job!
Interesting solution sir. I have a problem though. Our ipad keyboard case page is in drupal but I keep getting an error message. Any idea?
pants for work , I bookmarked this link. Thank you for good job!
buy cheap viagra online , cheap order viagra