Hi

I tried to create field_collections with the code below. But when I create a node, the code loop and I have a php error max_execution_time.

This code is in presave hook of my module.

function serveur_info_node_presave($node){
	  if ($node->type == 'serveur_info') {
   			$field_collection_item = entity_create('field_collection_item', array('field_name'=> 'serveur_appli'));
			$field_collection_item->setHostEntity('node', $node);
			$field_collection_item->serveur_appli_name[LANGUAGE_NONE][0]['value'] = 'test';
			$field_collection_item->save();//at this point my code doesn't respond and node is not created.
   
  }
	
}

Where is the problem ?

Comments

tatarbj’s picture

when you use hook_node_presave, you mustn't use a ->save() function by an entity. i think.
your node won't created, cause you will call around the save(), but if you call save(), you call again presave, and call save, etc... do you understand me? (sorry for my shit english)

jmuzz’s picture

Issue summary: View changes
Status: Active » Fixed

@tatarbj is correct, the call to setHostEntity should be enough make the field collection item be saved along with the node.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.