Question

I have a Drupal theme called wellington located in \sites\all\themes\wellington. I wish to override the menu_item function and followed the instructions at http://drupal.org/node/310356.

I want to add a class to the li as described.

I have tried naming the function wellington_menu_item and tried phptemplate_menu_item but no luck. I can put print statements in the function and these are displayed on screen.

Additionally I can print out the return string just before its returned and its correct but when the menu is rendered, no difference, its the normal main one showing, i.e. no override.

The theme itself is working fine and I can see the CSS and HTML.

Am stumped, any ideas?

<?php
/**
* Theme override for theme_menu_item()
*/
function phptemplate_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';
  }

  // Add unique identifier
  static $item_id = 0;
  $item_id += 1;
  $id .= ' ' . 'menu-item-custom-id-' . $item_id;
  // Add semi-unique class
  $class .= ' ' . preg_replace("/[^a-zA-Z0-9]/", "", strip_tags($link));

  return '<li class="'. $class .'" id="' . $id . '">'. $link . $menu ."</li>\n";
}
?>

It is called in page.tpl.php using

<?php print theme('links', $primary_links); ?>
Was it helpful?

Solution

As you mention being able to print the result string before returning it, the overrides does work. What I suspect is that the result of your function is not actually used when rendering. Perhaps because it is stored in a template variable which is then overridden with another value in a preprocess function called later.

I suggest your use the Theme developer module to check which template/function is used to render your menu item.

OTHER TIPS

Don't forget to empty your browser cache and the drupal cache when you added the function to the template.php . The clearing of the browser cache should not be necessairy but sometimes usefull for other changes.

(site configuration - performance - clear cached data)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top