Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
MurzExpiring same problem, subscribe to this issue.
Comment #2
RobW CreditAttribution: RobW commentedSince a computed field uses php just like a theme, I believe you should be able to use entity_load() or an entity API wrapper.
Comment #3
Thomas_M CreditAttribution: Thomas_M commentedHaving 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?
Comment #4
Thomas_M CreditAttribution: Thomas_M commentedok, 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.
Comment #5
Yuri CreditAttribution: Yuri commentedI 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.
Comment #6
stevebab CreditAttribution: stevebab commentedThis also works for fields with single values:
Comment #7
tvphong CreditAttribution: tvphong commentedHow to reference field collection fields in a computed field without any code. Please help
Comment #8
dmousefor multiple value
Comment #9
danon1981 CreditAttribution: danon1981 commented#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).
Comment #10
Jonny_Orange CreditAttribution: Jonny_Orange commentedThis worked for me
Comment #11
danon1981 CreditAttribution: danon1981 commentedI solved the error by usingi Thought i solved this error by using:
instead of
The complete code now looks like this
But it does not:)
Comment #12
dannygoh CreditAttribution: dannygoh commentedComment #13
W01F CreditAttribution: W01F commentedHow 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.
Comment #14
jmuzz CreditAttribution: jmuzz commented@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.
Comment #16
kaizerking CreditAttribution: kaizerking commentedI have profile2 profile,
which has the filed collection field field_resume_xp, unlimted values,this field collection field has a field called field_duration this value is populated with computed field
This field collection aalso jas a term reference field with two terms term 1 and term 2
Now I have 2 fields in profile2 profile "field_total_duration_term1" and "field_total_duration_term2" I want the sum of all the "field_duration" values for term 1 in "field_total_duration_term1" field and sum of all the "field_duration" values for term 2 in "field_total_duration_term2" field
Simplyfy I want sum of term1 durations and sum of term2 durations separately computed and stored
how to get this?
Comment #17
kaizerking CreditAttribution: kaizerking commentedPlease ignore this it was a cache problem and hast to edit this post to update
Comment #18
daniel-san CreditAttribution: daniel-san commented@dannygoh your code in #12 was incredibly helpful and it helped me solve this issue on my site. Thank you for adding that to the conversation.
Comment #19
sachav CreditAttribution: sachav commentedI have also been using the solution of posts #8/10 successfully for one "field collection".
Having created a second one with it's own computed field, although having added values for the FC when saving the user, it seems like it is looping an empty array.
I have two requirements on users:
Both can have an unlimited number of instances, and are similar in setup. The first is a " Field Collection " (FC) with a " string list " and a " date ". The second is an FC with a " string list " and two other " text fields ".
I am making status fields for both fields using a computed field and entity wrapper.
$wrapper = entity_metadata_wrapper($entity_type, $entity);
foreach($wrapper->field_membership_type_periodical as $period) {
...
This one is working as expected and it loops over every "field_membership_type_periodical" FC added to the user, and I can use all other properties of this FC as expected.
When I do the same for the "field_parents_guardian" FC, although I have added items to the list, it doesn't iterate over it and if I dump the "field_parents_guardian->value() ", it says it is an empty array.
If I dump the whole wrapper object, I find it strange that the "field_membership_type_periodical" is dumped as:
While the guardian field is dumped as:
So the first one it seems, is merely a reference to the FC item, which it lazy loaded, while the second one seems to be having no reference information, but the object itself already loaded.
Both FC's have been setup in the same way and I can't find any major differences in their settings. What might cause this strange behaviour in structure and could it be the source of the "empty list issue"?
Comment #20
ebenfarnworth CreditAttribution: ebenfarnworth commentedUsing @dannygoh's code in #12 I now get a green status message at the top of the saved content showing the sum of the fields I'm calculating with computed fields module (the value shown in the message is correct. However the computed field its self is always empty. I have the display for the computed field set to 'raw value, no display code'.
My computed field php looks like this:
My field collection is called: field_route_book
The field in the field collection is called: field_points
What am I doing wrong?
I have also tried setting the display for the computed field to 'plain text' and the display code as default:
$display_output = $entity_field_item['value'];
and then I tried this:
$display_output = $entity_metadata_wrapper_item['value'];
Still I get the sum showing up in the status message but the computed field its self stays empty.
Comment #21
ebenfarnworth CreditAttribution: ebenfarnworth commentedOK, I worked it out!
dsm( $sum );
sets the out put to be a status message. I changed it to:$entity_field[0]['value'] = ($sum);
Now the field works correctly and the full code in my case looks like this:
Comment #22
Yuri CreditAttribution: Yuri commentedWell done @ebenfarnworth, Thanks!