I would like to have a field (field_result) that would display a number that is the result of the product of an existing field (field_1) on the same content type (let's call it book) and a fixed number (let's say 3.5).

My idea was to use the Computed Field module in order to display the result. After reading its Community documentation I managed to do the following:

That's what I achieved so far:

  1. I configured field_result to accept only one value and save it on the database.
  2. I set its value as follows:

    $field_1 = field_get_items($entity_type, $entity, 'field_1'); $entity_field[0]['value'] = $field_1[0]['value'] * 3.5;

  3. I set its $display_output as follows: $display_output = $entity_field[0]['value']

Unfortunately when I edit the node and save it I get the following notice:

Notice: Undefined index: value in eval()

and the field displays 0.00 as a result.

What am I doing wrong?

up vote 6 down vote accepted

A NOT commonly known/used solution that might help to prevent your problem from happening, is to use the (fairly new) Math Field module (for D7), instead of the Computed Field module (so change your mind about which module to use). 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:

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:

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

  • Thank you! I didn't know this module and looks exactly what I was looking for. Unfortunately I didn't succeed on making it work, as I wrote [field_1:0:value] * 3 on its content (and made sure field_1 exists and has a value) but I do not get any result. I also tried with 2 + 2 with no results. Should I open a new question? – ccamara Feb 5 '16 at 17:07
  • I restarted from scratch and worked fine, thanks – ccamara Feb 8 '16 at 6:18
  • Great to hear! Have fun using the "math field" module whenever you can (instead of "computed field" module which requires the PHP filter ...) – Pierre.Vriens Feb 8 '16 at 6:24

You are missing the function

example:

function computed_field_field_profit_compute(&$entity_field, $entity_type, $entity,
$field, $instance, $langcode, $items) {
 $entity_field[0]['value'] = $entity->field_price[LANGUAGE_NONE][0]['value'] - $entity->field_cost[LANGUAGE_NONE][0]['value'];
}

The function name pattern that needs to be used is computed_field_{field_name}_compute.

For more info, I suggest you read Working with the Drupal Computed Field Module

But... I highly suggest you look into the Math field module as suggested in the other answer, any time you can avoid enabling PHP Filter you should take it. Have a look at "What are the downsides of using 'custom' PHP code in blocks, nodes, views-args, etc?" to understand why

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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