Does hook_nood_presave execute before or after a custom form submit handler in the same node ? and I need to perform some action after the node contents have been saved in the database. So, where should put this code ?
1 Answer
In the core system hook_node_presave()
is only ever invoked once, from inside node_save()
itself. In the context of a node add/edit form it's called by the original form submit handler, which unless you've explicitly engineered otherwise, will be run before the call to your custom submit handler.
Similar to your other question the submit handler isn't really the best place for this logic, as that code will literally only run when a form has been submitted (not when a node has been added programmatically for example).
After node_save()
has saved the node data to the database, it invokes one of two other node hooks...hook_node_insert()
and hook_entity_update()
(obviously depending on the current operation).
One of those hooks will be called after the data has been committed to the db so will be perfect for your needs.
-
hmm.. ok I'll move my code to the hook_node_presave(), but one of the issues I was thinking about is how to access form data inside the hook ? as only $node is passed as an argument. Whereas in my custom submit function I can access it through $form_state['values']. Commented Aug 17, 2012 at 12:28
-
1All the form values are dumped onto the node object, so if there's a form field called
$form['my_field']
, you'll have access to$node->my_field
in the presave hook– Clive ♦Commented Aug 17, 2012 at 12:33 -
gr8 thanks a lot.. talking to u clears a lot of doubts.. thanks again, really appreciate it Commented Aug 17, 2012 at 12:36