Fix for add extra spans to the primary menu links

  1. // a preprocess function to highjack primary links
  2. function phptemplate_preprocess_page(&$vars) {
  3.  
  4. // add some extra spans to the primary menu links
  5.   if (isset($vars['primary_links'])) {
  6.     $vars['klassik_primary_links'] = _klassik_primary_links($vars['primary_links']);  
  7.   }
  8. }
  9.  
  10.  
  11. // then rewrite theme_links for it, my theme is called klassik, I needed to add a lot of spans, modify as needed!
  12.  
  13. /**
  14.  * Implementation of theme_links(), we need extra span tags inside the primary menu links
  15.  *
  16.  * Return a themed set of links.
  17.  *
  18.  * @param $links
  19.  *   A keyed array of links to be themed.
  20.  * @param $attributes
  21.  *   A keyed array of attributes
  22.  * @return
  23.  *   A string containing an unordered list of links.
  24.  */
  25. function _klassik_primary_links($links, $attributes = array('id' => 'nav', 'class' => 'links')) {
  26.   $output = '';
  27.  
  28.   if (count($links) > 0) {
  29.     $output = '<ul'. drupal_attributes($attributes) .'>';
  30.  
  31.     $num_links = count($links);
  32.     $i = 1;
  33.  
  34.     foreach ($links as $key => $link) {
  35.       $class = $key;
  36.  
  37.       // Add first, last and active classes to the list of links to help out themers.
  38.       if ($i == 1) {
  39.         $class .= ' first';
  40.       }
  41.       if ($i == $num_links) {
  42.         $class .= ' last';
  43.       }
  44.       if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))) {
  45.         $class .= ' active';
  46.       }
  47.       $output .= '<li'. drupal_attributes(array('class' => $class)) .'>';
  48.  
  49.       if (isset($link['href'])) {
  50.         $link['title'] = '<span class="link-top-right"></span><span class="link-bottom-left"></span><span class="link-bottom-right"></span><span class="link-text">' . check_plain($link['title']) .'</span>';
  51.         $link['html'] = TRUE;
  52.         // Pass in $link as $options, they share the same keys.
  53.         $output .= l($link['title'], $link['href'], $link);
  54.       }
  55.       else if (!empty($link['title'])) {
  56.         // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
  57.         if (empty($link['html'])) {
  58.           $link['title'] = check_plain($link['title']);
  59.         }
  60.         $span_attributes = '';
  61.         if (isset($link['attributes'])) {
  62.           $span_attributes = drupal_attributes($link['attributes']);
  63.         }
  64.         $output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
  65.       }
  66.  
  67.       $i++;
  68.       $output .= "</li>\n";
  69.     }
  70.  
  71.     $output .= '</ul>';
  72.   }
  73.  
  74.   return $output;
  75. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.