I'm trying to update a new field collection added by a user as follow :

  • Access host entity fields and retrieve some datas
  • Do some custom stuff
  • Update a value of a field collection (field reference pointing to a product)

by using hook_field_collection_item_insert to :

  • access newly created field collection
  • 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
$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 why this doesn't work, as I can access host entity assuming that field collection is linked to host entity.

Below a simplified version of code that I'm using :

<?php
/**
 * Implements hook_field_collection_item_insert() 
 * Intercepte fc child of coaching program on insert in order to create :
 *	- Product
 *	- Product display
 */
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) {
        $node_wrapper = field_collection_item_get_host_entity($field_collection_item); //Access host entity
        //Do custom stuff with host entity fields 
        //...
        //set $product_type, $price, $extras values 
        //...
        $product = my_module_create_product($product_type, $price, $extras);   //Call a function that create product
        if ($product) {  //Product creadted 
            $product_display = mpc_create_product_display($product, $item_id);
        } else {
            throw new Exception('Product creation as failed...');
        }
    } 
}

//Function that create product
function mpc_create_product($product_type, $price, $extras) {
    //code that create product ...
    return $created_product;
}

//Function that create product display
function my_module_create_product_display($product, $item_id) {
    $fc_wrapper = entity_metadata_wrapper('field_collection_item', $item_id); //Loading created field collection
    if ($fc_wrapper) {
        if ($fc_wrapper->field_product_ref->value() == NULL) {
            $fc_wrapper->field_coaching_product_ref->set($product->product_id);  //Setting product reference 
            $fc_wrapper->save(TRUE);  //Save the current field collection
            //$fc_wrapper->save(); //I've tried bellow still don't work either
            return $fc_wrapper->getIdentifier();
        } else {    
              return FALSE; //Product display already exist 
        }
    } else {
        throw new Exception('Exception... ');
    }
}
?>

I tested to load an existing field collection and that work just fine
Any support will be much appreciated.

Thanks in advance

Comments

ggive created an issue. See original summary.

ggive’s picture

Issue summary: View changes