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

public
electBlake - Mon, 2008-01-28 18:55

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;

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);