Getting the Taxonomy Term as an argument for a view

Events happening in the community are now at Drupal community events on www.drupal.org.
Screenack's picture

Using Drupal 6.14; Views 2 6.x-2.7

I have defined documents that are related by taxonomy. For example there's a location hierarchy (with a doctype def at the root):

region-1
--Bladen
--Clinton
--Dunn
region-2
--Orange
--Chatham
Project-Description

So, a region's page will have a region assignment; i.e., "orange"
Meanwhile, the project description, which will apply to several regions, will have the following taxonomy:
Project-Description, Orange, Clinton

Finally, the overview page for that region will have one term, the region assignment, i.e., "Orange"

the url for the orange page is as follows

/sitedirectory/?q=appregions/region-1/Orange

(yes, the site is in a subdirectory)

So the overview page "knows" it's region. I want to create a view that takes the current page's taxonomy term and use it as an argument to filter the results of all docs that are "Project-Description" to only have those that are also "Orange" (the page's term)

If I create the view without an argument, it works, i.e. find all "Project-Description" docs and set the second filter for "Orange". So, to make this as it should, I'd rather make the view dynamic to take in the current page's taxonomy term as an argument and that's where I can not connect the dots.

I will dearly appreciate any pointers -- as always; thanks!

Comments

Default arguments + php to the rescue?

afreeman's picture

The trick here is figuring out the term you're after based on the current node. Since term information isn't available in the arg() path on a node page you're going to have to dig this info up yourself. I'd try something like this:

(the following assumes your region vocabulary isn't multiple select)

Try adding an argument for Taxonomy: Term ID
under "Action to take if argument is not present:" select "provide default argument"
Select "PHP Code" as your default argument type

//do some quick checking on arg()s to make sure you're in the proper context
if(arg(0) == 'node' && is_numeric(arg(1))) {
//yoink the tid for the current node
$nid = arg(1);
$vid = ;
$tid = db_result(db_query('select tid from {term_node} where nid=%d and vid=%d',$nid,$vid));
return $tid;
}

I helped him get it working

arcaneadam's picture

I helped him get it working earlier today and just used:

<?php

$arg
= explode('/',drupal_get_path_alias($_GET['q']));
return
arg[2];

?>

It took some playing around for a bit but it's working pretty well now.

Adam A. Gregory
Drupal Developer and Consultant
Hire me
http://AdamAGregory.com
Twitter Me
http://twitter.com/adamgregory

Another solution that

Screenack's picture

Another solution that arcaneadam got to work was to do the following:

Argument:Taxonomy Term:default php code:
$arg = explode('/',drupal_get_path_alias($_GET['q']));
return $arg[2];

We just wrapped this up via irc; but I'm curious to give your code a fly.

Would this work? "if(arg(0) == 'node' && is_numeric(arg(1))) {"
if I'm using pathauto, that is that the URL will not be ?q=node/260?

As far as I know pathauto

afreeman's picture

As far as I know pathauto doesn't change the output of arg(), all it does is work a little mod_rewrite magic.

triDUG

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week