Advertising sustains the DA. Ads are hidden for members. Join today

Value code

Last updated on
11 December 2018

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

Under an arbitrary Configure field: Global: PHP Views field, you will find a Value code text-area section for setting your own code wherein you can construct the value for this field. The returned value for this field will then be injected in the $value variable for eventual referencing, specifically for the Output code section.

Underneath this text-area exists a collapsible field list of AVAILABLE VARIABLES. Among the entries in this list of variables, the $data variable is probably the variable that can be utilized in most use-cases.

In the context of a row's worth of information where one can draw reference values, you can actually enumerate all row data by simply putting the following PHP code in the Value code:

return $data;

And then putting the following PHP code in the Output code section:

<?php print_r ($data); ?>

The $data variable represents a stdClass Object which contains an array of drawable information where you can build your Value code upon. Note that the $data variable may contain a massive mixed array of data possibly containing nested arrays and stdClass Objects.

Keep in mind that you will need to have at least one return statement in your Value code for this to be injected in the $value variable. While users can actually skip the Value code in some use cases, the benefit of using the Value code lies in its ability to be referenced elsewhere, e.g., from another field or table column.

Example 1: View of Users.

Objective: Display a hash value of a concatenated string of a user field internally referenced as $data->field_og_user_node[0]['raw']['entity']->title and the user email as $row->mail.

Put the following text in the Value code section:

$profile_data1 = "";
if (isset($data->field_og_user_node[0]['raw']['entity']->title)) $profile_data1 = $data->field_og_user_node[0]['raw']['entity']->title;
return hash('md5', $profile_data1 . $row->mail, false);

Then put the following text in the Output code section.

<?php echo $value; ?>

Help improve this page

Page status: No known problems

You can: