Drupal - How to use Computed_field as a product of an existing one?
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:
- the (amazing!) Community documentation about the Math Field module.
- the interesting article about Introducing the Math Field module, which also includes some screenprints to see it at work (such as the the add/edit form example).
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:
- The cTools math expression library causes an error when using function that take more than one parameter. This effects pow(), min(), max() (#1958538: Improve math expression engine).
- Math Field does not yet support multivalue options fields (checkboxes, or multiselect) (#2483453: Add aggregate functions for multivalue fields).
- Math Field does not yet support Field Collections (#2573643: Support the Field Collection module).
Bonus: you would not need the "PHP filter" (IMO only that should be a valid reason to investigate this alternative) ...
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