I have created a view of stories, and I need to add an icon next to stories who's tid is 622.

I have added the field taxonomy term: all, and specified my vocabulary (since these stories have two vocabularies each), and am using the following code in Custom Field:

<?php
  if($node->term_tid == "622") print "<img src=\"/images/breakingnewsgif\" border=\"0\" height=\"12\" width=\"12\" alt=\"Breaking News\">";
?>

I can't seem to get this to work no matter what I try. Anyone know how to find one specific taxonomy?

Comments

infojunkie’s picture

Try $data instead of $node.

djudd’s picture

Thank you for the advice, but I'm afraid that didn't work either.

Any other ideas? I'm stuck here.

infojunkie’s picture

You should examine the contents of the $data object (using print_r for example) to find the name of the attribute you need.

djudd’s picture

I really do appreciate that tip, and it worked wonderfully.

The problem is that Views apparently doesn't give the tid information when using Taxonomy Term: All, so I may just be out of luck here.

My fields are Title, Premium, and Taxonomy Term: All and this is what $data gives me...

stdClass Object ( [nid] => 84875 [node_title] => Fort battalion’s move to Texas to take $120M yearly toll [premium_nid] => [node_vid] => 84876 [nodequeue_nodes_node_position] => 2 )

nathanjo’s picture

Where did you place that code? If you use the THEME suggestion from the views, you can check the $row and $field. Are you sure the node has taxonomy value? Did you check the "exclude from display" is not check for that field?

noslokire’s picture

Alas, after a long battle I got it to do what I wanted and since I suffered so, I hope this helps the next battered soul!

<?php
$res = db_query("SELECT t.tid FROM term_node t INNER JOIN node n ON t.vid = n.vid WHERE t.nid = %d", $data->nid);
  while ($row = db_fetch_array($res)) {
    $out[] = $row['tid'];
  }
  $out = implode('+', $out);
  return $out;
?>
karljohann’s picture

I'm having the same problem. I would really prefer there was a way to do this without repeating the db query though...

infojunkie’s picture

You could use the taxonomy function taxonomy_node_get_terms($node), like so:

<?php
$node = node_load($data->nid);
$terms = taxonomy_node_get_terms($node);
// do something with the terms...
?>
thommyboy’s picture

Category: support » bug

same problem here. yes- there are workarounds using node_load but this will cause (unnecessary) db-queries. it seems to be a bug that a "taxonomy:all terms" field is not included into $data while "taxonomy:term" is?

dennys’s picture

$node = node_load($data->nid);
$terms = taxonomy_node_get_terms($node);
print_r($terms);
// do something with the terms...

After I got $terms, how could I got tid? I use $terms->tid, but it doesn't work. But print_r($terms) shows the correct tid in the array.

visheshd’s picture

$terms is an array ... try using $terms[actual-vocabulary-id]->tid;

Renee S’s picture

I've found this too. I looks to me like a bug.

prototyp1278’s picture

Version: 6.x-1.x-dev » 6.x-1.0

Hi all ! I need custom php. Can somebody help me. i need some source like.

$node = node_load($data->nid);
$terms = taxonomy_node_get_terms($node);
if ($terms[2]->tid==255) print "NEW";

I need print custom text when one of node terms is term id 255.

Thanx 4 answers Pavel

avibrazil@gmail.com’s picture

I have the same problem. This is definitivelly a bug.

seanr’s picture

This is still a problem even now. Over a year since this was reported. Why on earth doesn't Taxonomy: All Terms get added to the data array? Should this maybe be reported to Views, or is this specific to Views Custom Field?

kalimero’s picture

Hi everybody!

This bug is very annoying but I have a solution that despite of not being very elegant, doesn't involve node_load or make a query:

We must take advantage of $view parameter of function like that

<?php
$fields = $view->style_plugin->rendered_fields;
// here we have all fields of all rows, we need a loop
foreach($fields as $field) {
  if($rows->nid == $field['nid']) {
    // fields of this row, we take taxonomy:all terms field
    $terms = $field['tid'];
  }
}
?>

Ok, I know this solution it's not ver classy but it works and we don't need call node_load or make a cost query

Ps: sorry for my bad english

skolesnyk’s picture

Issue with status 'won't fix'on Views Issues list. Wow.

http://drupal.org/node/443186

orjantorang’s picture

My solution to the problem of not finding the term tid or term name in $data object is to add the display of the term name or term tid once again, even if you already have Taxonomy: All terms

Fields
Taxonomy: All terms Status
Node: Title Ticket
...
Customfield: PHP code PHP code (no terms visible in $data here)
Taxonomy: Term State name (add the term (name/tid) again)
Customfield: PHP code State (now you can find term data in $data)

[term_data_name] => BS_STILL_RUNNING
[term_data_vid] => 1
[term_data_tid] => 10
[term_data_weight] => 53

scotwith1t’s picture

Just FYI, this causes problems if you're dealing with multiple value taxonomy. Otherwise it works fine.

scotwith1t’s picture

duplicated