By manuelBS on
Hey, i would like to add some field collection items while to a currently saved node (in hook_node_presave I thougth it would be the right place).
actually I do the fallowing:
$values['field_name'] = 'field_invoice_vat';
$vat_rate_collection = entity_create('field_collection_item', $values);
// Attach the field_collection entity to the application node. This has to
// happen separately so that it actually works -- you can't just specify
// this via the $values array.
//@see http://drupal.org/node/1353926
$vat_rate_collection->setHostEntity('node', $invoice, LANGUAGE_NONE, FALSE); //last param: true= link node to host
//save the entity to link it to the host with its id, otherwise we dont have an id
$vat_rate_collection->field_vat_rate[LANGUAGE_NONE]['value'] = $vat_rate;
$vat_rate_collection->field_vat_total[LANGUAGE_NONE]['value'] = $vat;
$vat_rate_collection->save(true); //last param skipps host save, because this will result in an endless loop!
I even tried to set TRUE as the last parameter of setHostEntity but this didnt work. The new entity is saved into the database, but the node, that is saved doesn't link to this entity.I thought this should setHostEntity do.
Is there any other way to do this or is hook_node_presave the wrong place for that?
Comments
In hook_node_update it works
In hook_node_update it works right now with the fallowing code:
But it doesn't work on hook_node_insert. Does anybody have any suggestions why that doesn't work?
ERPAL - Drupal business applications
Drupal security update automation
Banging my head against this
Banging my head against this all day, finally found the solution. I guess it's too late for you, but I found this page after searching for the keywords: entity_create and hook_node_insert
Alright, the thing is that the $node is hook_node_insert is already committed before we can act on it. So with the code above, the field collection will be created, but the node won't be updated to inlude the reference. Perhaps this should've been taken care of with setHostEntity, I haven't checked. This worked for me:
Points of interest:
I'm stumped as to why I need to set the $node->field_FIELD... manually when a dsm($node) before this line shows it's there already, but as I said, without it, field_attach_update won't fire.
There might be som bugs hidden here, but I'm not sure where/which function should take care of what... I would've thought the setHostEntity method would take care of updating the node with the new field collection field... but perhaps the problem is that I'm running this code in hook_node_insert. If you know, please enlighten us all :)