Computing a field from a view
Last updated on
30 April 2025
You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules
Your mileage may vary, but here is some code which calculates a field based on a view.
This code may not do what you want— if the view changes (e.g. another node is added), this field would not be re-evaluated till the next time the node is saved.
One (ugly) solution is to put similar code into the header or footer of a view.
This is a coding alternative to the views_calc module, which allows a great deal of flexibility, but is perhaps not ideal.
<?php
$view = views_get_view('YOURVIEWNAME');
$info=views_build_view('items', $view, array($node->nid), false, false);
$total=0;
//drupal_set_message(serialize($info));
foreach ($info['items'] as $item) {
$total+=$item->YOUR_FIELD_USE_THE_DEBUG_MESSAGE_ABOVE_TO_FIND_IT;
}
$node_field[0]['value']=$total;
?>
Help improve this page
Page status: Not set
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion