I am creating a block that is only displayed on taxonomy pages. This is simple enough, just use taxonomy/* in the block's display settings. But after that, I'm lost. How can I tell which vocabulary it's being displayed under? It really seems like there should be a simple function to determine this, but everything I find at api.drupal.org relates to specific nodes, to the taxonomy listings.

My other question is how to find which vocabulary a term falls under. I don't have multiple parents (one vocabulary for Topics, another for Location), so this should be simple to determine. But I just can't figure it out.

Any help would be greatly appreciated! I've been searching and trying different snippets for weeks, to no avail!

Comments

Jeff Burnz’s picture

The tid is in the url.

Just look at the URL www.yourdomain/taxonomy/term/57 etc

So in the block display settings you could just add a list such as...

taxonomy/term/12
taxonomy/term/23
taxonomy/term/44

And the block will show up only on those pages (assuming you select 'Show on only the listed pages').

To the second part of your question - if you added the taxonomy terms you already know what vocab each term is listed under - if you forgot just go to Administer > Categories and click 'List terms' for a vocab to see the terms associated with that vocab. You only have two vocabs so it's a 50/50 chance you'll hit it on the first try:)

While looking at those terms - hover each term and you will see the tid in your browsers status bar - even better - right click and copy link location and just paste it into your block display text area (you will need to strip out the first part of the url - the http//www.yourdomain.com/ bit).

Have fun!

talkingwires’s picture

Thanks for the reply.

I think I should clarify what I'm trying to accomplish: I'm creating a large knowledgebase of legal information, news, forums, and eventually Organic Groups. I'm trying to improve navigation for the user by creating blocks displayed on the taxonomy term's pages. For example, a term from the "Topics" vocabulary will have a block that can filter the topic's nodes by state. Terms from the "Location" vocabulary can filter terms by a specific topic, and it's sub-topics. Also, I'd really like to have the term's description appear at the top of listings, too.

There are modules out there that mostly accomplish what I'm looking for, but they are not ported to 5.0, and may never be. But I think this could be easily accomplished with blocks. (Actually, I think some of it should be part of Core, but that's neither here nor there.)

So, to that end, I'm looking for a way for a block to say, "Hey, I'm displayed on a taxonomy term's page, which is part of such-and-such vocabulary. I should display X if in this vocabulary, Y if in this vocabulary, and get my term's description while I'm at it."

styro’s picture

If on a term listing page, create an array of terms being viewed:

<?php
      if (arg(0) == 'taxonomy' && arg(1) == 'term') {
        $tids = preg_split('/[+ ,]/', arg(2));
      }
?>

arg(2) = '2' when the url is 'taxonomy/term/2', or '2+5' when the url is 'taxonomy/term/2+5/5' etc

if arg(2) is '2+5', $tids[0] is 2 and $tids[1] is 5 etc

More examples:

Given a term id, fetch a term object:

<?php
  $term_obj = taxonomy_get_term($term_id);
?>

or given an array of term ids (eg from the first example) , fetch the term object of the first term id in the array:

<?php
  $term_obj = taxonomy_get_term($tids[0]);
?>

Given a term object, fetch its vocabulary id:

<?php
  $vocab_id = $term_obj->vid;
?>

Given a term object, fetch its vocabulary object:

<?php
  $vocab_obj = taxonomy_get_vocabulary($term_obj->vid);
?>

From a term object get the description:

<?php
  $desc = $term_obj->description;
?>

Taxonomy API reference:
http://api.drupal.org/api/4.7/file/modules/taxonomy.module

I suggest you install the devel module as it has a php textarea where you can type in php expressions into Drupal and see the results. It also has a dprint_r() function that pretty prints the contents of arrays and objects to let you see the internal structure of items returned by API functions.

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ

talkingwires’s picture

Thank you, this is exactly the information I needed! And thanks for the tip about the Devel module. Somehow in the past I had gotten the impression that it accomplished something completely different. But the PHP syntax/Drupal API textarea sounds like it would be invaluable for Drupal development. I'll be sure to check it out.

Oh, and thanks for deciding to continue work on your Taxonomy Filter module. I know I'll continue to make use of it, and I'm sure others will as well.

talkingwires’s picture

Seriously, nobody knows?

Jeff Burnz’s picture

Its a bit confusing as to what you actually want to achieve.

This sounds like a good solution http://drupal.org/node/91670 and contrary to your post it indeed looks like it's being updated for 5.

Also, a very simple solution - if your categories never (or rarely) change, build menus manually and display them as I suggested earlier.

talkingwires’s picture

Um, if you take a second look at the thread you're referring to, you'll that I created it. :) But yes, the author of the Taxonomy Filter module has decided to port it to 5.0, after all. He even popped into this one, a few posts above.

I briefly considered doing it statically, but it would be very impractical. One taxonomy is currently fixed at fifty terms, one for each state. But if the site grows, that my expand into the hundreds to include major cities. The other is a list of topics articles may be tagged with, and that will expand regularly as needed. That would make one big honkin' taxonomy filter menu, both to display to the user and to maintain. And there'd still be the question of how to only display it on a specific taxonomy term's page, not on nodes tagged with that term.