Skip to main content
added 486 characters in body
Source Link
berliner
  • 2.9k
  • 1
  • 21
  • 29

Calling node_save() from inside a hook_node_insert() implementation is error prone and should only be done with sufficient condition checks as to not provoke function nesting.

When you call node_save() coming from an insert operation and saving the $node object that already has an nid, then this call of node_save probably creates an update operation, which might result in strange errors. I don't fully follow the information flow here, but I would guess that one of the two calls to node_save() can easily run into a problem.

The correct way in a scenario where you want to alter node properties (e.g. status, promote, title, ...) prior to inserting, is to use hook_node_presave(). If you want to update field values inside a hook_node_insert(), that should be done by calling field_attach_update('node', $node).

Using the save() method of entity_metadata_wrapper doesn't exactly feel good either. Looking at the code in EntityDrupalWrapper::save, which I suppose is the wrapper class that is responsible, it looks like node_save() is going to be called in the end too. But I have never really used Entity Wrapper, so I'm a bit short here.

Edit: Regarding entity_metadata:_wrapper, looking again at the code of entity_save() I realize that it's not node_save() that is called in the end, but that this is entirely handled by the Entity module. At the end it will end up here if I'm not mistaken: EntityAPIController::save. I'm not sure though how that explains the behavior described in the comment.

Calling node_save() from inside a hook_node_insert() implementation is error prone and should only be done with sufficient condition checks as to not provoke function nesting.

When you call node_save() coming from an insert operation and saving the $node object that already has an nid, then this call of node_save probably creates an update operation, which might result in strange errors. I don't fully follow the information flow here, but I would guess that one of the two calls to node_save() can easily run into a problem.

The correct way in a scenario where you want to alter node properties (e.g. status, promote, title, ...) prior to inserting, is to use hook_node_presave(). If you want to update field values inside a hook_node_insert(), that should be done by calling field_attach_update('node', $node).

Using the save() method of entity_metadata_wrapper doesn't exactly feel good either. Looking at the code in EntityDrupalWrapper::save, which I suppose is the wrapper class that is responsible, it looks like node_save() is going to be called in the end too. But I have never really used Entity Wrapper, so I'm a bit short here.

Calling node_save() from inside a hook_node_insert() implementation is error prone and should only be done with sufficient condition checks as to not provoke function nesting.

When you call node_save() coming from an insert operation and saving the $node object that already has an nid, then this call of node_save probably creates an update operation, which might result in strange errors. I don't fully follow the information flow here, but I would guess that one of the two calls to node_save() can easily run into a problem.

The correct way in a scenario where you want to alter node properties (e.g. status, promote, title, ...) prior to inserting, is to use hook_node_presave(). If you want to update field values inside a hook_node_insert(), that should be done by calling field_attach_update('node', $node).

Using the save() method of entity_metadata_wrapper doesn't exactly feel good either. Looking at the code in EntityDrupalWrapper::save, which I suppose is the wrapper class that is responsible, it looks like node_save() is going to be called in the end too. But I have never really used Entity Wrapper, so I'm a bit short here.

Edit: Regarding entity_metadata:_wrapper, looking again at the code of entity_save() I realize that it's not node_save() that is called in the end, but that this is entirely handled by the Entity module. At the end it will end up here if I'm not mistaken: EntityAPIController::save. I'm not sure though how that explains the behavior described in the comment.

Source Link
berliner
  • 2.9k
  • 1
  • 21
  • 29

Calling node_save() from inside a hook_node_insert() implementation is error prone and should only be done with sufficient condition checks as to not provoke function nesting.

When you call node_save() coming from an insert operation and saving the $node object that already has an nid, then this call of node_save probably creates an update operation, which might result in strange errors. I don't fully follow the information flow here, but I would guess that one of the two calls to node_save() can easily run into a problem.

The correct way in a scenario where you want to alter node properties (e.g. status, promote, title, ...) prior to inserting, is to use hook_node_presave(). If you want to update field values inside a hook_node_insert(), that should be done by calling field_attach_update('node', $node).

Using the save() method of entity_metadata_wrapper doesn't exactly feel good either. Looking at the code in EntityDrupalWrapper::save, which I suppose is the wrapper class that is responsible, it looks like node_save() is going to be called in the end too. But I have never really used Entity Wrapper, so I'm a bit short here.