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
Your code appears to be
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)
(I avoid hard-coding even array indexes when I can, so use array_pop, reset or foreach over arrays one one etc usually)
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Thanks dman, I'll check this
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
this doesn't work for me...
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.
array_pop off the path will
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
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Thanks dman, it worked
Thanks dman, it worked finally with a little change in your code:
Thanks again,
Best Regards.
Its worked,but you can use this simple code to fetch term id
You can fetch the term id using
$termid = arg(2);
Your code also help me a lot.
Thanks,
great, it really reduces the
great, it really reduces the no. of code lines.
get the arguments from the URL and check taxonomy term validity
the code that worked out great for me was the one posted here: http://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/functi...