0

I need <span> tags around strings on my menu items. Trying to use this answer but it does not work for me. I probably just need some guidance in terms of naming things (just started on Drupal).

My theme folder name is sub_website, it is inheriting from theme called website (set by base theme = website in sub_website/sub_website.info). The menu in question is named 'Primary menu', it is selected as "source for the primary links" in the settings, and it is accessible via /admin/build/menu-customize/primary-links URI.

I tried adding the following into the sub-theme's template.php:

function sub_website_links__system_primary_links_menu($variables) {
  foreach ($variables['links'] as $link) {
    $output .= l('<span>'. check_plain($link['title']) .'</span>', $link['href'], $link);
  }
  return $output;
}

What am I doing wrong? Am I not clearing the cache properly? Did I not name the function correctly? Is there no such hook in Drupal 6? Documentation is very fragmented and is further complicated by two major versions existing side by side.

1 Answer 1

0

The answer that you found is for Drupal 7 not for Drupal 6.

In Drupal 6 you can't override theme_links() for a specific target.

You need to override theme_links() function itself to add span tag as the following code:

function sub_website_links($links, $attributes = array('class' => 'links')) {
  global $language;
  $output = '';

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

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

    foreach ($links as $key => $link) {
      $class = $key;

      // 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()))
          && (empty($link['language']) || $link['language']->language == $language->language)) {
        $class .= ' active';
      }
      $output .= '<li'. drupal_attributes(array('class' => $class)) .'>';

      if (isset($link['href'])) {
        // Pass in $link as $options, they share the same keys.

        // check if menu id attribute is set to primary-links
        if(isset($attributes['id']) && $attributes['id'] == 'primary-links') {

          // add html to link options 
          $link['html'] = true;

          // wrap text with span
          $output .= l('<span>' . $link['title'] . '</span>', $link['href'], $link);

        } else {
          $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;
}

Then edit page.tpl.php and add id attribute for $primary_links menu

print theme('links', $primary_links, array('id' => 'primary-links', 'class' => 'links clearfix'));
2
  • Thank you. Would that affect all the menus? What's a proper way of doing it for one particular menu? Commented May 25, 2013 at 21:44
  • 1
    @dalbaeb Yes it will effect all menus. However I updated my answer to target one particular menu.
    – Aboodred1
    Commented May 25, 2013 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.