Displaying Taxonomy (Free Tag) Terms in the Order Entered?

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
TKS's picture

Has anyone in this group found an elegant way to override the order in which Taxonomy Terms are displayed on a node? Or any way, for that matter...

While weight, then alphabetical is ideal most of the time, there are occasions when it would be great to display the terms exactly in the order they were entered. (My site, for example, has a vocabulary for authors of reports & articles, and the order in which those names appear on a node is very important. And different individuals might be the "lead author" on different reports, so we can't just weight certain names to always rise to the top.)

The code below was intended to provide that "as-entered" functionality when it was written for our site rollout on Drupal 4.7. I think it worked as intended at the time, and broke when we upgraded to 5.x, but I can't swear that it wasn't always a problem that I just didn't notice at the time.

So, three questions, I guess:

  1. Has this issue been raised and solved elsewhere? (None of my searches turned up relevant threads)
  2. Are there any obvious flaws or outdated references in the code below?
  3. Are there any changes in Drupal 6's approach to taxonomy that would be relevant to this issue and my attempts to fix it? (It'll likely be several months before we go to 6, but it's nice to plan ahead...)

Thanks in advance for any suggestions or insights. Trying to wrap my head around the way taxonomy works "under the hood" is almost as important to me as finding an actual fix to this problem -- and I'm hoping this issue might actually be interesting to others in the group!

TKS

<?php
/**
* Displays a list of term links for a given node and vocabulary.
*
* @param $nid
*  Node ID
* @param $vid
*  Vocabulary ID
* @return
*  Comma-separated list of links, or FALSE if none were found.
*/

function theme_naf_term_links($nid, $vid) {
   
$vocabulary = taxonomy_get_vocabulary($vid);
   
// Can't use this function because it orders terms by weight;
    // we want to order them by the order they were attached to the node.
    //$terms = taxonomy_node_get_terms_by_vocabulary($nid, $vid);

 
$node = node_load($nid);
 
$node_terms = $node->taxonomy;
 
$terms = array();
  foreach (
$node_terms as $term) {
    if (
$term->vid == $vid) {
     
// Have to put the term ID in quotes, otherwise it will order the list
      // by term ID rather than the order they were entered.
     
$terms["'". $term->tid ."'"] = $term;
    }
  }

   
$links = array();
   
$output = '';

    foreach (
$terms as $tid => $term) {
       
$links[] = l($term->name, 'taxonomy/term/'. $term->tid);
    }
    if (
count($links)) {
        return
implode(', ', $links);
    }
    else {
        return
FALSE;
    }
}
?>

Comments

not stored by Drupal

catch's picture

The order of terms entered isn't stored anywhere - take a look at the term_node table.

You'd probably want to add a custom table which stores nid, vid and then a field for the tids serialized - 5,4,3,2,6 in the order they appear in the form - then you could use that column to ensure you get them in the order entered and pass it into theme_links etc. Not something I've ever wanted or needed to do though.

Thanks

TKS's picture

I could've sworn that the terms displayed in the order entered when one went back into edit mode, but clearly that's not happening.

Oh well -- something to tackle another day. Thanks for the quick answer!

TKS

This issue still isn't resolved

blueflowers's picture

Not sure this needs to be a part of the taxonomy module per say (although it would be nice), as it can be solved on a module level.

We ran into the same problem recently when pulling in related content via tags. Problem was we needed the 1st weighted tag to be the most important, 2nd weighted tag second most important etc, etc. when querying related content from the db.

Would anyone be interested in us packaging our freetag sort module solution for d5/d6? Our company would be willing to provide sponsorship to the project, but not able to commit to maintenance ATM.

Try the new Tag Order module

tagorder's picture

We had a similar problem at FastCompany.com -- the Tag Order module was my solution, actually a similar approach to that suggested by catch.

http://drupal.org/project/tagorder

Tag order is exactly what I

hunt3r's picture

Tag order is exactly what I wanted. Thanks! I have an issue where we want tag clouds on nodes based on that node only. So I came up with the block module to do this by evenly distributing the tag weights available across all tags in the vocabulary, but the weights are dependent on ordering. When I noticed that ordering was not preserved by default, this became a bit of an issue! As you can imagine.

If anyones interested in my tag cloud code (maybe tagadelic module could internalize it), I will contribute it.

Thanks for tagorder though!

Hunter

Tag order is exactly what I

hunt3r's picture

Tag order is exactly what I wanted. Thanks! I have an issue where we want tag clouds on nodes based on that node only. So I came up with the block module to do this by evenly distributing the tag weights available across all tags in the vocabulary, but the weights are dependent on ordering. When I noticed that ordering was not preserved by default, this became a bit of an issue! As you can imagine.

If anyones interested in my tag cloud code (maybe tagadelic module could internalize it), I will contribute it.

Thanks for tagorder though!

Hunter