0
votes

I'm trying to print the taxonomy terms (which is field_company) but it's printing node id of that article.

These are my available variables

code used in Global: PHP

<?php
$term = $row->field_company;
print($term);
?>

In my view Auto Preview, its showing like below.


article 1 Title

PHP: 2345 //these are node ids


article 2 Title

PHP: 2346 //these are node ids


article 3 Title

PHP: 2347 //these are node ids


article 4 Title

PHP: 2348 //these are node ids


What I'm expecting here is: for example in my taxonomy list (company) I have terms like,

company taxonomy terms list
-term1
-term2
-term3

article 1 Title

PHP: term1


article 2 Title

PHP: term2


article 3 Title

PHP: term2


article 4 Title

PHP: term3


article 5 Title

PHP: term1


2

2 Answers

0
votes

If the field field_company is a taxonomy reference field, then below should work.

$tid = $row->field_company;
$term = taxonomy_term_load($tid);
print($term->name);

But you say it is outputting a node id? Which means field_company is not a taxonomy term reference field, maybe it is a node reference field?
If it is referencing a node, then this may work.

$nid = $row->field_company;
$n = node_load($nid);
print($n->title);

Or you could use the entity api to load the term or node.

But,
you really should not be doing this with a Global:PHP field (in fact you should probably never use Global:PHP).
The output you want should be easy to achived in views (relationshiips) without using the Global:PHP field and will likely be faster and easier to change later if need be.

0
votes

You can also use the view object to collect your result,

Just take your field above global PHP and use foreach to get the term name :

<?php 
foreach($view->result as $k => $value){
print $value->field_field_company[0]['rendered']['#title'];
}
?>

Note- use isset if your field is not required.else you get errors.