I need to put a inside of the anchor tags of the Primary links.

Example:

<a href="URL"><span>link text</span></a>

How can I do this in Drupal 5 ?

[edit -sepeck: added code tags]

Comments

Chill35’s picture

I don't have the explanations, but hopefully OTHER people will step in too :)

Adding a <span></span> to Primary links involve what's called in Drupal terminology "using a themeing function".

STEP 1 : Copy & paste : from theme.inc to template.php

Go into includes/theme.inc and find the function theme_links(). Copy that whole code block into
your theme folder file template.php.

STEP 2 : Renaming the function (that you just pasted in STEP 1) and, optionnally, giving it extra or altogether different arguments. In template.php.

The function's name must start with : phptemplate_ (that's a rule)
It must NOT be named phptemplate_links because it would theme all your links. And you only want to affect
primary links.

For example, the name could be :

function phptemplate_primary_links($links, $attributes = array('class' => 'links')) {

STEP 3 : In your template file, i.e. page.tpl.php insert the following code where you want to print your primary links :

<?php if (isset($primary_links)) : ?>
<?php print theme('primary_links', $primary_links, array('class' => 'links primary-links')) ?>
<?php endif; ?>

The first argument to the theme function must be the name you gave your themeing function, after phptemplate_.
The arguments are the ones passed to the phptemplate_primary_links function, as defined (by you) in phptemplate.php.

STEP 4 : Test to see if that did not break anything, that is, check if Primary Links still appear. If it's business as usual.

STEP 5 : Now roll up your sleeves. Time to modify the function in template.php (phptemplate_primary_links) to make it do what you want, i.e. insert a <span>.

There are other ways too !!!

You can have a template file just to print the Primary links.

Caroline

irongoddess’s picture

Caroline, your explanation was very clear and easy to follow, but after I tried to apply this method to do the exact same thing on my site (i.e. simply add span tags to each of the primary links), my menu simply disappeared.

I've tried a dozen different approaches, and one of two things happens: the menu either disappears entirely from the source code, or the menu formats the same as it always did.

I tried starting very simple, just adding the function override to my template.php file and adding an HTML comment so that I could see that the PHPTemplate engine was reading the template.php override, but no dice. To all appearances, the engine is ignoring my template.php file -- at least, that's my best theory.

I'm incredibly frustrated. I've quadruple-checked spellings, filenames, where I'm uploading to, making sure I have the opening and closing PHP tags: ...

Any ideas? Anyone?

Beth

irongoddess’s picture

Ha! Stupid typo -- disregard my post above.

And again, thanks for the earlier info -- once I caught the mistake, everything worked beautifully.

Beth

ellanylea’s picture

This solution http://drupal.org/node/130177#comment-237501 worked for me in Drupal 5.

-------------------------------------------------
AdAstra.ca - Optimze the Web. Optimze the World.

mxt’s picture

In Drupal 6, if I use:

... phptemplate_links(.....

It works fine to theming all links as Carolina said, but if I rename the function like this:

... phptemplate_primary_links(.....

everything disappear!
(yes, I use "...theme('primary_links',..." as explained in page.tpl )

I think the reason is that in Drupal 6 I have to register the new function in template.php, but I can't understand how this can be done! I need an example please! I read i have to use "hook_theme()" to do so, but... HOW!

Please give me an example.

Thanks

Massimo

rats’s picture

same problem here ~_~

mekrueger’s picture

Yup, me too. very frustrating. Can someone shed some light on how to use the theme hooks, including how to register them so they actually get applied in Drupal 6.

nickbits’s picture

Any update on this?

Nick
----------------------------------
Nick Young
www.nickbits.co.uk

------------oOo----------------------
Nick Young (www.nickbits.co.uk)

cperalta’s picture

Here's what you need to do to register the primary_links themable function.

In your template.php, edit hook_theme() to include the new function.

/**
 * Implementation of HOOK_theme().
 */
function SUBTHEME_theme(&$existing, $type, $theme, $path) {
  <strong>return array(
    'primary_links' => array(
      'arguments' => array('links' => NULL),
    ),
  );</strong>
  return zen_theme($existing, $type, $theme, $path);
}

Then just make sure to include the new function.

function SUBTHEME_primary_links($links, $attributes = array('class' => 'links')) {
  $output = '';
  ...

Don't forget to clear your theme registry.

smilodon’s picture

This is very confusing. I need a WORKING example.
Just cant figure it out... i dont know PHP...

Why the hell was it changed at all... in 4.6 it was all fine.

drinkdecaf’s picture

Make a template.php file in your theme folder.

This code will add a ~ between list items:

<?php
$my_delimeter = "~";

function phptemplate_primary_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 = '';

      // Automatically add a class to each link and also to each LI
      if (isset($link['attributes']) && isset($link['attributes']['class'])) {
        $link['attributes']['class'] .= ' ' . $key;
        $class = $key;
      }
      else {
        $link['attributes']['class'] = $key;
        $class = $key;
      }

      // Add first and last classes to the list of links to help out themers.
      $extra_class = '';
      if ($i == 1) {
        $extra_class .= 'first ';
      }
      if ($i == $num_links) {
        $extra_class .= 'last ';
      }
      $output .= '<li class="'. $extra_class . $class .'">';

      // Is the title HTML?
      $html = isset($link['html']) && $link['html'];

      // Initialize fragment and query variables.
      $link['query'] = isset($link['query']) ? $link['query'] : NULL;
      $link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;

      if (isset($link['href'])) {
        $output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
      }
      else if ($link['title']) {
        //Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (!$html) {
          $link['title'] = check_plain($link['title']);
        }
        $output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
      }

      $output .= "</li>\n";
      
      if ($i < $num_links) {
          $output .= $my_delimeter;
      }
      

      $i++;
    }

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

  return $output;
}
?>
cliftoo’s picture

Hi,

I'm creating a subtheme of Zen and need to be able to wrap primary links in a span tags as described in this thread. I noticed that Zen (5.x-1.0) provides the following code in the subtheme's template.php:

/* Generate the HTML representing a given menu item ID.
 * An implementation of theme_menu_item_link() */

function zen_menu_item_link($item, $link_item) {
  // If an item is a LOCAL TASK, render it as a tab
  $tab = ($item['type'] & MENU_IS_LOCAL_TASK) ? TRUE : FALSE;
  return l(
    $tab ? '<span class="tab">'. check_plain($item['title']) .'</span>' : $item['title'],
    $link_item['path'],
    !empty($item['description']) ? array('title' => $item['description']) : array(),
    !empty($item['query']) ? $item['query'] : NULL,
    !empty($link_item['fragment']) ? $link_item['fragment'] : NULL,
    FALSE,
    $tab
  );
}

What does this code do? I thought it would wrap my primary links in a span tag. I enabled the function override but nothing happened. I have no idea what I'm doing wrong. I've followed the subtheme instructions in the Zen documentation, but changes to the subtheme's template.php have no effect.

I'm new to theming Drupal. Any advice on how to to do this with Zen would be appreciated.

TIA,

Cliftoo

yumminy’s picture

This should help http://drupal.org/node/221382

Scroll down to this comment for the final solution http://drupal.org/node/221382#comment-755469. There are issues with the html output which are resolved there.

OpenChimp’s picture

Apparently, I almost had the solution by myself, but the check_plain() stuff was not something I would have figured out on my own. That comment shows a very simple solution and provides a great explanation of how it works.