Return to Snippet

Revision: 23250
at February 2, 2010 05:42 by ReeceMarsland


Initial Code
/**
 * Adds spans and useful ID's to the primary links.
 *
 */
function THEMENAME_links($links, $attributes = array('class' => 'links')) {
  $output = '';

  if (count($links) > 0) {
    $output = '<ul'. drupal_attributes($attributes) .'>';

    $num_links = count($links);
    $i = 1;

    foreach ($links as $key => $link) {
      
      $class = '';
      $needle = array('&', ' ');
      $replace = array('and', '-');
      $id = strtolower(str_replace($needle, $replace, $link['title']));
      			
      // Add first, last and active classes to the list of links to help out themers.
      if ($i == 1) {
        $class .= ' first';
      }
      if ($i == $num_links) {
        $class .= ' last';
      }
      if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))) {
        $class .= ' active';
      }
      $output .= '<li id="'. $id .'"';
      
      if ($class) {
      	$output .= ' class="'. $class .'"';
      }
      
      $output .= '>';

      if (isset($link['href'])) {
        // Pass in $link as $options, they share the same keys.
        $link['title'] = '<span>'.$link['title'].'</span>';
        $link['html'] = TRUE;
        $output .= l($link['title'], $link['href'], $link);
      }
      else if (!empty($link['title'])) {
        // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (empty($link['html'])) {
          $link['title'] = check_plain($link['title']);
        }
        $span_attributes = '';
        if (isset($link['attributes'])) {
          $span_attributes = drupal_attributes($link['attributes']);
        }
        $output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
      }

      $i++;
      $output .= "</li>\n";
    }

    $output .= '</ul>';
  }

  return $output;
}

Initial URL

                                

Initial Description
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.

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

Initial Tags
drupal, theme

Initial Language
PHP