Hi,

Modules I'm using:
CCK + content_taxonomy + Path Auto + Views

Requirment:
I want to filter a view block based on the Term in the URL. Since view blocks do not see arguments so it is implemented it as:

Add an argument and setup this as follows:
-Action to take if argument is not present:
-Provide default argument
-PHP Code

//get alias of URL
$path = drupal_get_path_alias($_GET['q']);

//break path into an array
$path = explode('/', $path);

$arraysize = sizeof($path);

if ($arraysize>0) {
  //return the last argument e.g. http://www.mywebsite.com/departments/general will return general
  return $path[$arraysize - 1];
}

Problem:
I can not use the term in the URL as used in the example above since content_taxonomy (CCK Taxonomy Field) views implementation does not support this. It has to be a taxonomy term ID. so the code requires a little change.

How do I get the Term ID for a give taxonomy term (from the URL)


//get alias of URL
$path = drupal_get_path_alias($_GET['q']);

//break path into an array
$path = explode('/', $path);

$arraysize = sizeof($path);

if ($arraysize>0) {
$term_ids_array=taxonomy_get_term_by_name($path[$arraysize - 1]);//get the term ID of this Term name and return.
return $term_ids_array[0];
}

But this is not working, if I simply return a static value as term ID it works

return 1; //assuming "general" term's id is 1.

Somebody please suggest me what is wrong in my approach to get the term ID for a given term name, Could you please also suggest me how can I use "Devel module" to fix it?

Regards

Comments

dman’s picture

Your code appears to be returning the $term object, but you are saying you want to return the $tid number.
Make the code a bit more readable (and therefore don't need comments)

$termname = array_pop($path);
$terms = taxonomy_get_term_by_name($termname);
$term = reset($termname);
return $term->tid;

(I avoid hard-coding even array indexes when I can, so use array_pop, reset or foreach over arrays one one etc usually)

I am learning’s picture

Thanks dman,

I'll check this and get back, unfortunately there is some problem with the internet service provider at my end, the moment my site is back, I'll do the way you have suggested.

Regards

baisong’s picture

As a PHP default value for my term ID argument, this doesn't appear to work. I'm testing it as the only argument of my view, and currently I'm in the same boat as the original post-- tid's work, but names don't.

dman’s picture

array_pop off the path will only work if your URL includes the name or something.
That's not actually the normal case unless you are using pathauto or manual links.
It depends on a lot.

The ACTUAL Url of the context you are trying to use this in, for a start

I am learning’s picture

Thanks dman, it worked finally with a little change in your code:

$path = drupal_get_path_alias($_GET['q']); //added this to get the query string (Nice URL / path auto)
//$termname = array_pop($path); //gives me following error:
/*
warning: array_pop() [function.array-pop]: The argument should be an array in /home/ilng/public_html/jmh/sites/all/modules/views/plugins/views_plugin_argument_default_php.inc(48) : eval()'d code on line 2.
*/
//it gives me error because it is a string, so we need to explode it in order to get this in an array.


//break path into an array
$path = explode('/', $path);
$termname = array_pop($path);
//Nice URLs converts spaces into - (dashes), so get the space back e.g. general-medicine in URL is a term called general medicine.
$termname = str_replace("-", " ", $termname);
$terms = taxonomy_get_term_by_name($termname );

//$term = reset($termname); //I believe a type error in your code, $terms is the object not the $termname
$term = reset($terms);
return $term->tid;

Thanks again,
Best Regards.

kajal4ever’s picture

You can fetch the term id using

$termid = arg(2);

Your code also help me a lot.
Thanks,

I am learning’s picture

great, it really reduces the no. of code lines.

luthien’s picture

the code that worked out great for me was the one posted here: http://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/functi...