I have a use case where I need to create a copy of an entity (commerce product) that is home to various field collections.
the entity is created by a simple load/save combo:
// load entity $old_entity = commerce_product_load_by_sku($sku); // copy $new_entity = $old_entity; // alter copy so it can be saved as a new entity $new_entity->status = 0; $new_entity->title = $old_entity->title . '(COPY)'; $new_entity->is_new = TRUE; // [...] and a few more unsets (created, revision, copying translations...) // ->>> missing field_collection clone / exchange of id references in the entity here <<<- // save entity commerce_product_save($new_entity);
As the field_collection id stored in the entity remains unchanged, both entities now share the field collection which leads to unwanted effects.
Can someone point me to a reference on how to create copies of field_collections and get their id's returned, so I can exchange them in the parent entity before saving it? ... or maybe there is an even better way to deal with this?