Score:0

computed field inside field collection

de flag

I have two fields (field_a and field_b) which are inside the field_available_water_s field collection. I want to set the field_c computed field, which is also inside the field collection, to the sum of those fields.

I tried using this code, but it doesn't work.

$entity_field[0]['value'] = $entity->field_a[LANGUAGE_NONE][0]['value'] + $entity->field_b[LANGUAGE_NONE][0]['value'];

What code should I use?

Score:0
us flag

In the Computed Code (PHP) textarea shown in the field settings of the computed field, you need to enter the following code.

$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"];

That is the code the description for the Computed Code (PHP) textarea uses as example. (Emphasis is mine.)

The variables available to your code include: &$entity_field, $entity_type, $entity, $field, $instance, $langcode, and $items. 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 value of the sum of the number fields (field_a and field_b) in a node entity:

$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"];

screenshot

I added the fields described in the question to a node. With this code, I get the following output.

screenshot

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.