19

I've found several posts with this same question, but the answers always diverge to some other topic or they don't work for my needs.

After a node is saved, I want my custom module to insert a row into one of my custom tables which contains the nid of the new node. When hook_node_submit is called, the nid doesn't exist yet. hook_node_insert does have the nid, but only gets called the first time the node is saved, but I potentially need to take action each time the node is saved.

My plan is to separate my functionality into its own method and implement both hooks, but I'm trying to learn to do things properly with Drupal and so I'm wondering if there is a hook that actually fits with my requirements.

Is there a hook which is called every time a node is saved and called AFTER the save is complete?

6
  • 4
    you can use the combination of hook_node_insert and hook_node_update (acts every time a node is being saved).
    – AjitS
    Jan 15, 2013 at 5:22
  • Yes, I described that in the post above. Jan 15, 2013 at 6:00
  • @KennyWyland you mentioned hook_node_submit but not hook_node_update? Jan 15, 2013 at 7:38
  • You will get the node nid in hook_node_insert as well as in hook_node_update. Jan 15, 2013 at 9:24
  • 2
    hook_node_insert() and hook_node_update() are the correct hooks for this, hook_node_submit() is called during the form submission process before the node is saved. Don't forget you need to clear caches any time you add/remove a hook
    – Clive
    Jan 15, 2013 at 10:36

3 Answers 3

8

Looking at the comments on the question, it looks like the obvious solution is to use the hooks hook_node_insert() and hook_node_update().

I actually just did something very similar as described in the answer: I wrote a function that does what I want to be done every time a node is created/updated and then call it from each of those hooks. It works just fine.

2
  • That's cool! You could even do this from hook_form_alter(). drupal.stackexchange.com/a/221041/15055
    – leymannx
    Jun 21, 2017 at 11:38
  • 2
    @leymannx The downside in that method is it would only be called when a node is inserted/updated via a specific form. In my case, I needed to catch all additions and modifications of nodes.
    – donut
    Jun 21, 2017 at 17:07
14

The correct way of doing this is to respect Drupal's db transaction. If you want to do any database action in hook_node_insert you must consider that the node is not actually saved yet. which means if it fails and rolled back you end up with orphaned data in your database. So Drupal database API should be used and not direct query execution.

If you want to update the node itself in hook_node_insert, you can't because it's not possible to use node_save since the node is not yet saved in the database and calling it causes exception. One solution is to use register_shutdown_function function and passing nid, you can use node_load to make sure that it's really saved and then do any other action you may want on the new node.

function your_module_node_update($node){
  if($node->type == 'your_node'){
    register_shutdown_function('_your_module_post_insert',$node->nid);
  }
}

function _your_module_post_insert($nid) {
  $node = node_load($nid);
  if ($node) {
      node_save($node);
  }
} 

UPDATE: You can also find an easier alternative here https://stackoverflow.com/a/24035797/1726778

2
  • 1
    Good idea. Though it's better to use drupal_register_shutdown_function(). But be aware that both of them seem to have problems in php-fpm environment, see drupal.stackexchange.com/q/102603/10667
    – Shevchuk
    Feb 7, 2014 at 7:57
  • I was having trouble checking if a node was acting as an Organic Group og_is_group. I think I was getting false negatives for this reason.
    – icicleking
    Apr 20, 2018 at 14:19
7

There is a new drupal module, Hook Post Action, which adds the post save hooks. Let's try it to see if it works.

1
  • I tried using it in our project. Initially, it worked, but after some time we noticed some memory leaks being supposedly caused by it. Not sure if it's something in our D7 application though. May 5, 2022 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.