View Argument Handling Code - Using Multiple Taxnomy Term Names as Arguments

Preface:

I've read a lot of requests for using multiple taxonomy term names as view arguments. I found a code, but it didnt work for me.
Views default argument handling using termid rather then name works beautifully, using forms like: "mydomain.com/viewpage/termid1,termid3,termid2" or "mydomain.com/viewpage/termid1+termid2+termid3", but I found the support for term names lacking.

Concept:

  • What I did with my handling code was basically grab the arguments array from mydomain.com/viewpage/term1/term2/term3, then convert any string arguments to a termid, and then flatten the arguments array (which is now all term id's) to the form of "termid1,termid2,termid3", so I can take advantage of the great termid argument support.

Setup

  1. Setup a view with ONE argument field of type: Taxonomy Term ID Notice it is term ID NOT term name
  2. Paste Code below into Argument handling Code
  3. browse your view with: mydomain.com/viewpage/termname1/termname2/termname3
    NOTE: with this setup, you can use term id's as well as termnames at the same time, and in any order you want!

Code:

// Blake Edwards Arugment Handling Code
// - Multiple Taxonomy Term Names as Arguments
$exclude_vocab = array('13');

if ($args[0]):
//replace underscores with spaces
foreach ($args as $key=>$arg) { $args[$key] = str_replace('_',' ',$arg); }
$args_original = $args;
$args_processed = array();
foreach ($args as $key=>$arg)
{
   if (!is_numeric($arg) || strlen($arg) == 4)
   {
       $terms = taxonomy_get_term_by_name($arg); //do a search for terms, return term objects
       if (count($terms) > 1)
       {
           //Exclude Vocabularies
            foreach ($terms as $key=>$term) { if (in_array($term->vid,$exclude_vocab)) { unset($terms[$key]); } }
       }
       array_push($args_processed,$terms[0]->tid);
    }
    else { array_push($args_processed,$arg); }
}

//Flatten Array to one Argument Comma Separated
foreach ($args_processed as $arg) { $newargs .= "$arg,"; }
$newargs = substr($newargs,0,-1);


return array($newargs);
ENDIF;

Groups:
Login to post comments

Thanks for your solution. Am

basby@drupal.org - Thu, 2008-02-07 14:27

Thanks for your solution.

Am I correct if I state that your solution works for "mydomain.com/viewpage/termid1,termid3,termid2" but not for "mydomain.com/viewpage/termid1+termid2+termid3"?

Do you have a solution for the latter one?

Thanks!


yea, currently the setup is

electBlake - Thu, 2008-02-07 18:11

yea, currently the setup is for termid1,termid2,termid3

but that can be changed easily!

If you look down at the bottom of the handling code, you see the line:
foreach ($args_processed as $arg) { $newargs .= "$arg , "; }

Thats the final part which formats the term ids into one argument, if you change the , (that I bolded and spaced above) to a plus sign, it will give you termid1+termid2+termid3


Just a little modification

hyrcan - Thu, 2008-08-07 19:33

A quick mod to your code to meet my needs. Thought I would share.

The difference in the code (aside from the _ to a - in the str_replace) is instead of removing the $exclude_vocab, which would lead to an ever growing list, I've switched it to explicitly stating the vocab to search in via $vocab_required something that for my case won't be changing very often if ever.

I also removed the "strlen($arg) == 4" only because I couldn't see a reason to limit the $arg to four. Any reason for that I'm missing?

$vocab_required = array('6');
//replace underscores with spaces
foreach ($args as $key=>$arg) { $args[$key] = str_replace('-',' ',$arg); }
$args_original = $args;
$args_processed = array();
foreach ($args as $key=>$arg) {
  if (!is_numeric($arg)) {
    $possibilities = taxonomy_get_term_by_name($arg);
    $arg_tid = NULL; // tid match if any.
    foreach ($possibilities as $possibility) {
      if (in_array($possibility->vid,$vocab_required)){
          $arg_tid = $possibility->tid;
      }
    }
    array_push($args_processed,$arg_tid);
  }
  else { array_push($args_processed,$arg); }
}

//Flatten Array to one Argument Comma Separated
foreach ($args_processed as $arg) { $newargs .= "$arg,"; }
$newargs = substr($newargs,0,-1);

array($newargs);


This code works like a dream

zoro - Mon, 2008-09-15 13:25

This code works like a dream for me - thanks


how about ...OG

najibx's picture
najibx - Sat, 2008-08-30 17:08

My need is content is published in to more than one groups, and I want to use Views to filter and put it in a block inside a og_panel.

I was wondering how this can be adapted for "Argument: OG: Group name". Secondly how we can have these context forwarded to og_panel. I have no problem with single group.

Thanks


Slight improvements

gramie - Wed, 2009-05-06 16:24

I'm not sure why you used your own code to flatten the array. Why not just

implode(',', $args_processed);

and for that matter (not that I'm criticizing you, but I'm used to seeing code written differently):

array_push($args_processed, $arg);

could be

$args_processed[] = $arg;

And to replace underscores with spaces, you can use the str_replace function, which will go through an array:

str_replace('_', ' ', $args);

Hi, How would it be possible

Summit - Sun, 2009-06-28 12:56

Hi,
How would it be possible to filter on these terms with only a certain depth on views2?
I tried this on www.drupal.org/node/496756
greetings, Martijn


I am trying to set something

emilyf - Mon, 2009-08-03 13:17

I am trying to set something like this up in Views 2, but it doesn't appear to work properly there. Can anyone provide any pointers on getting it to work with Views 2?
thanks much!


@emilyf -- i just got this

arh1's picture
arh1 - Thu, 2009-08-13 18:58

@emilyf -- i just got this working by using two Taxonomy Term arguments, and making sure the "Allow multiple arguments to work together" box was checked for both (per http://drupal.org/node/428742#comment-1461586 ). try leaving the argument validators off initially, then experimenting with those values.

hth!


But: multiple args will give logical OR?

1.kenthomas - Fri, 2009-10-16 19:53

If you do this (immediately above), your view will result in a logical OR, not an AND, correct?

~kwt


Or to put that somewhat

1.kenthomas - Fri, 2009-10-16 20:01

Or to put that somewhat differently: to robustly deal with tnames instead of tids, raises a number of issues, for instance, what behavior is desired by the user when tnames overlap in different vocabularies?

One could think through building a GUI handler for these situations, but so far, each of my clients' needs has been different enough to require a custom "twist..." and usually won't fit into tid args or multiple tax terms... (heck, in the end, I wind up abandoning views args handling code on anything big, and throwing an array of the nids I want @ views ... but that's another story). s,

What I'm saying is that to deal in tnames, (which most clients will want), with any sophistication, may not be possible from a GUI, and requires some planning &, most likely, custom coding.

~kwt