Community Documentation

Views PHP

Last updated January 14, 2013. Created by peterx on July 16, 2012.
Edited by alibama, olleolleolle. Log in to edit this page.

Add PHP to your views. Use PHP to select rows from the results.

Here is an example of PHP code used in a view filter field.

$nid = db_query('SELECT field_reference_nid FROM `field_data_field_reference` WHERE entity_id = ' . $data->nid)->fetchColumn();
if (!isset($static[$job_nid])) {
  $uid = db_query('SELECT uid FROM `node` WHERE nid = ' . $nid)->fetchColumn();
  $static[$nid] = $uid;
}
global $user;
return $user->uid != $static[$nid];

Forget the quality of the code. It is just a quick example to test Views PHP.

See http://drupal.org/node/1678082 for information on how to access the PHP.

Open the Available variables section to see the data supplied to your code. You can add fields to the $row variable by adding them as fields in your view. If you do not want them displayed in your view, use the view option to exclude them from the display.

Use the $static variable to carry data from one row to another. In the example, I read the database once to get a uid from a related node and store each result for subsequence rows. In my test case, each entry is reused ten or more times.

db_query is officially the fastest way to read the database in D7 and you do not hog memory caching whole nodes. You might find this type of PHP faster than joins for some data lookups.

If you start using a lot of code like this to lookup the same data, you might want to add a module or a node reference or some other trick to automate the process instead of using PHP in views. This type of code is just a quick way to make stuff work. For my example, I will eventually converted it to a module providing a filter.

Available variables

Here is the list of available variables in my example filter before I added some fields.

  • $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->title: Content: Title
  • $data: Contains the retrieved record from the database (e.g. $data->nid).

Here is the list of available variables in my example filter after I added some fields.

  • $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->title: Content: Title
  • $row->field_name: Content: Name
  • $row->field_phone: Content: Phone
  • $row->field_text: Content: Text
  • $row->field_ref_number: Content: Ref. no
  • $row->field_short_description: Content: Short Description
  • $data: Contains the retrieved record from the database (e.g. $data->nid).

While in Drupal 6 the $row->field_goes_here would return the actual data of the field in Drupal 7 it returns the key value (ie nid, or tid) of the field. This may be confusing for some users used to using views php in D6. To delve in further it may help to test print_r($data); to see what values are generated. As noted in dalearyous's tutorial $data->field_your-field-goes-here['0']['raw']['value']; works pretty well. So if you have a field named field_account_name $data->field_field_account_name will return the value you expect.

Page status

About this page

Drupal version
Drupal 7.x
Audience
Programmers, Site administrators, Site builders
Keywords
php

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here