Essentially, there really isn't much difference between node_save()
and the save()
method of the entity metadata wrapper. Neither should however be used to save the node passed in as the argument to hook_node_insert()
.
The argument is passed by reference (since it's an object and all objects are passed by reference in later PHP versions), and any changes you make to it will be included when the node is saved by Drupal just after it's done calling the hook implementation.
Trying to manually save the same node again could trigger endless recursion into your hook_node_insert()
implementation.
Just alter the node object as you see fit (setting the title in this case, though that is not a translatable string, so replace t()
with check_plain()
) and do nothing more.
If you need to do more complex alterations, the entity metadata wrapper could come in handy, but you must remember that any alterations you want to have saved must be performed on the node instance passed as a reference to hook_node_insert()
. This means you can't node_load($node->nid)
here first, since that would actually just create a new node object with data that's already in the database. If you do manage to save that node copy without ending up in a recursion loop, it would either mean that the node save already in progress - the reason hook_node_insert()
was originally called - overwrites any changes you made, or fails because the new node to be saved already exists.
Instead, pass the $node
argument to entity_metadata_wrapper()
directly and the wrapper will be able to alter the correct node object for you, but remember to not call save()
on the wrapper!
entity_metadata_wrapper()
.entity_save()
is different, and I think this matches you question better.node_save()
inside ahook_node_insert()
, as this hook is triggered fromnode_save()
. Smells bad. If you just want to alter the data before it get's saved, usehook_node_presave()
.hook_node_insert()
[doc page] (api.drupal.org/api/drupal/modules!node!node.api.php/function/…), on whether or not to trust the nid contains in $node object while still insidehook_node_insert()
scope. Say, I wanted to trigger an email with link to the new node, and trigger it fromhook_node_insert()
can I be sure that the node is already committed to database?hook_node_insert
is invoked. Practically though, I'm not aware of any other hook based solution to react on a node being created, and usually there should be no reason for a transaction rollback. The note in the docs is also mostly about not tying to reload node data from the database, as it's not yet written.