The main goal is to create product & product display using drupal 7 & drupal commerce when a user with a certain role add a new field collection.
What I'm trying to do is when a new field collection is added :
- access host entity fields and retrieve some datas
- do some custom stuff
- update 1 value of a field collection which is a field reference to product
Steps followed :
- I'm using hook_field_collection_item_insert to access newly created field collection.
- I call a function which create a product (using commerce modules suites). Above steps works just fine.
- Then I call a function that update the current field collection field by adding a value which is a reference of product created in function my-module-create_product().
$fc_wrapper->save(TRUE);
is failling to save with the following error :
Exception: Unable to save a field collection item without a valid reference to a host entity in FieldCollectionItemEntity->save() (line 520 of ../mysite.local/docroot/sites/all/modules/contrib/field_collection/field_collection.module)
I've done some search so far without any success.
I can't figure out the fix for this, some direction or any information that will help to solve this issue will be much appreciated.
Below a simplified code that I'm using :
function my_module_field_collection_item_insert($field_collection_item) {
$item_id = $field_collection_item->item_id;
//dpm($item_id);
//Check the field collection bundle & if it's just have been created
if ($field_collection_item->field_name == 'field_my_field_collection' && $field_collection_item->is_new == TRUE) {
//Access host entity
$node_wrapper = field_collection_item_get_host_entity($field_collection_item);
//Do custom stuff with host entity fields
//...
//set $product_type, $price, $extras values
//Call a function that create product
$product = my_module_create_product($product_type, $price, $extras);
//Product creadted
if ($product) {
$product_display = my_module_create_product_display($product, $item_id);
} else {
throw new Exception('Product creation as failed...');
}
}
}
//Function that create product
function my_module_create_product($product_type, $price, $extras) {
//create product
return $created_product;
}
//Function that create product display
function my_module_create_product_display($product, $item_id) {
//Note that loading existing field collection here works but it not what I need
//Loading created field collection
$fc_wrapper = entity_metadata_wrapper('field_collection_item', $item_id);
if ($fc_wrapper) {
if ($fc_wrapper->field_product_ref->value() == NULL) {
//Setting product reference
$fc_wrapper->field_coaching_product_ref->set($product->product_id);
//Only need to save the current field collection
$fc_wrapper->save(TRUE);
//I've tried bellow still don't work either
//$fc_wrapper->save();
return $fc_wrapper->getIdentifier();
} else {
//Product display already exist
return FALSE;
}
} else {
throw new Exception('Exception... ');
}
}
$fc_wrapper->value()->setHostEntity('node', $node);
, where 'node' is the entity type the FC should be attached to, and$node
is the entity itselfException: The host entity may be set only during creation of a field collection item. in FieldCollectionItemEntity->setHostEntity() (line 330 of ../my_site.local/docroot/sites/all/modules/contrib/field_collection/field_collection.module
The api says this 'Sets the host entity. Only possible during creation of a item. My item is already created and linked to the host entity as I can access hostEntity fields and play with. Any other suggestion are welcome. thanks