22 July 2011

Menu delimiters or separators in Drupal. Tested on Drupal version 6.

Source code viewer
  1. <?php
  2. /**
  3. * Return a themed set of links delimitated by "|"
  4. * An override of theme_links()
  5. *
  6. * @see theme_links
  7. */
  8. function theme_name_links($links, $attributes = array('class' => 'links')) { //replace theme_name
  9. $output = '';
  10.  
  11. if (count($links) > 0) {
  12. $output = '<ul'. drupal_attributes($attributes) .'>';
  13.  
  14. $num_links = count($links);
  15. $i = 1;
  16.  
  17. foreach ($links as $key => $link) {
  18. $class = $key;
  19.  
  20. // Add first, last and active classes to the list of links to help out themers.
  21. if ($i == 1) {
  22. $class .= ' first';
  23. }
  24. if ($i == $num_links) {
  25. $class .= ' last';
  26. }
  27. if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))) {
  28. $class .= ' active';
  29. }
  30. $output .= '<li'. drupal_attributes(array('class' => $class)) .'>';
  31.  
  32. if (isset($link['href'])) {
  33. // Pass in $link as $options, they share the same keys.
  34. $output .= l($link['title'], $link['href'], $link);
  35. }
  36. else if (!empty($link['title'])) {
  37. // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
  38. if (empty($link['html'])) {
  39. $link['title'] = check_plain($link['title']);
  40. }
  41. $span_attributes = '';
  42. if (isset($link['attributes'])) {
  43. $span_attributes = drupal_attributes($link['attributes']);
  44. }
  45. $output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
  46. }
  47.  
  48. $delimiter = '<li class="delimiter">|</li>';
  49.  
  50. if ($i == $num_links){
  51. $delimiter = '';
  52. }
  53.  
  54. $i++;
  55.  
  56. $output .= "</li>" . $delimiter;
  57. }
  58.  
  59. $output .= '</ul>';
  60. }
  61.  
  62. return $output;
  63. }
Programming Language: PHP