My use case:
- A host content type, named "shopping_cart," uses field_collection to have a variable number of items (named "product"), with each item entity containing (among others) a numeric price field.
- The shopping_cart content type also has a computed numeric field to contain the total price, i.e. the sum of the price fields of each product item.

It looks like the computed_field module is only given access to the numerical entity ID of each "product" field_collection item, and I have to use the entity_load() and then node_load() functions in the code for the total_price field to access the products' embedded price fields:

$temp_entity_ids = array();
$total_price = 0;
foreach ($entity->field_items[LANGUAGE_NONE] as $temp_id => $temp_item) {
  $temp_entity_ids[] = $temp_item['value'];
}
$temp_entities = entity_load('field_collection_item', $temp_entity_ids);
foreach ( $temp_entities as $temp_ent_id => $temp_ent) {
 $product_nid = $temp_ent->field_product[LANGUAGE_NONE][0]['nid'];
 if ($product_node = node_load($product_nid)) {
   $total_price += $product_node->field_price[LANGUAGE_NONE][0]['value'];
 }
}
$entity_field[0]['value'] = '$'.$total_price;

Is there a better way to do computed fields of values embedded within field_collection items?

Also, the value of the total_price field is only updated when the shopping_cart node it edited. If I change the price value in an instance of a product node within the cart's field_collection, the cart's total_price is not updated. Suggestions for an elegant way to automatically update the total_price field, besides writing my own module that implements hook_entity_update() to update the shopping_cart node appropriately?

(P.S. I'm not trying to mimic the Ubercart module. I posted here since computed_field + field_collection offered a nice, simple solution for a very basic shopping cart app.)

Comments

fago’s picture

Status: Active » Fixed

Basically, the way you do it looks fine, but you could use the entity-metadata-wrappers to easily access the entities.

>If I change the price value in an instance of a product node within the cart's field_collection, the cart's total_price is not updated.

in dev version the host-entity is updated each time an item is updated, what should solve this issue for you,.

Status: Fixed » Closed (fixed)

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

Yuri’s picture

@fago: can you give an example of the use case above, using entity-metadata-wrappers ?
Thanks

h.arefmanesh’s picture

check field_collection_views module.
i use that for my project and seems great!
this module provide a method to edit or delete every collection item even add another item in display page of your host entity (shopping_cart) that can trigger computed field of shopping_cart content type.

my question :
is any way to access field of host entity in computed field in field collection ?
i have a cck field in my host entity that user fill that, and i want to use value of that field in a computed field that exist in field collection items.

5t4rdu5t’s picture

This example is working for me:

<?php

$sale_wrapper = entity_metadata_wrapper('node', $sale_node);

foreach ($sale_wrapper->field_saleitems->value() as $key => $saleitem) {
  $product = node_load($saleitem->field_product[LANGUAGE_NONE][0]['target_id']);
  ...
}
?>

In my case field_saleitems is the field collection and field_product is an Entity Reference field inside the collection that relates the sale item to a product node.

liquidcms’s picture

Status: Closed (fixed) » Active

maybe a silly question but since this makes it so much more painful to use a collection; why would the collection module not just add its fields to the entity object when they are loaded? so that all the code that is going to be written to access this info can simply do a node load or entity load like we have always done?

jmuzz’s picture

Status: Active » Fixed
Issue tags: -computed field

It's the normal way to handle fields that reference another object. Look at other examples like Taxonomy and Entity Reference and you will see that node_load only puts the ID's of the referenced entities in the field.

Status: Fixed » Closed (fixed)

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

Karan Sen’s picture

To load the field collection items in node please see the module Field Collection load. The link to the module is https://www.drupal.org/project/field_collection_load.