1

I have a views that include node title, a text field and a taxonomy term reference field.

I need to print the taxonomy term name via Global: PHP field (Views PHP) but I couldn't.

How to print taxonomy term name with Views PHP?



I can print the node title with that code:

Value Code:

return $row->title; 

Output Code:

<?php print $value; ?>


or without Value Code:

Output Code:

<?php print $row->title; ?>


And I could print the taxonomy term ID with that code:

Output Code:

<?php print $data->field_field_my_taxonomy_term[0]['raw']['tid']; ?>


But using name or value instead of tid doesn't return anything.


I also tried to print the term name with these ones but unfortunately none of them worked:

$data->field_my_taxonomy_term[0]['raw']['value'];

(Above code prints nothing.)


<?php $data->field_my_taxonomy_term[0]['raw']['taxonomy_term']->name; ?>

(Above code prints nothing.)


So, I'm confused now and not know how to print taxonomy term name with Views PHP?


Note: I looked at these but still couldn't solve the problem:

  1. Views Php how to select actual term name not term ID

  2. https://www.drupal.org/node/1707002

  3. https://www.drupal.org/node/1516348

2 Answers 2

1

Unless the term name is in the query for the View, you'll need to load the object to get its properties:

$term = taxonomy_term_load($data->field_field_my_taxonomy_term[0]['raw']['tid']);
$name = $term->name;

That will incur a performance cost, so it would be better to add the relationship to the term in the View itself, and add the name as a hidden field. Then you can refer to it without having to load any entities.

4
  • Actually, the term name was in the query for the View but didn't work. Now, it's working correctly and I'm very surprised to see the result. Thanks a lot. And a note for the friends in the same situation: my actual field name is field_my_taxonomy_term and in the code above doesn't work without adding the field_ before field_my_taxonomy_term (so it becomes field_field_my_taxonomy_term). My mistake was that.
    – herci
    Jun 30, 2015 at 10:15
  • How can I use the code above for multiple terms instead of one? I tried to add a foreach loop but didn't work. If all the nodes have equal numbers of terms it's may be easy but each node can have 0 to 7 terms.
    – herci
    Jun 30, 2015 at 11:42
  • 1
    Yep foreach or for, either is fine. Can't tell you which/how as I can't see your data, but it should be straightforward in theory
    – Clive
    Jun 30, 2015 at 12:07
  • For multiple terms you can use that code: foreach($data->field_field_my_taxonomy as $mydata) { $term = taxonomy_term_load($mydata['raw']['tid']); ... }
    – herci
    Jun 30, 2015 at 21:12
2

Storing PHP code directly on database is not a correct method, it causes many issues. Better you can add the relations on your views - "Content: Taxonomy terms on node". and then add the taxonomy term name field as a "hidden field"

Print the value via "Global: Custom text" field if required any custom wrapper around of it.

1
  • Yes, I know "storing PHP code directly on database" is not a good way but I'm using this View just for a while (to export data). And because I'm manipulating the term name, using "Global: Custom text" doesn't work for this case. Thanks.
    – herci
    Jun 30, 2015 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.