function theme_menu_item_link

You are here

4.6 menu.inc theme_menu_item_link($item, $link_item)
4.7 menu.inc theme_menu_item_link($item, $link_item)
5 menu.inc theme_menu_item_link($item, $link_item)
6 menu.inc theme_menu_item_link($link)

Generate the HTML output for a single menu link.

Related topics

2 theme calls to theme_menu_item_link()
menu_local_tasks in includes/menu.inc
Collects the local tasks (tabs) for a given level.
menu_tree_output in includes/menu.inc
Returns a rendered menu tree.

File

includes/menu.inc, line 1129
API for the Drupal menu system.

Code

function theme_menu_item_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }

  return l($link['title'], $link['href'], $link['localized_options']);
}

Comments

Use theme_links to theme primary links

I'm using ThemeKey to change the theme on a series of pages on my website that have a completely different theme from the rest of the site -- based on a parameter appended to the url. Some of these pages are nodes that must be able to be updated from the subtheme side, as well as the main theme. So I’m trying to hook into the menu_item_link in template.php, but it doesn’t like my code. I copied the applicable snippet from the zen theme template php, and modified it as below for my zen_dis sub theme -- It still seems to be looking at the main Zen theme for this option, though – how do I phrase the Hook_ language to force it to use this?:

function zen_dis_menu_item_link($link) {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}

// If an item is a LOCAL TASK, render it as a tab
if ($link['type'] & MENU_IS_LOCAL_TASK) {
$link['title'] = '' . check_plain($link['title']) . '';
$link['localized_options']['query'] = '?dis-site-search-organization';
$link['localized_options']['html'] = TRUE;
}

return l($link['title'], $link['href'], $link['localized_options']);
}

I put this on the the theme_menu_item page incorrectly, sorry for double-posting.. but i have found it helpful to have a class on links to pages that are not accessible.

(note: requires nodeaccess module)

add this code into template.php:

<?php
function yourThemeName_menu_item_link($link) {
  if (empty(
$link['localized_options'])) {
   
$link['localized_options'] = array();
  }

 

//check the target for access, assign class to nav items not accessible
   
global $user;
   
//assume non-accessible
   
$has_access = false;
   
    if(
$target = menu_get_object($type = 'node', $position = 1, $link['href']))
       
$has_access = node_access("view", $target, $user);   

   

//give access to off-site pages and dummy links
   
if($link['access_callback'] != "node_access_access" || $link['href']=="javascript:void(0)")
       
$has_access = true;   

    if(

$has_access)
       
$access_class = " submenu-has-access";
    else
       
$access_class = " submenu-no-access";

   

$link['localized_options']['attributes']['class'] .= $access_class;

    return

l($link['title'], $link['href'], $link['localized_options']);
}
?>

I wanted to remove the title attribute from all the <a href> in just one of my menus. Can't figure out a solution here with Drupal's awesomeness. Guess I am not awesome enough.

So... I went to jQuery and wrote this function. Put this jQuery code in a unfiltered block with visibility settings as you desire, or directly in one of your theme's tpl.php files.

<script>
$("div#foo ul#foo li a").each(function() {
    $(this).removeAttr("title");
});
</script>

Rather override theme_menu_item_link($link) in your template.php and remove the title attribute. The above function only shows a title tag, in case there is a description set. Check it out and uncomment the final print for debugging! Good luck ;)

<?php
function YOURTHEMENAME_menu_item_link($link) {
 
  if (empty(
$link['localized_options'])) {
   
$link['localized_options'] = array();
  }
 
 
$attributes = array();
 
 
// set link title
 
if ($link['description']) {
   
$attributes['title'] = $link['description'];
  }
 
 
// open all external links in a new window
 
if (substr($link['href'], 0, 4) == 'http') {
   
$attributes['target'] = '_blank';
  }
 
 
$link['localized_options']['attributes'] = $attributes;
 
//print '<pre>'. check_plain(print_r($link, 1)) .'</pre>';
 
 
return l($link['title'], $link['href'], $link['localized_options']);
}
?>