I have created a view (page, list of nodes) and have included the field "Taxonomy: Terms for Locations" (locations is the name of my vocab).

I am theming that View using the theme wizard.

The term is called to the template with <?php print $name?>

What I want to do is call $name twice - once for the node term, and once to get the most distant parent.

Heres what I mean - this is an example of my taxonomy tree;

Australia
- New South Wales
-- Blue Mountains

So if the nodes term is "Blue Mountains" I want to print "Australia" (the country).

I assume I will need to write a function in template.php and call it in the template (I suspect using taxonomy_get_parents_all).

Only problem is I really have no idea where to start, let alone how to grab the most distant parent and print it out.

Any help and guidance much appreciated, I am just learning Drupal and PHP so this stuff is a bit beyond me right now.

Comments

progga’s picture

If you have the vocabulary ID, then the following might be useful to find "Australia".

$adam_info = array_shift(taxonomy_get_tree($vid));
$adam = $adam_info->name;

Leon.lb’s picture

Thanks for the reply. Yes, I know the vid.

I see that array_shift will return the first element in the array - looks like smart thinking.

Hopefully I can make some ground on this today.

progga’s picture

I later realized that the first element might not always be the distant element. e.g. in this tree, every item has the same vid:

1
  1.1
    1.1.1
2
  2.1
    2.1.1

- running array_shift(taxonomy_get_tree($vid)) on this will return the taxonomy object for "1". taxonomy_get_parents_all(), OTOH, will return only the parents of the given tid. The last (or the one before the last, not sure) taxonomy object in this returned array is the one you are looking for. So doing array_pop() once (or twice) on taxonomy_get_parents_all()'s returned array might be the ultimate solution. Let us know what happens.

Leon.lb’s picture

Ok, using the first example, you are correct, indeed it gives me 1 (following you example above).

This the code I used;

$vid = 6;
$parent_info = array_shift(taxonomy_get_tree($vid));
$parent = $parent_info->name;
echo $parent;

I'm sure your suggesting to array_pop taxonomy_get_parents_all is correct, however, for the life I me I cant figure out how to put it all togeather. I'll keep working on it, any sugesstions would be most appreciated.

Leon.lb’s picture

The problem I am having (as far as I can tell) is getting the term ID from the variable $name, and then figuring out how to gets its parents.

Is that on the right track?

progga’s picture

What's the value of $name ? Is it just a taxonomy term? If so then you can find the taxonomy object using taxonomy_get_term_by_name($name). This'll actually return all taxonomy term objects matching $name. Then you'll have to look into the "vid" field of each of the objects to find the right term object. Hope this helps.

johnbeamer’s picture

Or you might find it easier to query the database directly ...

function F1PHome_active_taxonomy($nid) {


$term = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = $nid"));

do{
$term_old = $term;
$parent = db_result(db_query("SELECT h.parent FROM {term_data} d INNER JOIN {term_hierarchy} h ON d.tid = h.tid WHERE d.tid = $term"));
$term = $parent;
}while ($parent <>0);

return $term_old;
}

This finds the parents of a term ....

Leon.lb’s picture

Thanks John, might you briefly explain how I would use that, do I put that in template.php and call it into the template, if so how?

johnbeamer’s picture

Put it in template.php and just call it from page-xxx.tpl.php. Bear in mind that particular code was for Drupal 6.x so you'll just need to check that the database for taxonomy is the same in whatever version of Drupal you are using. I expect it is.

For instance in page-xxx.tpl.php

$call_function = $F1PHome_active_taxonomy($node->nid);

Leon.lb’s picture

Ok, I changed that to $call_function = F1PHome_active_taxonomy($node->nid); as I got the error "Function name must be a string...".

Anyway, that returns the correct TID (the most distant parents TID) - fantastic!

Sorry to be a bother, but how do I make it return the term name?

Would that perhaps be something to do with me using Drupal 5.x?

johnbeamer’s picture

what you need to do is to query the database. Something like this should work:

$name = db_result(db_query("SELECT t.name FROM {term_data} t WHERE t.tid=$term");

$term = the tid of the term you want the name of. You can then set return to $name.

This should work in D5

johnbeamer’s picture

Sorry the reason why the first code didn't work was because the "$" was a typo. Nothing to do with D5 vs 6

Leon.lb’s picture

I'm loath to ask for more help, however I don't really get it - not so sure what I am meant to change to get it to work.

johnbeamer’s picture

You can call that fn from anywhere. If you want my F1PHome_xxx_yyy() to return the name of the taxonomy put that fn just before the return. Set the t.tid to $term_old in the SQL and that should get the name of the tid associated with $term_old. Then return that value.

eg,

 $return_val = db_result(db_query(SELECT xxxxxxxx WHERE t.tid = $term_old);
 return $return val

Alternatively the F1Phome_xxx_yyy can return the number (tid) and you apply the fn in page-xxx.tpl.php file.

Leon.lb’s picture

Great!

Yes, now I understand - works perfectly!

John & progga - thank-you so much for helping out with this and hanging in there with me:) I owe you both a cold one.

To anyone else stumbling on this - here is what I ended up with:

In tempate.php:

function F1PHome_active_taxonomy($nid) {
$term = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = $nid"));
do{
$term_old = $term;
$parent = db_result(db_query("SELECT h.parent FROM {term_data} d INNER JOIN {term_hierarchy} h ON d.tid = h.tid WHERE d.tid = $term"));
$term = $parent;
}while ($parent <>0);
$return_val = db_result(db_query("SELECT t.name FROM {term_data} t WHERE t.tid = $term_old"));
return $return_val;
}

And in my templage (in my case views-list-xxx.tpl.php, but can be any template):

<?php print $call_function = F1PHome_active_taxonomy($node->nid); ?>

Great stuff!

cinquetooty’s picture

$parents = taxonomy_get_parents_all($sourceTID);
$oldestParent = $parents[sizeof($parents)-1];
print $oldestParent->name;

mindgarden’s picture

Do we need to define the tid in that code? (like specify a number). Because what happens if you have lots of nodes and want to return the parent for each one? You can't specify each tid in the code, right?

Also, for Drupal 6, do we put this code in the template.php and call it in the theme file?

oddible’s picture

Yes @cinquetooty has the simple method listed above. Thanks!

$parents = taxonomy_get_parents_all($sourceTID);
$oldestParent = $parents[sizeof($parents)-1];
print $oldestParent->name;
vthirteen’s picture

where should it be added? as a function in template.php page? or as php in node-type template page?

oddible’s picture

Wherever you need it. Do you need it as part of a function override in template or just in a page display?

asak’s picture

Thank you, that works great.

any idea how i can get now the children only?

vthirteen’s picture

this code used to work and then since a few weeks ago gives this error message:

    * user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 query: SELECT h.parent FROM term_data d INNER JOIN term_hierarchy h ON d.tid = h.tid WHERE d.tid = in /path/to/mytheme/template.php on line 61.
    * user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 query: SELECT t.name FROM term_data t WHERE t.tid = in /path/to/mytheme/template.php on line 64.

line 61 is
$parent = db_result(db_query("SELECT h.parent FROM {term_data} d INNER JOIN {term_hierarchy} h ON d.tid = h.tid WHERE d.tid = $term"));

[EDIT]
it seems that the error was caused by the fact that i was trying to use this code in a node type where taxonomy was chosen through a content_taxonomy field and not through standard taxonomy selector.

progga’s picture

> $parent = db_result(db_query("SELECT h.parent FROM {term_data} d INNER JOIN {term_hierarchy} h ON d.tid = h.tid WHERE d.tid = $term"));

"$term" is not defined. So you need to replace it with whatever the equivalent is. I am rewritten this line slightly:

$parent = db_result(db_query("SELECT h.parent FROM {term_data} d INNER JOIN {term_hierarchy} h ON d.tid = h.tid WHERE d.tid = %d", $term_id));

vthirteen’s picture

you're right, thank you