Gosh, I can't believe how hard it is to find a short, working, updated code snippet. I was able to solve my problem only after stitching together bits and pieces from multiple threads. I'm hoping the ff could help others after me, in the same situation.

Problem: I needed to call the value of a custom field inside a node so that my PHP code would have something to work on.

What I did: After installing Views PHP, I went to Views and created a new Global PHP Field. In that new field, there are three places that can take code: Setup code, Value code, and Output code. For the following snippets to work, they must be placed in the Output code field.

Don't forget: the Output code field requires the starting and ending PHP tags.

For the current node, I used to following snippets to display various items:

NID:
print $data->nid;

Title:
print $row->title;

Raw text value of a custom field (text format):
print $data->field_field_of_mine[0]['raw']['value'];

Raw TID of a custom field (term reference):
print $data->field_field_of_mine[0]['raw']['tid'];

Notice that the last two snippets above say "field_field_" instead of just "field_"? I got the recommendation from one of the many many comments in the queue. Seems to do the trick.

I am not sure if these code snippets will work everywhere, and I don't know how long before they get outdated. But as of today, 2012-April-04, they all work for me.

Good luck.

Comments

johnv’s picture

Buckminster_Bond’s picture

Really helped me - thank you.

jmatuska’s picture

thanks, molave. that is perfect.
and the "field_field_" tip was very helpful!
cheers, Jean

tms8707056’s picture

Why doesn't print_r($data) work when placed in the output code section?

MHLut’s picture

For me, print_r($data); (surrounded by PHP delimiters, as it's the output field) does work with a content view. You could also try returning $data in the value field and then use print_r($value); in the output field. In theory, this should not make any difference, but better be safe than sorry.

relish27’s picture

This works great -- thank you. I'm still having problems, though, when trying to display a file path. I've tried both adding the file field itself to the Field list (displaying URL to file) and also the File Usage/File Path field. The PHP attribute options continue to offer only $row->uri as an option and that only displays the uri. I've just guessed and tried many variations, but still cannot seem to get the full path to display.

Any thoughts?

MHLut’s picture

@relish27 I'm not sure whether I understand your problem. But there are two things that came up in my mind:

  1. You can only use another field (token) in a field, when this token comes from a field that is added before the field you want to use it in. In other words: make sure field A is displayed above field B in the fields list, if you wish to use the value of field A in the settings of field B.
  2. Links can be displayed as absolute URLs. To do this, open the field settings of your path (or other link) field. Under rewrite results, you will find the option to use an absolute link. This way, the value of the field will change to the full URL of the link your field represents.
MtRoxx’s picture

This post has helped a great deal, thank you very much.

My challenge was to create a simple link. To help others here is what I wound up with

print 'view_node.'"> '. $row->title.'';

MHLut’s picture

@MtRoxx, you might want to look into the l() function.

MtRoxx’s picture

I will check it out, thank you. For the time being I am using the code below.
print '<a href="'. url($value, array('absolute' => TRUE)).'node/'.$row->nid.'">Learn more</a>';

kloewer’s picture

Thank you! This post should be in the readme!

zephirus’s picture

Thanks! How can a simple task have to be so difficult :D

truyenle’s picture

Thank just to post here in case some need it in case of a field of type link, to get url
print ($data->field_field_video_url[0]['raw']['display_url']);

stinky’s picture

Any other ideas on how to print the value of a link? I tried the following, but it doesn't print anything and I'm getting Undefined property errors.

$mydb = ($data->field_field_resource_url[0]['raw']['display_url']);
print $mydb;

dan.mantyla’s picture

To get the taxonomy name from the taxonomy field for a node in the view:

$data->field_field_my_taxonomy_field_name[0]['raw']['taxonomy_term']->name;

(had to decipher a var dump to figure that one out myself). If the field name for your taxonomy is News Section, then it would be:

$data->field_field_news_section[0]['raw']['taxonomy_term']->name;

It might be wise to do this if the taxonomy is not a required field when creating the content:

$taxonomy = isset($data->field_field_my_taxonomy_field_name[0]['raw']['taxonomy_term']->name) ? $data->field_field_my_taxonomy_field_name[0]['raw']['taxonomy_term']->name : "";

EDIT: I can only get this to work for the PHP field in the FIELDS and not FILTER CRITERIA
:(

skrtvm’s picture

Thank you Very much.

I am looking for this solution for the last 1.5 week....

one more doubt.. if the field value is a date.. so how can I format that and display it..?

please help me if anyone knows..

jay.lee.bio’s picture

I think some of the info here is outdated, especially regarding the variables $data & $row: https://www.drupal.org/node/1140896, #183

kiramarch’s picture

If you're trying to trace back where your values are -- this made it possible for me to sort through all the mess of fields in $data:

  • Install the Devel module
  • Temporarily add this to your output field, inside the PHP tags: dpm($data);
  • Save your view and go to a page where you can look at the results
  • Click on the Devel tab
  • Click on the table to expand items and see how they nest.
Liam Morland’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

ghaleb’s picture

please how to print (name) in global php view.
[field_field_reference_user] => Array
(
[0] => Array
(
[rendered] => Array
(
[#theme] => entityreference_label
[#label] => nourhan
[#item] => Array
(
[target_id] => 52
[entity] => stdClass Object
(
[uid] => 52
[name] => nourhan
[pass] =>

Liam Morland’s picture

Please create a new issue for your request.

ghaleb’s picture

done sorry thank