Last updated June 10, 2014. Created by criznach on March 10, 2007.
Edited by Kartagis, a_thakur, colan, sean_msu. Log in to edit this page.

Computed Field is a module that lets you add a computed field to custom content types. You can choose whether to store your computed field in the database, or simply have it calculated upon view. You can also choose whether to display the field, and how to format it. The value of the field is set using PHP code so it can draw on anything available to Drupal, including other fields, the current user, database tables, etc. The drawback of this is of course that you must be PHP-savvy to use it.

Before you can use Computed Field for versions prior to Drupal 7, you'll need to get CCK and enable (at the very least) the 'content' module. You will probably also want to enable the other cck modules, such as 'text', 'number', 'date', etc.

Before you can use Computed Field for Drupal 7+, you'll need to enable the Field module. You will probably also want to enable the 'Field UI' module and other field
modules, such as 'Text','Number', etc.

To add a computed field to a content type, go to Administer > Content > Content types (pre-D7) or Adminstration > Structure > Content types (D7), select the content type you want to add to, and click on the 'Add field' tab (pre-D7) or fill in the "Add new field" line (D7). One of the field types available should be 'Computed', and it should have a single widget type available to it, labelled 'Computed'. If you select this, give your field human-readable and machine-readable names, and submit the form, you will get to the configuration page for your new computed field.

For details on configuration, see the Configuring Computed Field page.

When is the field actually computed?

If you wish to store the values in the database then you have to update or re-submit the node to compute the value of the field.

If it is not stored in the database then the value computes when the node loads and only when the node is loaded. The field will not work in views nor will it function properly if it depends on other non-stored computed fields.

Examples

Drupal 6

See the README and sub-pages for examples.

Drupal 7

As described above, 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 sum of the number fields field_a and field_b in a node entity:

<?php
$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). field_get_items() saves you from having to deal with languages; you'll always get the current one, even if language-neutral.

Note that when setting values for $entity_field, you should not set specific language codes. These have no place here, and are not necessary for the field to function properly.

To see details of the arguments provided to you, enable the Devel module and use dd() or a similar function to output the full contents of each argument.

Drupal 7 example usage:

How to write the computed field override function

Drupal 6

Alternately, the code can be supplied by your own custom function named computed_field_FIELD-NAME_compute($node, $field, &$node_field). Note the ampersand in the &$node_field argument - it must be passed by reference to ensure changes are kept. Here's an example of how to do this:

<?php
function computed_field_FIELD-NAME_compute($node, $field, &$node_field){
   
// Change the value of the field
   
$node_field[0]['value'] = 'foo';
}
?>

Drupal 7

To use a function in a custom module instead of writing the code in text area create a function that looks like:

<?php
>
function
computed_field_YOUR_FIELD_MACHINE_NAME_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items) {
 
$entity_field[0]['value'] = 'value';
}
?>

Parameters:
  • &$entity_field - The computed field. Used to store the computed value.
  • $entity_type - The entity type: node, user, comment, etc.
  • $entity - The actual entity (a node, user, comment, etc.)
  • $field - General field settings.
  • $instance - Field instance settings.
  • $items - The list of items.

Note:
Make sure $entity_field is passed by reference.

How to write the computed field display function

Drupal 6

To be filled in.

Drupal 7

Here is a clear example. Note the necessary arguments:

<?php
function computed_field_YOUR_FIELD_MACHINE_NAME_display($field, $entity_field_item, $entity_lang, $langcode) {
  return
'$' . number_format($entity_field_item['value'], 2);
}
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

agungsuyono’s picture

In order computed field override function to work, make sure $entity (and ALSO $entity_field) is passed by reference. Here is example of my code:

<?php
function computed_field_field_mhs_nim_convertion_compute(&$entity_field, $entity_type, &$entity, $field, $instance, $langcode, $items) {
 
$entity[0]['value'] = 'test test';
}
?>
colan’s picture

Just &$entity_field should be sufficient.

Leksat’s picture

Overriding display output example (D7):

<?php
function computed_field_{field_name}_display($field, $entity_field_item, $entity_lang, $langcode) {
  return
'$' . number_format($entity_field_item['value'], 2);
}
?>
colan’s picture

Used it above.

cperg’s picture

We are needing to place a button (or a link) on the view page of a node. We believe it should be possible to do so using a computed field. We would then like to have the pressing of the button to change a value shown on the view screen. I am sure this would require a page refresh but that point is moot.

Does anyone have a snippet that would produce a button in the display area of a computed field?

Chris Pergantis
Founder/Chairman/President
Accelerated Design Inc

mori’s picture

I guess it would be very helpfull to split the snippet-section into an area for D6 & D7 for a better usability.