Programmatically alter a field collection field item not yet saved in the database Updating field collection field item gives error when one item is added.

I have the following code which works well when just changing values of a field already saved in the database but if I add a new field collection item to the node it throws an error (I guess because the value hasn't been saved yet in the database)


  EntityMetadataWrapperException: Unknown data property field_date :


function pdf_app_node_presave($node) {

 $node_wrapper = entity_metadata_wrapper('node', $node);

foreach ($node_wrapper-&gt;field_chapitres-&gt;value() as $library) {
$library_wrapper = entity_metadata_wrapper('field_collection_item', $library);
 $library_wrapper-&gt;field_pdf_title-&gt;set('newtitle');
                $library_wrapper-&gt;save();
  }
 unset($library);
 }
I might try removing $library_wrapper-&gt;save(). Calling $entity_wrapper-&gt;save() within hook_node_presave() is problematic for the exact reason you're stating -- the field collection item doesn't exist yet so it doesn't have the data needed to allow it to be created/saved. Calling $entity_wrapper-&gt;save() inside of hook_node_presave() can also sometimes cause issues on its own. By setting the value but not saving it, it should act as though you entered the value in the field on the edit form and should be saved when the parent node is saved.

Alternatively, you might try hook_node_update() or hook_node_insert() instead of hook_node_presave(). These fire later in the saving process, at which point your field collection item may already exist, in which case you won't have any problem saving it