Hello,

I have a List field with 3 different sets of Key|Label options set. The field shows up correctly in views. However when I try to pull it up in the Global PHP field using it's variable all I get is the NID. What am I doing wrong?

Available variables

    $view: The view object.
    $handler: The handler object.
    $static: A variable that can be used to store reusable data per row.
    $row: Contains the retrieved record from the database (e.g. $data->nid).
    $row->views_bulk_operations: Content: Bulk operations
    $row->title: Content: Title
    $row->field_auto_remove_content: Content: Automatically Remove Content
    $row->changed: Content: Updated date
    $row->php: Global: PHP
    $row->nid: Content: Nid
    $data: Contains the retrieved record from the database (e.g. $data->nid).
    $value: Value of this field.

I was trying echo $row->field_auto_remove_content;. Then I tried echo $row->field_auto_remove_content[$value];, but this wasn't any more successful. I'm obviously doing something wrong. Please help.

Thank you in advance.

Files: 

Comments

dawehner’s picture

Status: Active » Fixed

That's drupal7, only the nid is part of the query. You have to load the full entity via node_load and then get the value you need.

bluestarstudios’s picture

Oooh. Ok. now sadly my knowledge on how to do that is pretty limited. Could anybody please explain/show me what you mean? Thank you.

bluestarstudios’s picture

Status: Fixed » Active
liquidcms’s picture

yea, this is a bug.

at the very least PHP field shouldn't show a list of variables that are available to use to create the php field when all of them simply return NID....

http://screencast.com/t/yU1wghP5KO

...and i was so happy when i saw this, thinking views finally sorted out consistent variable names..

liquidcms’s picture

actually it seems as though both $data and $row are pretty much useless.

is there really no way to do a custom field now in views that has access to the fields above it?

endiku’s picture

is there really no way to do a custom field now in views that has access to the fields above it?

ditto question. It should be straight forward to refer to a field above the global php. You can easily refer to values using replacement patterns in the same global php field, inside the rewrite results area. Yet the php execution areas cannot?

liquidcms’s picture

this seems to work now since i have updated to latest dev of Views. not sure if this is simply specific to the latest view that i am working on or if the bug was actually fixed (will need to go back to the odler view and see if fixed there).

endiku’s picture

How is that? I just installed the dev from today and I'm still not seeing any fields passed to the global php.

If I use a global counter above the global php, and then return $row, the $row->counter is empty, while $row->title has info.

trgreen17’s picture

Subscribing.

Same here, I'm having trouble getting anything useful out of "Global PHP" fields in Views 3. I can only get NIDs.

liquidcms’s picture

everything is available in a PHP field; it just isn't mapped to the "tokens" as described in the description text when you are using this.

if you use the D6 way of pulling out pieces of the $data object you can still do whatever you like... but, being able to use the tokens without needing to always put in a dpm() call to find out what the var names is would be nice... which i think is the point of this thread.

trgreen17’s picture

@liquidcms:

can you explain "the D6 way of pulling out pieces of the $data object" or just point me to the right place? I've spent all day trying to get a simple query to run in a "Global PHP" field. The query works fine in phpMyAdmin. Here it is: SELECT `tid` FROM `taxonomy_index` WHERE `nid` = 353

I'm just trying to get the tid from the taxonomy_index table for the current nid. They are both in the same table so no joins necessary.

Here's what I've got in the Global PHP field, but I'm getting errors. Ten hours on one query. This is crazy. Can somebody please help me?

<?php
$nid = ($row->nid); // to get the current nid
node_load($nid); // it was mentioned above that we have to use this
$result = db_query("SELECT tid FROM {taxonomy_index} WHERE nid = :nid", array(':nid' = $nid)); // got this from the docs
print_r($result->tid); // output the tid
?>

Please, any help at all very much appreciated. Thanks.
Tim

bpriceless’s picture

Here's a possible solution, probably along the lines of "pulling out pieces of the $data object". My case is using a Views, Global PHP field to allow iframe code within a views block. I needed to insert a value (user twitter name) into part of some iframe code. View fields were defined and then excluded from the display. All of the Global PHP code is within the OUTPUT CODE area of the Global PHP field. (Versions: Drupal 7, Views 7.x-3.0-rc3, Views PHP 7.x-1.x-dev)

Start with:

<pre>
<?php
print_r($data);
?>
</pre>

to see all of the data values available (totally forget about the "Available Fields" stuff, which is incorrect and obviously doesn't work)
Then drill down into $data to get closer to the value you are looking for. For example:

<pre>
<?php
print_r($data->field_field_agenttwitter);
?>
</pre>

This also provides the exact array arguments to get to the value you want. In my case:

<pre>
<?php
print_r($data->field_field_agenttwitter['0']['raw']['value']);
?>
</pre>

And the final result with a check for field existence and in the iframe tags was:
(a very simple follow me on twitter button. iframe code found at https://dev.twitter.com/docs/follow-button#followers-count-display):

<?php 
if (empty($data->field_field_agenttwitter)) {return;}
$tw = $data->field_field_agenttwitter['0']['raw']['value'];
?>
<iframe allowtransparency="true" frameborder="0" scrolling="no"
  src="//platform.twitter.com/widgets/follow_button.html?screen_name=<?php print $tw; ?>&show_count=false"
  style="width:60px; height:20px;"></iframe>
 
dawehner’s picture

Status: Active » Fixed

I guess this support issue can then be closed.

3rdLOF’s picture

bpriceless is right on the money. I just built a massive HTML5 widget using field collections (14 field collections with 3 fields in each, 1 one them unlimited) using precisely that technique and it was awesome once you get the flow going.

Status: Fixed » Closed (fixed)

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

dalearyous’s picture

the print_r($data->field_field_agenttwitter['0']['raw']['value']); seems to work if it is left in the output code field but what if you need that value in the value code part ( i need to reuse the value)? nothing i try seems to work!

if it was working this should be right:

$tcb = db_query("
SELECT SUM( field_cost_budget_value ) 
FROM field_data_field_cost_budget, field_data_field_award_number_s, field_data_field_project_number_s
WHERE field_award_number_s_value = $row->field_award_number_s
AND field_project_number_s_value = $row->field_project_number_s
AND field_data_field_cost_budget.entity_id = field_data_field_award_number_s.entity_id
AND field_data_field_cost_budget.entity_id = field_data_field_project_number_s.entity_id")->fetchField();
return $tcb;

since its not i do this like stated in the above post:

$tcb = db_query("
SELECT SUM( field_cost_budget_value ) 
FROM field_data_field_cost_budget, field_data_field_award_number_s, field_data_field_project_number_s
WHERE field_award_number_s_value = '$data->field_field_award_number_s['0']['raw']['value']'
AND field_project_number_s_value = '$data->field_field_project_number_s['0']['raw']['value']'
AND field_data_field_cost_budget.entity_id = field_data_field_award_number_s.entity_id
AND field_data_field_cost_budget.entity_id = field_data_field_project_number_s.entity_id")->fetchField();
return $tcb;

but with that i get nothing

vaccinemedia’s picture

What I'm after achieving is the following:
I am trying to create a virtual tours website and have the following fields for the virtual tour content type in Drupal 7:

Name of location (text)
Tour type (values are either 100 or 180)
Company logo (if applicable)
Virtual tour images (multiple value image field)

On an earlier Drupal 6 site I achieved showing the VTs in a lightbox by using contemplate although I could have just created a tpl.php file for the virtual tour content type. The code basically went through each image and checked as to whether or not the value of tour type was 100 or 180 and chose the correct xml file accordingly. See attachment. I'm struggling to achieve the same in D7 using templates probably due to lack of knowledge so I'm trying to achieve the same thing using views using the php field to ask the "if statement" in php to output the correct markup depending on what the value of "Tour type" is.

Tour type is being fed out as the NID in each case as above. So I used the method outlined also above to grab the value So far so good. However, when it comes to obtaining the file path to the image I'm only getting an URI of path://etc...

It would be far easier to have the thumbnail as a field, the path to the image as a field and loop through them and generate the correct markup depending on the value of "Tour type" instead of NID.

Any ideas on how I can achieve this?

vaccinemedia’s picture

Status: Closed (fixed) » Needs work
Nick Robillard’s picture

Yup. I'm using Services to get views data, which pulls from the value (not output) so I'm in the same predicament. I've hit the same walls everyone else on this thread has. Sure views php field is kind of a hack but I rely on it a lot. Especially in my case using Services.

liquidcms’s picture

would it be possible to close this or mark as duplicate of this: #1140896: Variable $row does not contain correct values ($data->_field_data does)

especially since this isn't an issue with Views, it is an issue with the Views PHP module.

liquidcms’s picture

Issue summary: View changes

typo correction

hondaman900’s picture

Issue summary: View changes

@bpriceless #12, thank you for that clear example, it works as noted.

How can I expand that code to access the field if it's in a different entity than the one for the row I'm on? My view lists entityForm submissions, and I have one separate entityForm for user settings, but I need a setting to show up on every row of my view. It's only showing up now on a row for it's own entityForm ID. I want to use Global:PHP to place that value on every row, even if the field placed doesn't belong to that row's entityForm.

Any suggestions as to how to connect these two items?

hondaman900’s picture

For others looking to do this, I found the following workaround:

  1. Use your favorite database management tool (PHPMyAdmin, MySQL Workbench, dbForge...) to create the SQL query that extracts the data field/element you want.
  2. Copy and paste that SQL statement into a Drupal db_query statement in your PHP field, in the form of:

    $myVariable = db_query("my SQL statement pasted here")->fetchField(): and then print/echo your $myVariable.

    For example:

    $fedCGperiod = db_query("SELECT
      field_data_field_long_term_gain_holding_per.field_long_term_gain_holding_per_value
    FROM {field_data_field_long_term_gain_holding_per}")->fetchField();
    
    echo $fedCGperiod;

    Remember to enclose the table reference in curly braces "{}"

  3. The field value should now show up per row in your view

Hope this helps.