Calculate Field Values Using Computed Field in Drupal 7

Background

I usually don’t install too many modules as it slows down my site, but I’d like to recommend computed_field.module. There are some other similar modules but this is the only good one.

I’m running a contest event aggregator site using Drupal 7, and this post is a simple guide to show how to use computed field. Here’s my content structure, content type and pieces of fields I need and their machine names.

Content type: “Contest” (contest)

Field: “First Prize” (first_prize)

Field: “Second Prize” (second_prize)

Field: “Third Prize” (third_prize)

Propose

In content view (node view) I’d like to show the total prizes from the first to the third.

Solution

I add a computed field – Field: “Total Prize” (total_prize)

Then enter this page.

I only code in files as it’s easier to maintein (and I suggest you to do so), so I copied the function name from the guide. It’s

computed_field_FIELD_NAME_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items)

and for the display, it’s

computed_field_FIELD_NAME_display($field, $entity_field_item, $entity_lang, $langcode)

Then open an editor. I usually put all the tweaks and custom features about a content type into the same custom module, and prefix with the project name. In this case, project name is “Bounty Hunter”, so it’s “bh”, the name of the module would be “bh_contest.module”.

In the module, here’s the snippet. (And I keep the help text from the module) (remember to add “dependencies[] = computed_field” in your module’s .info)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 /** * Computed field : field_contest_total_prize * * The variables available to your code include: * &$entity_field, $entity_type, $entity, $field, $instance, $langcode, and $items. * To set the value of the field, set $entity_field[0]['value']. * For multi-value computed fields continue with $entity_field[1]['value']. * Here's a simple example which sets the computed field's value to the value of the sum of the number * $entity_field[0]['value'] = * array_pop(array_pop(field_get_items($entity_type, $entity, 'field_a'))) + * array_pop(array_pop(field_get_items($entity_type, $entity, 'field_b'))); * The first pop fetches the last (or only) item from the field while the second pop fetches its * ['value'] contents (assuming it's the only key that's set). */function computed_field_field_total_prize_compute(&$entity_field,$entity_type,$entity,$field,$instance,$langcode,$items){$entity_field[0]['value']=$entity->field_first_prize_cash['und'][0]['value']+$entity->field_second_prize_cash['und'][0]['value']+$entity->field_third_prize_cash['und'][0]['value'];}
##### Another Method: hook_node_presave

It’d be even better to use hook_node_presave if your purpose is this simple. Here’s the example code. “FIELD_COMPUTED” would be the new field to generate values. And also you can use hook_form alter to hide the form in edit mode.

1 2 3 4 5 6 7 function MODULE_NAME_node_presave($node){if($node->type==='NODE_TYPE'){$node->FIELD_COMPUTED['und'][0]['value']=$node->FIELD_COMPUTED_A['und'][0]['value']+$node->FIELD_COMPUTED_B['und'][0]['value'];}}
And that’s it! Here’s the link to the module and some other snippets. [http://drupal.org/project/computed_field](http://drupal.org/project/computed_field) [http://drupal.org/node/149228](http://drupal.org/node/149228)

But computed field only generate values on node save, if you already have contents, use this to re-compute all the entities.

http://drupal.org/project/computed_field_tools