Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. function my_module_field_collection_item_insert($field_collection_item) {
  2.  
  3. $item_id = $field_collection_item->item_id;
  4. //dpm($item_id);
  5.  
  6. //Check the field collection bundle & if it's just have been created
  7. if ($field_collection_item->field_name == 'field_my_field_collection' && $field_collection_item->is_new == TRUE) {
  8.  
  9. //Access host entity
  10. $node_wrapper = field_collection_item_get_host_entity($field_collection_item);
  11.  
  12. //Do custom stuff with host entity fields
  13. //...
  14. //set $product_type, $price, $extras values
  15.  
  16. //Call a function that create product
  17. $product = my_module_create_product($product_type, $price, $extras);
  18.  
  19. //Product creadted
  20. if ($product) {
  21.  
  22. $product_display = my_module_create_product_display($product, $item_id);
  23.  
  24. } else {
  25. throw new Exception('Product creation as failed...');
  26. }
  27. }
  28. }
  29.  
  30. //Function that create product
  31. function my_module_create_product($product_type, $price, $extras) {
  32. //create product
  33. return $created_product;
  34. }
  35.  
  36. //Function that create product display
  37. function my_module_create_product_display($product, $item_id) {
  38.  
  39. //Note that loading existing field collection here works but it not what I need
  40. //Loading created field collection
  41. $fc_wrapper = entity_metadata_wrapper('field_collection_item', $item_id);
  42.  
  43. if ($fc_wrapper) {
  44.  
  45. if ($fc_wrapper->field_product_ref->value() == NULL) {
  46.  
  47. //Setting product reference
  48. $fc_wrapper->field_coaching_product_ref->set($product->product_id);
  49.  
  50. //Only need to save the current field collection
  51. $fc_wrapper->save(TRUE);
  52.  
  53. //I've tried bellow still don't work either
  54. //$fc_wrapper->save();
  55. return $fc_wrapper->getIdentifier();
  56.  
  57. } else {
  58. //Product display already exist
  59. return FALSE;
  60. }
  61. } else {
  62. throw new Exception('Exception... ');
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement