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
Comment #1
johnvI made a reference of this nice explanation in #1140896: Variable $row does not contain correct values ($data->_field_data does)
Comment #2
Buckminster_Bond CreditAttribution: Buckminster_Bond commentedReally helped me - thank you.
Comment #3
jmatuska CreditAttribution: jmatuska commentedthanks, molave. that is perfect.
and the "field_field_" tip was very helpful!
cheers, Jean
Comment #4
tms8707056 CreditAttribution: tms8707056 commentedWhy doesn't print_r($data) work when placed in the output code section?
Comment #5
MHLut CreditAttribution: MHLut commentedFor 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.
Comment #6
relish27 CreditAttribution: relish27 commentedThis 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?
Comment #7
MHLut CreditAttribution: MHLut commented@relish27 I'm not sure whether I understand your problem. But there are two things that came up in my mind:
Comment #8
MtRoxx CreditAttribution: MtRoxx commentedThis 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.'';
Comment #9
MHLut CreditAttribution: MHLut commented@MtRoxx, you might want to look into the l() function.
Comment #10
MtRoxx CreditAttribution: MtRoxx commentedI 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>';
Comment #11
kloewer CreditAttribution: kloewer commentedThank you! This post should be in the readme!
Comment #12
zephirus CreditAttribution: zephirus commentedThanks! How can a simple task have to be so difficult :D
Comment #13
truyenle CreditAttribution: truyenle commentedThank 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']);
Comment #14
stinky CreditAttribution: stinky commentedAny 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;
Comment #15
dan.mantyla CreditAttribution: dan.mantyla commentedTo get the taxonomy name from the taxonomy field for a node in the view:
(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:
It might be wise to do this if the taxonomy is not a required field when creating the content:
EDIT: I can only get this to work for the PHP field in the FIELDS and not FILTER CRITERIA
:(
Comment #16
skrtvm CreditAttribution: skrtvm commentedThank 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..
Comment #17
jay.lee.bio CreditAttribution: jay.lee.bio commentedI think some of the info here is outdated, especially regarding the variables $data & $row: https://www.drupal.org/node/1140896, #183
Comment #18
kiramarch CreditAttribution: kiramarch as a volunteer commentedIf 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:
Comment #19
Liam MorlandComment #21
ghaleb CreditAttribution: ghaleb commentedplease 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] =>
Comment #22
Liam MorlandPlease create a new issue for your request.
Comment #23
ghaleb CreditAttribution: ghaleb commenteddone sorry thank