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. 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';
}

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

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);
}

Drupal 8

Here's how you get the value of a field in a referenced entity:

$value = $entity->field_a->entity->field_b->value;

Simple example for a star formatter in Drupal 8

<?php

/**
 * @file
 * Contains \Drupal\Core\Field\Plugin\Field\FieldFormatter\PD5CustomFormatter.
 */

namespace Drupal\pd5_custom\Plugin\Field\FieldFormatter;

use Drupal\computed_field\Plugin\Field\FieldFormatter\ComputedPhpFormatterBase;
use Drupal\Core\Field\FieldItemInterface;

/**
 * Plugin implementation of the 'PD5_Custom' formatter for computed fields Gesamtbewertung.
 *
 * @FieldFormatter(
 *   id = "PD5_Custom",
 *   label = @Translation("PD5 Formatter Gesamtbewertung"),
 *   field_types = {
 *     "computed_integer",
 *     "computed_decimal",
 *     "computed_float",
 *   }
 * )
 */
class PD5CustomFormatter extends ComputedPhpFormatterBase {

  /**
   * Do something with the value before displaying it.
   *
   * @param mixed $value
   *   The (computed) value of the item.
   * @param \Drupal\Core\Field\FieldItemInterface $item
   *   The item to format for output
   * @param int $delta
   *   The delta value (index) of the item in case of multiple items
   * @param string $langcode
   *   The language code
   * @return mixed
   */
  public function formatItem($value, FieldItemInterface $item, $delta = 0, $langcode = NULL) {

    $rounded = round($value, 0);
    $markup = '';

    switch ($rounded) {
      case 0:
        $markup = "<div class='starrating'>";
        $markup .= "<span class='rate-value'>$rounded</span>";
        $markup .= "<span class='rate-image star-off odd s1'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off even s2'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off odd s3'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off even s4'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off odd s5'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off even s6'><i class='fas fa-star'></i></span>";
        $markup .= "</div>";
        break;

      case 1:
        $markup = "<div class='starrating'>";
        $markup .= "<span class='rate-value'>$rounded</span>";
        $markup .= "<span class='rate-image star-on odd s1'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off even s2'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off odd s3'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off even s4'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off odd s5'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off even s6'><i class='fas fa-star'></i></span>";
        $markup .= "</div>";
        break;
      case 2:
        $markup = "<div class='starrating'>";
        $markup .= "<span class='rate-value'>$rounded</span>";
        $markup .= "<span class='rate-image star-on odd s1'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on even s2'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off odd s3'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off even s4'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off odd s5'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off even s6'><i class='fas fa-star'></i></span>";
        $markup .= "</div>";
        break;
      case 3:
        $markup = "<div class='starrating'>";
        $markup .= "<span class='rate-value'>$rounded</span>";
        $markup .= "<span class='rate-image star-on odd s1'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on even s2'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on odd s3'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off even s4'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off odd s5'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off even s6'><i class='fas fa-star'></i></span>";
        $markup .= "</div>";
        break;
      case 4:
        $markup = "<div class='starrating'>";
        $markup .= "<span class='rate-value'>$rounded</span>";
        $markup .= "<span class='rate-image star-on odd s1'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on even s2'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on odd s3'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on even s4'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off odd s5'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off even s6'><i class='fas fa-star'></i></span>";
        $markup .= "</div>";
        break;
      case 5:
        $markup = "<div class='starrating'>";
        $markup .= "<span class='rate-value'>$rounded</span>";
        $markup .= "<span class='rate-image star-on odd s1'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on even s2'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on odd s3'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on even s4'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on odd s5'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-off even s6'><i class='fas fa-star'></i></span>";
        $markup .= "</div>";
        break;
      case 6:
        $markup = "<div class='starrating'>";
        $markup .= "<span class='rate-value'>$rounded</span>";
        $markup .= "<span class='rate-image star-on odd s1'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on even s2'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on odd s3'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on even s4'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on odd s5'><i class='fas fa-star'></i></span>";
        $markup .= "<span class='rate-image star-on even s6'><i class='fas fa-star'></i></span>";
        $markup .= "</div>";
        break;
    }

    return $markup;

  }



}

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:

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.