I have a node type with two field_collection fields. When I save the node I need to write the entity_id of the first field_collection into the secondo field_collection. This is my code:
function mymodule_node_update($node){
$entity_id = $node->field_collection_1['und'][0]['value'];
$entiry_array = entity_load('field_collection_item',array(entity_id));
$entity = array_shift($entity_array);
if (empty($entity->field_reference['und'][0]['value'])) {
$entity->field_reference['und'][0]['value'] = $node->field_collection_2['und'][0]['value'];
$entity->save();
}
}
There are 2 problems there: 1) in hook_node_update() data is not committed yet https://drupal.org/node/1677830 2) $entity->save() triggers again hook_node_update() (and in my opinion it shouldn't)
So at the end in $node->field_collection_1 I have the right id $node->field_collection_2 would have, but due to recursive call to hook_node_update() and the fact that in hook_node_update() data is not committed to database yet field_collection_2 entity and all changes made to the node vanish.
Can someone suggest me a possible solution?
Thank you
hook_node_update()
: That hook is called for ALL node types, so it's wise to check the $node->type.