2

I'm confused by the field_collection API. I created a Field Collection and attached it to a node (in the GUI) but I can't figure out how to code an update/addition to the field collection. This is my scenario:

I have a node (request) that has product information and quantities. When users go to a product page they see a link to "Add to Request". This link should add the product (nid) to the node request. The difficulty lies in the fact that I need quantity information as well, hence the Field Collection. I added a quantities field and grouped it with the product field.

Node Type: request
Request ID: $requestid
Product ID: $productid
Field Collection = field_request_collection (Unlimited values)
Field Collection Field = field_products (Entity Reference, Max 1)
Field Collection Field = field_product_quantities (Integer, Max 1, Default 1)

My function looks like this (the values for $requestid and $productid are passed in the URL of the link: /my_request/5/12):

function _my_module_request_update($requestid,$productid) {
$nload = node_load($requestid);
$field_collection_item_value = $nload->field_request_collection[LANGUAGE_NONE][0]['value'];
$field_collection_item = entity_load('field_collection_item', array($field_collection_item_value));
$field_collection_item->field_products[LANGUAGE_NONE][0]['value'] = intval($productid);
$field_collection_item->save();
}

The error I'm getting with the above code is:

PHP Fatal error: Call to a member function save() on a non-object

Keep in mind that I'm attempting to ADD Field Collection values NOT replace them. Please help, thanks!

  • Looks like your entity_load() is failing. What do you get for the $field_collection_item object when you inspect it? – Clive Jun 7 '13 at 14:49
  • I'm trying to the pull the field collection entity so I can set the value of a field in that collection. – Ken J Jun 7 '13 at 16:14
  • This works fine to load all field_collections: entity_load('field_collection_item') but when I try to grab this one: entity_load('field_collection_item', array('field_request_collection')); it gives me an empty array. – Ken J Jun 7 '13 at 16:35
  • So I had to use the entity id which in this case is 1(I only have a single field collection as of now). So: entity_load('field_collection_item', array('1')); – Ken J Jun 7 '13 at 16:46
  • So I figured out how to REPLACE the value of an existing field. This is on drupal.org/node/1842304. However, I want to ADD a new value as this is a multi-value field. – Ken J Jun 7 '13 at 17:02
4

Something like this ought to do the trick:

$nload = node_load($requestid);
$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_request_collection'));
$field_collection_item->setHostEntity('node', $nload);
$field_collection_item->field_products[LANGUAGE_NONE][0]['target_id'] = intval($productid);
$field_collection_item->save();

$nload->field_request_collection[LANGUAGE_NONE][]['value'] = $field_collection_item->item_id;
  • I was working on the same code but I'm getting caught up with this: PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'field_products_target_id' cannot be null: – Ken J Jun 7 '13 at 17:43
  • Ah ok, field_products is an entity reference. I'll update the answer – Clive Jun 7 '13 at 17:43
  • Tip: After $field_collection_item->save(); the Field collection entity is already attached to the Host entity. – 42droids Ltd Jun 26 '15 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.