Posted By

zachharkey on 04/14/09


Tagged

drupal


Versions (?)

Who likes this?

7 people have marked this snippet as a favorite

Fake
jmiller
panatlantica
picxelplay
tronx
fadehelix
knibals


Add unique IDs to Drupal menu items


 / Published in: PHP
 

URL: http://drupal.org/node/310356#comment-1210124

Place the following function in your theme's template.php

  1. /**
  2.  * Theme override for theme_menu_item()
  3.  */
  4. function phptemplate_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
  5. $class = ($menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf'));
  6. if (!empty($extra_class)) {
  7. $class .= ' '. $extra_class;
  8. }
  9. if ($in_active_trail) {
  10. $class .= ' active-trail';
  11. }
  12.  
  13. // Add unique identifier
  14. static $item_id = 0;
  15. $item_id += 1;
  16. $id .= ' ' . 'menu-item-custom-id-' . $item_id;
  17. // Add semi-unique class
  18. $class .= ' ' . preg_replace("/[^a-zA-Z0-9]/", "", strip_tags($link));
  19.  
  20. return '<li class="'. $class .'" id="' . $id . '">'. $link . $menu ."</li>\n";
  21. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Fake on July 23, 2009

[Drupal 6] It works! Big thanks!

You need to login to post a comment.