Last updated 4 June 2015. Created on 10 March 2007.
Edited by colan, lmeurs, tyler.frankenstein, Kartagis. 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.


Drupal 6

See the README and sub-pages for examples. Also see #858970: custom function hook help is incomplete for an important note about using a .module file to compute the value(s) for a field.

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:

  // Simply return the sum of the two other fields.
  $field_a = field_get_items($entity_type, $entity, 'field_a');
  $field_b = field_get_items($entity_type, $entity, 'field_b');
  $entity_field[0]['value'] = $field_a[0]['value'] + $field_b[0]['value'];

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:

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:

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


  • &$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.

Make sure $entity_field is passed by reference.

How to write the computed field display function

Drupal 6

function computed_field_YOUR_FIELD_MACHINE_NAME_display($field, $element) {
  $markup = isset($element['#item']['value']) ? $element['#item']['value'] : '';
  return $markup;

Drupal 7

Here is a clear example. Note the necessary arguments:

function computed_field_YOUR_FIELD_MACHINE_NAME_display($field, $entity_field_item, $entity_lang, $langcode, $entity) {
  return '$' . number_format($entity_field_item['value'], 2);

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


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:

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):

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.

Chris Pergantis’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
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.

nithinkolekar’s picture

Will computed field code triggers when node is deleted? just like while it triggers on node creation and editing.
Searched on issue queue but haven't found any thing and thought this might be the right place.

Nithin Kolekar

pthornhi6’s picture

@nithinkolekar.. I am running into that problem right now. I have yet to verify what it is doing, but my Computed Field code, which works for insert and update events, is preventing me from deleting by causing a server error. This leads me to believe it is trying to compute.


nithinkolekar’s picture

though not tested when importing nodes using feeds.. what is the out of the box behavior of this module? should it process while importing or ignore it?
perhaps options could be given per field basis like when it should (not) process like node creation, update , feed import etc.