Need some help with your project? Contact me

Drupal 7 post insert hooks as a shutdown function

Have you ever needed to perform an action after a node has been inserted or updated? In this article we are going to see how we can trigger functionality after nodes have been inserted or updated. And no, it's probably not what you think.

By default, there is no hook that provides this in Drupal core. Sure, you have hook_node_insert(), hook_node_update() and hook_node_presave() but all of these are called within the scope of node_save(). This is important because, at this point, we cannot rely on the database for any data that may have been affected by the node save process. As the documentation for hook_node_insert() says:

when this hook is invoked, the changes have not yet been written to the database, because a database transaction is still in progress.

When you have to deal with something totally external to the node in question, this may not be a problem. Heck, you might not even need the database. But there are those one or two cases in which you do need data that has been affected by the process.

For example, when inserting a new node, various actions take place. One of them can be Pathauto doing some magic with the node title and saving a path alias for this node. So if you need to alter this final alias in the scope of hook_node_insert(), you cannot rely on something like path_load() to get the node alias data because it might have not yet been written. I've actually seen it return different things in different cases.

What you can do then is register a function with Drupal that will get called just about when Drupal is finished with the request. It's actually very simple. You just call drupal_register_shutdown_function() with the name of the function you want to be called. Additionally, you can pass as further arguments whatever it is in your context you want passed to it. It's kinda like subscribing a function to an event.

So now when the request is finishing, we know the node_save() database transaction has completed and we can rely on the data from the database. Using path_load() now will guarantee that the latest path for that node is being loaded.

Hope this helps.



Nice article.

In case it can help, the Hook Post Action module follows that exact method to expose several hooks that allow you to act after an entity has been inserted / updated / deleted.

There is already a module for that :)

:) Yes there is, but sometimes you may not need the entire thing just for one small use case. And knowing the power of drupal_register_shutdown_function() can be very helpful in other contexts as well :)

have any example drupal_register_shutdown_function ()


Thanks for sharing!

There is a hook to alter the alias before it is saved: hook_pathauto_alias_alter().

Thanks Danny for informative article. I usually use module for this...However, your guide also is very helpful.


Note that you will not be able to dsm() or var_dump() inside a drupal_register_shutdown_function callback function.
You may use your favorite IDE debugger (solution which I've not tested), or like me you can use an output file :

 * Implements hook_node_update().
function my_module_node_update($node) {
  if($node->type == 'my_type'){
    drupal_register_shutdown_function('_test_post_insert', $node);

function _test_post_insert($node) {
  // debug the output
  file_put_contents('/temp/drupal-debug.txt', print_r($node, true));

Wouldn't dd() function work in this case as well?

Thank you Danny, you saved my day!
I need this solution for a client site with very complex forms using paragraphs and field_collection modules. To gain overall delivery performance i decided to prerender the 'body' part of the node content as drupal render items and store them serialized into a custom module table. Because some of the nodes are a sort of aggregation pages which show teasers of other nodes and therefore they need to be updated as well when an editor updates a teaser node.
The drupal_register_shutdown_function makes this implementation easy, clean and handy.
Thanx for that.

btw: referring to debugging opportunities inside the shutdown callback you can write as well to watchdog.

exactly wat i needed

Add new comment

You can post comments in Markdown and basic HTML tags.
For code blocks, wrap your code within '~~~'. For example:
$var = 'my variable';