1

Hi I wrote a SQl query to generate a list of taxonomy terms, I want to add a class to each taxonomy term when its clicked, like when I click any term it goes to its term page, on that page I want some class should be be added to this taxonomy term.

I tried with jquery when I click any term, the class gets added but the page refreshes as it goes to this term page, the class is removed. I can do with cookie procedure, but I think there might be any other way in drupal.

2

The only thing to think about here is the page you end up on (the term page), so don't worry about links that appear on any other pages.

So what you want to do is on taxonomy/term/% pages, add a class to links that match that page.

Seeing as we are adding a class to an a tag, we want to be altering/overriding the markup output of an a tag.

So then you just have to find the best way to do that.

There are a lot of places that a link might come from in drupal, so to try to cover the most you possibly can, you want to be as general as possible, which in this case means theme_link(), which as the name indicates, handles the themeing of a link.

For more information on overriding themeable output see About overriding themable output. There are also many other examples across the internet.

In this case you will want to do something like this...

In your theme's template.php file add this:

/**
 * Theme a link tag.
 */
function YOURTHEMENAME_link($variables) {
  // If we are on a term page.
  if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) {
    // The easiest way to check all possibilities, like absolute & relative
    // paths and path aliases and real paths is to compare via the url function,
    // which will normalise all this stuff.
    if (url($variables['path'], array('absolute' => TRUE)) == url($_GET['q'], array('absolute' => TRUE)) {
      // Add the class to the link definition so that it will be
      // added to the rendered link.
      $variables['options']['attributes']['class'][] = 'your-class-goes-here';
    }
  }

  // Print the link as usual.
  return '<a href="' . check_plain(url($variables['path'], $variables['options'])) . '"' . drupal_attributes($variables['options']['attributes']) . '>' . ($variables['options']['html'] ? $variables['text'] : check_plain($variables['text'])) . '</a>';
}

After adding this function to your template.php you will have to clear the drupal cache for it to take effect.

Note that if your theme or any modules print links without using the l() function or theme('link') or similar then it won't be affected by this.

If you have user entered content that has these links in them then they will also not be affected by default. The easiest way to make it so they are also handled is to enable and configure the pathologic module.

The other option is javascript, but that is not ideal as it isn't really necessary in this case and doesn't work for users that don't have javascript.

In that case you need to include your script on the taxonomy/term/% pages only (see drupal_add_js()), then in the script check for the current page matching any link hrefs and add the class.

0

Thanks Rooby, I did it with jquery.

$('li').each(function(){ if(window.location.href.indexOf($(this).find('a:first').attr('href'))>-1) {

    $(this).addClass('activelink').siblings().removeClass('activelink');

    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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