views:

35

answers:

2

Hello

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); ?>
+1  A: 

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)

Jozzeh
Hey, thanks. I have tried that repeatedly but no luck. It just doesn't pick up the override.
Ed Henderson
Have you also set in the .info file of the theme the engine to phptemplate ? ( engine = phptemplate ) ... I'm just saying everything it COULD be.
Jozzeh
engine = phptemplate, that is correct. Thanks for your suggestions. The theme is rendering OK, CSS, etc. Just not picking up this override.
Ed Henderson
+1  A: 

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.

mongolito404
Its called in the page.tpl.php file with <?php print theme('links', $primary_links); ?> which I assume is the default? I will check the module you suggested, cheers.
Ed Henderson
Cannot see it calling menu_item anything in the log? Clicking on the primary nav is giving mewellington_links < phptemplate_links < theme_links
Ed Henderson
Was able to use the _links function to do what I wanted. Thanks.
Ed Henderson

related questions