Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By Tim-Erwin on
Hi,
I'd like to create a block which shows a list of items related to the current node. I know how to do this in Drupal 6: views with an argument with taxonomy_node_get_terms(). However, this function does not exist in Drupal 7.
What is the correct way to get the terms associated to a node in Drupal 7?
Is there even a better way? I noticed the option "Load default argument from node page, thats good for related taxonomy blocks." for the Taxonomy: Term ID argument. However, it doesn't seem to work (the option does not get saved).
Thanks
Tim
Comments
ugly solution
Well, I found a way:
$node = node_load(arg(1));
if ($node != NULL) {
$field = $node->field_my_vocabulary;
$terms_en = $field['en'];
foreach ($terms_en as $term) {
$terms[] = $term['tid'];
}
return implode('+', $terms);
}
In Drupal 7 taxonomies are stored in fields, in this example "field_my_vocabulary". This contains a language array of which each item contains the terms. It works.
What's ugly about it is the hard-coded "en" in line 4. Is there a more generic way?
Thanks for your solution. For
Thanks for your solution.
For the language I have tried doing the following:
$lang= $node->language;
$terms = $field[$lang];
What I don't know if it would work with multilanguage sites.. but for the sites set up for a language seems to work.
New SQL query
Hi,
I checked the code you were suggesting but debugging it I realized that the data that was returned was not the same that was obtaining with the taxonomy_get_term function.
In order to get a suitable solution I modified the query that is done in the function but taking into account the new tables in Drupal 7.
This is what the query looks like:
$result = db_query(
SELECT ttd.tid, ttd.vid, ttd.name, ttd.description, ttd.weight
FROM taxonomy_term_data AS ttd
INNER JOIN taxonomy_index ti ON ti.tid = ttd.tid
WHERE ti.nid= :nid
ORDER BY ttd.weight, ttd.name',array(':nid' =>$node->nid));
Now the returned data looks like what one obtained by the function in Drupal 6.
If you are working in a
If you are working in a single-language environment, substitute und for en.
saved by Pro Drupal 7 Development
A variation on the following worked for me, (credit goes to page 355 of Pro Drupal 7 Development):
Matt - What is the variation
Matt -
What is the variation you used? This returns the rendered field with field label. How would one get just the field values? I'm trying to convert the following to D7 in order to have a related content block view:
$node=node_load(arg(1));
if($node){
foreach($node->taxonomy as $term){$terms[]=$term->tid;}
return implode('+',$terms);
}else {return;}
ah Drupal, so easy and SO
ah Drupal, so easy and SO COMPLICATED!!
Here's how to get the taxonomy name; I don't know if it will work for everybody. Using the various variations of code in this thread and exhaustive foreach'ing, I found the following print statement after calling 'field_view_field':
I'll post the code I used to find that so that you can all get to the same solution even if your system is set up differently. Here are the steps:
1)Seeing the taxonomy name is easy:
2) What is render doing? After the code in step 1, I printed the array to see where the name was coming from:
3) A little bit much, how about this:
4)Now we're getting somewhere. We only want the tags though so only look at 'field_tags' key.
5) The TID is easy to get. Now we find the name itself. There's another embedded object in there so the following code goes inside the IF statement in step 4:
6) Finally, we can see the name inside the 'taxonomy_term' object with a 'print_r'. Here's that foreach again, but modified to show what we are looking for.
These steps should let you see all taxonomies, but I only have one. The name is therefor inside the 'taxonomy_term', of the first array inside the 'und' key field of the field_tags array, which is inside the '#object' field of the result from field_view_field().
print $result['#object']->field_tags['und'][0]['taxonomy_term']->name;
Easy right? XD
For anyone showing up here
For anyone showing up here later and wondering what this starred comment meant:
Und means "Undefined." That part of the array determines the language the site is being viewed in, since settings inside the structure might vary for different languages. Therefore, calling $field['und'] is not a very portable snippet - this code would likely fail with a multilingual site.
Works for me. But I want to
Works for me. But I want to have only the plain term name to get printed. Any idea? Thnx
If no one else has already
If no one else has already mentioned it, "field_tags" should be replaced with the "machine name" of the vocabulary. It can be found on the "manage fields" tab when editing the content type. If you're upgrading an older Drupal site, it might be "taxonomy_vocabulary_2" or something else.
It took me a while to figure that out, not knowing that "field_tags" is a specific vocabulary in Drupal 7 and not "print out whatever taxonomy is associated with that page" like $terms did in older versions of Drupal.
An answer
An answer from #959984: taxonomy_node_get_terms doesn't work with drupal 7
For non-php programmers, I believe that CCK Blocks module can do this for you, but it will only display one field per block, not ALL terms for the node.
Thanks for pointing me in the right direction
Scott,
Thanks for the update with link to that discussion. I've been struggling with this (finding terms associated with current node) in template.php for several days and was beginning to question my sanity. That discussion thread sheds a great deal of light on the issue.
Cheers,
Andrew
hi tim ;)
hi tim ;)
--
keine zeit für spielkonsolen mein leben ist jump n run!
valderama.net
I'd love to see a reason as
I'd love to see a reason as to why this function was removed, I can't seem to get anything to work correctly anymore. I understand taxonomy was revamped and changed a fair bit, but i'm sure the function could of been modified.
Doing such a simple task has become way to complicated and difficult it's to the point it's ridiculous.
Completely agree
How does the taxonomy module not support returning all terms for a node? It's kind of ridiculous that any module that wants to get the term names for a node has to manually muck through the db to grab the information. Sure all fields are automatically loaded with node_load, but in no way are they marked as being taxonomy terms.
For anyone who is interested
For anyone who is interested this can be accomplished with the use of Views and a contextual filter.
This basically will force the results to be node specific so add a contextual filter : Content: Nid.
"When the filter value is NOT in the URL"
-> Provide default value
- Type: PHP Code
- PHP contextual filter code: return arg(1);
Thanks
Thanks that work for me very well!!!
Many Thanks!
THANKS FOR ALL THE SOLUTIONS!
here's the version to display all the tags seperated by ',' as string stored in $tags variable.
NOTE:
i pasted the codes into my theme's node.tpl.php file
you can paste the code under your block or article body with php filters
DRUPAL 7:
That's great - and this does
That's great - and this does the same thing but with a bit less PHP:
page template or region template
Thanks to everyone this was helpful. I found I needed a two slight variations to work in page and sidebar templates..
OR
Thank you so much! I've been
Thank you so much alienzed and everyone! I've been searching for days. Not being a programmer, this was very difficult. I needed to print different URL Links based on the taxonomy term name from a custom node template file. This did the trick! I only have one registration page URL available now. I will change this to a 'switch' statement when I get additional landing pages for the registration. This was the code I used if anyone is interested.
Here's a simple way to
Here's a simple way to iterate over the tags in a node.
Hope that helps.
Another way of getting taxonomy terms
This may not be the preferred way to get taxonomy terms, but I've done it this way before:
This creates two variables: $tags, which is a comma delimited list of tags and $facets, which is a comma delimited list of respective vocabularies.
Getting taxonomies for a node
I know it's a bit late of a reply on this, but I had a similar problem in that I had the node object but needed all of its taxonomy ids to pass into a view result - I took this approach as it does not require mucking about with language settings or markup:
No SQL required and no language issues.
Here is a generic function
Here is a generic function that does this:
Cheers,
Albert
Three years and this is still
Three years and this is still an issue... With entities and fields drupal has become very compley, IMHO. Thanks for this clean solution, Albert!
Modification
Thanks @Alberto56 for the code. Definitely smart code but there are modules othen than "taxonomy" that handle taxonomy fields. Like "term_reference_tree".
So modify this line...
if ($info['widget']['module'] == 'taxonomy') {
With this line... (and for everybody pay attention if some other module is handling taxonomy fields)
if ( in_array ( $info['widget']['module'], array ('term_reference_tree', 'taxonomy' ) ) ) {
Hope that helps!
Drupal Backend / Frontend Developer
Unfortunately, not generic enough
The function
MYMODULE_taxonomy_terms($node)
provided by alberto56 works if the widget used to populate an taxonomy field is of the autocomplete kind.Unfortunately, not only does it miss taxonomy terms handled by fairly specific widgets such as
term_reference_tree
, it fails to spot taxonomy terms that are selected by means such as radio buttons and checkboxes. These widgets are also used by fields that are not taxonomy terms, so scanning for them may produce false positives.Most other solutions posted here are even less general, and assumes the name of the field associated with the vocabulary (e.g.
field_tags
) is known.Still looking for a generic solution.
- gisle
Here's a very generic way
Here's a very generic way to grab all terms without specifying field names and no db_query:
simple way to get taxonomy terms name or id
here is another simple way to get taxonomy terms name or id related to the current node.
that's it!