Advertising sustains the DA. Ads are hidden for members. Join today

Computed Field

Computed Fields: Adding numeric values stored in two separate fields (basic info).

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

Note: the Computed Field Module can be used instead of this custom code (in Drupal 5.x and 6.x)

Imagine you have two existing number fields, called field_product_price and field_postage_price. You want to create a computed field field_total_cost which adds these two fields. Create a new computed field with the name 'Total Cost', and in your computed field's configuration set the following:

  • Computed Code:
    $node_field[0]['value'] = $node->field_product_price[0]['value'] + $node->field_postage_price[0]['value'];
    • If you are using the E-commerce module the price value is available as $node->price
    • If you are using the UBERCART module the public price value is available as $node->sell_price
  • Check 'Display this field'
  • Display Format:
    $display = '' . $node_field_item['value'];
  • Check 'Store using the database settings below'
  • Data Type: float
  • Data Length: 10,2
  • Default Value: 0.00
  • Check 'Not NULL'
  • Check 'Sortable'

While the example above uses a price and postage which both can be handled using ecommerce modules or Ubercart, there are other examples when you would want to compute two fields and the above instructions/example provide guidance.

Help improve this page

Page status: Not set

You can: