menu_item_link() generates links for menus, it calls internal link format function l() defined in common.inc which returns anchor on a array in our case its menu items.

say if i want to add span tag within anchor how shoul we change the following default code and overwrite it in template.php

function theme_menu_item_link($item, $link_item) {
  return l($item['title'], $link_item['path'], !empty($item['description']) ? array('title' => $item['description']) : array(), isset($item['query']) ? $item['query'] : NULL);
}

Comments

nevets’s picture

Something like this

<?php
function phptemplate_menu_item_link($item, $link_item) {
  $title = '<span>' . $item['title'], . '</span>';
  return l($item['title'], $link_item['path'], !empty($item['description']) ? array('title' => $item['description']) : array(), isset($item['query']) ? $item['query'] : NULL, NULL, FALSE, TRUE);
}
?>

But if all you are using the span for is to add a class name you could do this instead

<?php
function phptemplate_menu_item_link($item, $link_item) {
  $attributes = array();
  if ( !empty($item['description'])  ) {
    $attributes['title'] = $item['description'];
  }
  $attributes['class'] = "some-class-name";

  return l($item['title'], $link_item['path'], $attributes, isset($item['query']) ? $item['query'] : NULL);
}
?>
mediamash’s picture

I want to add an active trail to menu_item_link (the A tag) , just like this code adds an $active_class to the LI tag

function phptemplate_menu_item($mid, $children = '', $leaf = TRUE) {
    $active_class = in_array($mid, _menu_get_active_trail()) ? ' active' : '';
  return '<li class="'. ($leaf ? 'leaf' : ($children ? 'expanded' : 'collapsed')) . $active_class .'">'. menu_item_link($mid) . $children ."</li>\n";
}

grtz
tom

nevets’s picture

How about this (notice the call to menu_item_link() is changed)

function phptemplate_menu_item($mid, $children = '', $leaf = TRUE) {
    $active_class = in_array($mid, _menu_get_active_trail()) ? ' active' : '';
  return '<li class="'. ($leaf ? 'leaf' : ($children ? 'expanded' : 'collapsed')) . $active_class .'">'. menu_item_link($mid,TRUE) . $children ."</li>\n";
}

function phptemplate_menu_item_link($item, $link_item) {
  $attributes = array();
  if (  !empty($item['description']) ) {
    $attributes['title'] = $item['description'];
  }

  if ( in_array($item['mid'], _menu_get_active_trail()) ) {
    $attributes['class'] = 'active';
  }

  $link = array(
    'title' => $item['title'],
    'href' => $link_item['path'],
    'attributes' =>  $attributes
  );

  return $link;

}
mediamash’s picture

the output here is 'array'
my output for the_links function should be <a href="url_here" class="ACTIVE">title_here</A>

nevets’s picture

Sorry, late night reading, change

  $link = array(
    'title' => $item['title'],
    'href' => $link_item['path'],
    'attributes' =>  $attributes
  );

  return $link;

to

return l($item['title'], $link_item['path'], $attributes);
mediamash’s picture

strange, only the LI-tag gets an active class, i need the a-tag to have one too