4

Could anybody figure out to me how i could create a field that computes the sum of values in 2 other fields.

In other words how to do a simple addition (say of price and vat) like so:

price
+
vat
======
result

The result should be displayd after the editor saved the node.

4 Answers 4

0

Another answer to this question (without accepted answer ...), is to use the (fairly new) Math Field module (for D7), instead of the Computed Field module (as suggested in all 3 previous answers ...).

Some more details about this module (from its project page):

The Math Field module defines a new field type to dynamically calculate values on entity forms. This is an alternative to Computed Field that does not require the use of PHP Filter.

Math expression fields are dynamically updated via ajax on the entity form as soon as all necessary fields are populated. If JavaScript is disabled, the field will be evaluated when the form is submitted. The result is stored in the database when the form is submitted.

For more details (step-by-step instructions), refer to either of these links:

Bonus: you would not need the "PHP filter" (IMO only that should be a valid reason to investigate this alternative) ...

Known issues

As this is a fairly new D7 module, there are still a few (known) issues with it, as mentioned also on its project page:

4

Computed Field is a good solution. You can achieve a similar results without an additional module (provided you already have a custom module).

If you need to store the result when the node is saved, you can implement hook_node_presave() to set the value of the third fields just before the node data is saved to the database.

MODULE_node_presave($node) {
  $value = 0;
  $price_items = field_get_items('node', $node, 'field_price');
  if ($price_items) {
    $value += $price_items[0]['value'];
  }
  $vat_items = field_get_tems('node', $node, 'field_vat');
  if ($vat_items) {
    $value += $vat_items[0]['value'];
  }
  $node->field_value[LANGUAGE_NONE] = array(array('value' => $value));
}

Because the value is always calculated and simple, there is really no need to store it in the database. If your are already using the Entity API module and its entity metadata wrappers, this is easily doable with a hook_entity_property_info() implementation.

2
  • Great! It looks pretty good solution. I think there needs to add condition like, if($node->type =='my_content_type')in node_presave function. Am I right? Apr 24, 2014 at 5:01
  • 1
    Indeed, you need to add a condition to avoid adding the $node->field_value[LANGUAGE_NONE] to any type of node. Apr 24, 2014 at 13:07
3

You can use Computed Field module to achieve this.

About Computed Field

Computed Field is a very powerful field module that lets you add a custom "computed fields" to your content types. These computed fields are populated with values that you define via PHP code. You may draw on anything available to Drupal, including other fields, the current user, database tables, you name it. (Feeling the power yet? :) ) You can also choose whether to store your computed field values in the database with other content fields, or have them "calculated" on the fly during node views. (Although you should note that Views use requires database stored values.) This field is literally the Swiss Army knife of fields. So start cooking up your PHP based values!

How to use:

Here are the steps to achieve your case.

Add computed filed type (say result) to your content type to compute price and Vat filed values.

Here are the settings for result field (to compute price and vat)

In the Computed Code (PHP) textarea, paste the following code.

(Assuming that price and vat are machine names for price and vat fields)

$entity_field[0]['value'] = $entity-> field_price['und'][0]['value'] + $entity->field_vat['und'][0]['value'];

In your case, You may not have to change the code in Display code (PHP) textarea

$display_output = $entity_field_item['value'];
1

you can use http://drupal.org/project/computed_field. Computed fields are populated with PHP code. You may choose to store your computed field in the database , or have them "calculated" on the fly during node views.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.