Add unique IDs to Drupal menu items


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.