Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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... ');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement