15

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?

  • 3
    you can use the combination of hook_node_insert and hook_node_update (acts every time a node is being saved). – AjitS Jan 15 '13 at 5:22
  • Yes, I described that in the post above. – Kenny Wyland Jan 15 '13 at 6:00
  • @KennyWyland you mentioned hook_node_submit but not hook_node_update? – David Thomas Jan 15 '13 at 7:38
  • You will get the node nid in hook_node_insert as well as in hook_node_update. – Sumit Madan Jan 15 '13 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 '13 at 10:36
5

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.

  • That's cool! You could even do this from hook_form_alter(). drupal.stackexchange.com/a/221041/15055 – leymannx Jun 21 '17 at 11:38
  • 1
    @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 '17 at 17:07
12

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

  • 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 '14 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 '18 at 14:19
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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