Call a function on post-save of a node


As described in the documentation, there is no hook that could allow you to trigger some actions right after a node has been entirely saved. You may use a contrib module named Hook Post Action if you have huge needs of this hooks, but you may need of this just for one use-case, so add a new contrib module for that is not very clever...

Here is a simple way to trigger some action AFTER a node has been saved to the db, for example through the hook_node_ update (you may use hook_node_insert also).
The node object you get in your drupal_register_shutdown_function callback is fully saved in the database, so you may safely change it and save again if you would like to.

Notice that you can't use dsm() in a drupal_register_shutdown_function callback because the queries to the database are ended at this time (just not executed) and page is ready for output, so you may use a simple output file to test your code. I guess you may also use your IDE debugger.

Get raw version
  1. /**
  2.  * Implements hook_node_update().
  3.  */
  4. function my_module_node_update($node) {
  5. if($node->type == 'my_type'){
  6. // you can pass any parameters to your callback
  7. drupal_register_shutdown_function('_test_post_insert', $node);
  8. }
  9. }
  11. function _test_post_insert($node) {
  12. // debug the output
  13. file_put_contents('/temp/drupal-debug.txt', print_r($node, true));
  14. }