Use Computed Field module to fetch Field Collection values Primary tabs

By xngo on February 26, 2019

If you have a field collection(e.g. field_transaction) that have quantity field (e.g field_stock_quantity) and you want to sum all the quantity up. Here is code to do it in Computed Field module:

$field_items = field_get_items($entity_type, $entity, 'field_transaction');
$total = 0;
foreach ($field_items as $item)
  // Note: I don't know why 'und' is within the field collection.
  //  Is there a better way to get the value of field_stock_quantity?
  $total += $item['field_stock_quantity']['und'][0]['value'];
$entity_field[0]['value'] = $total;

About the author

Xuan Ngo is the founder of He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.