I'm trying to implement creation of child nodes programmatically in hook_submit when creating parent nodes according to the parent node field value. Here is the idea. Parent node: field_stages (unlimited count). I want to create new "stage" nodes when I've added new parent node and save a reference to these child nodes in parent, although the references should not be editable and visible in add/edit form and node view. How it can be implemented in drupal?
Your main problem here is you won't be able to create a reference to the created node in a form submit as the node id hasn't been created yet. I'm not actually sure at what point it becomes available in the node process either. hook_node_save is still too early and it's the last node specific hook you can do this with.
I would skip trying to do this programmatically and use rules and entity reference field. Have your rule fire after a new node is saved. then have it create a new entity (node) and set data value (entity_reference) to the newly created node. much easier to maintain than code.
I can't use Rules directly because the field and content type are created in my own module Jul 15, 2013 at 4:52