Theme Primary Links Drupal 6.x Adding spans and useful ID's


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

themes the primary links adding spans (useful for sliding doors technique) and nice ID's based on the link title rather than the standard ugly menu-851 classes etc.


Copy this code and paste it in your HTML
  1. /**
  2.  * Adds spans and useful ID's to the primary links.
  3.  *
  4.  */
  5. function THEMENAME_links($links, $attributes = array('class' => 'links')) {
  6. $output = '';
  7.  
  8. if (count($links) > 0) {
  9. $output = '<ul'. drupal_attributes($attributes) .'>';
  10.  
  11. $num_links = count($links);
  12. $i = 1;
  13.  
  14. foreach ($links as $key => $link) {
  15.  
  16. $class = '';
  17. $needle = array('&', ' ');
  18. $replace = array('and', '-');
  19. $id = strtolower(str_replace($needle, $replace, $link['title']));
  20.  
  21. // Add first, last and active classes to the list of links to help out themers.
  22. if ($i == 1) {
  23. $class .= ' first';
  24. }
  25. if ($i == $num_links) {
  26. $class .= ' last';
  27. }
  28. if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))) {
  29. $class .= ' active';
  30. }
  31. $output .= '<li id="'. $id .'"';
  32.  
  33. if ($class) {
  34. $output .= ' class="'. $class .'"';
  35. }
  36.  
  37. $output .= '>';
  38.  
  39. if (isset($link['href'])) {
  40. // Pass in $link as $options, they share the same keys.
  41. $link['title'] = '<span>'.$link['title'].'</span>';
  42. $link['html'] = TRUE;
  43. $output .= l($link['title'], $link['href'], $link);
  44. }
  45. else if (!empty($link['title'])) {
  46. // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
  47. if (empty($link['html'])) {
  48. $link['title'] = check_plain($link['title']);
  49. }
  50. $span_attributes = '';
  51. if (isset($link['attributes'])) {
  52. $span_attributes = drupal_attributes($link['attributes']);
  53. }
  54. $output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
  55. }
  56.  
  57. $i++;
  58. $output .= "</li>\n";
  59. }
  60.  
  61. $output .= '</ul>';
  62. }
  63.  
  64. return $output;
  65. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.