I have a field collection on a custom content type and I'd like to add up the values of all the instances of one field within the field collection. The normal way to reference fields doesn't seem to work when they're in a collection.

For instance, say I had a "widget" field collection (field_widget) with two fields - widget name (field_wname) and widget quantity (field_wquantity).

How would I reference the widget quantity field in my computed field php in order to add up all the quantities of widgets to get a total number of all types of widgets?

Comments

Expiring same problem, subscribe to this issue.

Since a computed field uses php just like a theme, I believe you should be able to use entity_load() or an entity API wrapper.

Having the same problem and am stuck here.
Trying to use values from field_collection fields in presave. How can I access them?

Can you give an example?

ok, got it

Here I have a custom content type ($node) with a field collection "field_field_collection_item".
The field collection has two fields: "foo" and "bar".
To keep it easy, foo and bar don't have multiple entries.

<?php
       
/*
         *  Get field_collection_item ids saved in the "field_field_collection_item" field of custom content type ($node).
         */
       
$field_collection_items = field_get_items('node', $node, 'field_field_collection_item');
       
$field_collection_item_ids = array();
       
$i = 0;
        foreach (
$field_collection_items as $field_collection_item){
           
$field_collection_item_ids[$i] = $field_collection_item['value'];
           
$i++;
        }
       
/*
         *  load the content of field_collection_items fields "foo" and "bar"
         */
       
$field_collection_item_fields = entity_load('field_collection_item', $field_collection_item_ids);
       
$foo_fields = array();
       
$bar_fields = array();
       
$i = 0;
        foreach (
$field_collection_item_fields as $field_collection_item_field){
           
$foo_fields[$i] = field_get_items('field_collection_item', $field_collection_item_field, 'field_foo');
           
$bar_fields[$i] = field_get_items('field_collection_item', $field_collection_item_field, 'field_bar');
           
$i++;
        }
       
/*
         *  getting the the values for each foo and bar (delta = 0)
         */
       
$foos= array();
       
$i = 0;
        foreach (
$foo_fields as $foo_field){
           
$foos[$i] = $foo_field[0]['value'];
           
$i++;
        }
       
$bars= array();
       
$i = 0;
        foreach (
$bar_fields as $bar_field){
           
$bars[$i] = $bar_field[0]['value'];
           
$i++;
        }
?>

I am just wondering if other people than the #4 poster succeeded in getting a working 'total' field for the field collection's price field items. I tried the code in $4 but the calculated field shows me nothing.

Also, maybe there are some other D7 functions that make the the #4 code shorter.

This also works for fields with single values:

$wrapper = entity_metadata_wrapper($entity_type, $entity);
$foo = $wrapper->field_collection->field_item_foo->value();

How to reference field collection fields in a computed field without any code. Please help

for multiple value

<?php
$wrapper
= entity_metadata_wrapper($entity_type, $entity);
foreach(
$wrapper->field_collections as $collection)
  
$sum += $collection->field_integer->value();
?>

#8 works great, nice and short code and exactly the code snippet I was looking for. Only problem is that it throws and error for every field I use it on.

Notice: Undefined variable: sum in eval() (line 4 of /Applications/MAMP/htdocs/sitename/sites/all/modules/computed_field/computed_field.module(466) : eval()'d code).

This worked for me

<?php
    $wrapper
= entity_metadata_wrapper($entity_type, $entity);
    foreach(
$wrapper->YOUR_FC_COLLECTION as $collection) {
           
$time = $collection->YOUR_FC_FIELD->value();
           
$sum += hoursToMinutes($time);
    }
   
$entity_field[0]['value'] =  minutesToHours(($sum));
?>

I solved the error by using
i Thought i solved this error by using:

<?php
$sum
+= $collection->YOUR_FC_FIELD->['value'];
?>

instead of
<?php
$sum
+= $collection->YOUR_FC_FIELD->value();
?>

The complete code now looks like this

<?php
$wrapper
= entity_metadata_wrapper($entity_type, $entity);
foreach(
$wrapper->YOUR_FC_COLLECTION as $collection) {
  
$sum += $collection->YOUR_FC_FIELD->['value'];
}
?>

But it does not:)

<?php
// have you forgot to declare this var first?
$sum = 0;
$wrapper = entity_metadata_wrapper($entity_type, $entity);
foreach(
$wrapper->YOUR_FC_COLLECTION as $collection) {
  
$sum += $collection->YOUR_FC_FIELD->value();
}
// see the result
dsm( $sum );
?>

How could I pull just the first value in a multiple value field?

My case is the user can input multiple pricings and I just need to pull out the first one they use as an example.

Issue summary:View changes
Status:Active» Fixed

@W0LF you should be able to use the array dereference operator. So in the above example replace the loop with $collection = $wrapper->YOUR_FC_COLLECTION[0] or the inner part with sum += $collection->YOUR_FC_FIELD[0]->value();

More details about how to use metadata wrappers can be found here:

https://drupal.org/node/1021556

It looks like people have found their solutions. I will set it to fixed.

Status:Fixed» Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.