Hey! I've gotten pretty far learning Drupal, but I admit I do not know PHP.

So, I found this fantastic snippet to make a block with links to taxonomy terms (which update the term is edited, which my users will do).

However, I need it to convert "Recent Paintings" to "recent-paintings" [term-raw] in the URL (leaving the term as is in the list). Is this something I should hire someone to do?


$vid = 1;  // Set the vid to the vocabulary id of the vocabulary you wish to list the terms from
$items = array();
$terms = taxonomy_get_tree($vid);
foreach ( $terms as $term ) { 
    $count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d", $term->tid)); 
    $items[] = l($term->name, "work/$term->name") . " ($count)";
}
if ( count($items) ) {  print theme('item_list', $items);}

Comments

nevets’s picture

If I understand you correctly you want to change

    $items[] = l($term->name, "work/$term->name") . " ($count)";

to

   $name = strtolower($term->name); // Make lower case
   $name = str_replace(' ', '-', $name); // Replace space with dash
    $items[] = l($term->name, "work/$name") . " ($count)";  // Corrected to use $name
stellarvisions’s picture

thanks for replying!

the lower case part works a treat, but I'm not getting the dashes. . .

also, it's changing the displayed text, I was hoping to be able to change only the link so the resulting HTML would be:
<a href="work/recent-paintings">Recent Paintings</a>

This is because I'm linking to a URL alias for the term with the URL works/[term-raw] and I have token putting in dashes.

any help is greatly appreciated (or an estimate of cost to solve it?) - -
ideally, I'd also like menu-like behavior where the current term has a different class. . . :)

best,

---
Stella Gassaway
principal

STELLARViSIONs : communication architects
http://www.stellarvisions.com

We design culture-driven tools™
that shape how information is
organized, displayed, shared, and
experienced to reflect core values.

---
Stella Gassaway
principal

stellarvisions : communication architects
http://www.stellarvisions.com

We design culture-driven tools™
that shape how information is
organized, displayed, shared, and
experienced.

nevets’s picture

See the correction above, I replaced "work/$term->name" with "work/$name".

As for a different class for the "active" term you would need to define what active term means

stellarvisions’s picture

wonderful. awesome.

thanks very much for the speedy help -- it removes a huge roadblock for me.

---
Stella Gassaway
principal

STELLARViSIONs : communication architects
http://www.stellarvisions.com

We design culture-driven tools™
that shape how information is
organized, displayed, shared, and
experienced to reflect core values.

---
Stella Gassaway
principal

stellarvisions : communication architects
http://www.stellarvisions.com

We design culture-driven tools™
that shape how information is
organized, displayed, shared, and
experienced.

stellarvisions’s picture

I'm obviously doing something wrong because I investigated more carefully and it seems that I'm just not getting it to work at all (even the lower case part), let me work on it a bit -- maybe I'm missing something obvious. . .

---
Stella Gassaway
principal

STELLARViSIONs : communication architects
http://www.stellarvisions.com

We design culture-driven tools™
that shape how information is
organized, displayed, shared, and
experienced to reflect core values.

---
Stella Gassaway
principal

stellarvisions : communication architects
http://www.stellarvisions.com

We design culture-driven tools™
that shape how information is
organized, displayed, shared, and
experienced.

stellarvisions’s picture

---
Stella Gassaway
principal

STELLARViSIONs : communication architects
http://www.stellarvisions.com

We design culture-driven tools™
that shape how information is
organized, displayed, shared, and
experienced to reflect core values.

---
Stella Gassaway
principal

stellarvisions : communication architects
http://www.stellarvisions.com

We design culture-driven tools™
that shape how information is
organized, displayed, shared, and
experienced.

juroon’s picture

Thanks! I've been trying to change the default taxonomy link, and this finally does it.

I made nevets' changes to stellarvision's code, then combined it with another version that doesn't show the count numbers, but hides terms with no content. I might have some extra stuff in there but it seems to work for me:

$vid = 1;  // Set the vid to the vocabulary id of the vocabulary you wish to list the terms from
$items = array();
$terms = taxonomy_get_tree($vid);
foreach ( $terms as $term ) {
    $count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d", $term->tid));
    $name = ($term->name);   // Don't know if I need the parenthesis here, but didn't need lowercase
if ($count) { // don't show terms with 0 count 
    $items[] = l($term->name, "mypathname/$name");  // Corrected to use $name
    }
  } /* end foreach */
  print theme('item_list', $items);
sukameister’s picture

This is a very helpful code snippet, but how would I add the item count for term in parenthesis next to the term's name? Any suggestions would be greatly appreciated!

nevets’s picture

Change

    $items[] = l($term->name, "mypathname/$name");  // Corrected to use $name

to

    $items[] = l($term->name . " ($count)", "mypathname/$name");  // Corrected to use $name

if you want the count part of the link or

    $items[] = l($term->name, "mypathname/$name"). " ($count)";  // Corrected to use $name

if you don't want the count as part of the link.

vood002’s picture

Thank you very much for this code, it worked great for me.

Some people may run into a situation like I had in which they need to order their terms Alphabetically. This is complicated a bit because the sort() function and the asort() function can't grab data from the 'term' objects in this 'terms' array. The answer lies in php's usort function, which I'd never used until now, but did the trick immediately.

Somewhere in the page's PHP put the following function:

function termSort($a, $b)
{
    return strcmp($a->name, $b->name);
}

Then, right before you begin looping through the terms ( foreach ( $terms as $term ) { in stellarvisions code ) call the function with the code:

   usort ($terms,'termSort')

More information on usort is at http://us2.php.net/usort

nevets’s picture

Note this is only needed if your terms are not using the default order (which is alphabetical) and you want them in alphabetical order.