I want to run some code after a node is inserted into the database. hook_node_insert() is close, of course, but as the doc says, "when this hook is invoked, the changes have not yet been written to the database, because a database transaction is still in progress". Is there a hook that I can implement that give me access to the newly-inserted node after it's completely in the database? hook_path_insert() does this, and is related to what I want to do, but it's not optimal for all sorts of reasons. Thoughts?


3 Answers 3


There isn't a hook that is called after the transaction is completed, as the code node_save() executes is executed during the transaction.

function node_save($node) {
  $transaction = db_transaction();

  try {

    // ...

  catch (Exception $e) {
    watchdog_exception('node', $e);
    throw $e;

If node_save() is called from node_form_submit() (the form submission handler for the node form), you can add a form submission handler that is called after node_form_submit() to the node edit form. It would be executed after the node is saved in the database.

What the documentation for hook_node_insert() means is that, since the data are not still saved in the database table, you cannot query a table making a join with the "node" table, as that table still contains the old data; in the same way, you should not call a function that queries the "node" table.


There is another way this can be achievable by adding your custom callback submit function in hook_form_alter which will execute after insert/update node.

Check the following code which is for article.

 * Implement hook_form_alter
 * @param type $form
 * @param type $form_state
 * @param type $form_id
function mymodule_form_alter(&$form, &$form_state, $form_id) {
    // Checking form_id
    if ($form_id == 'article_node_form') {
        // Adding custom callback function.
        $form['actions']['submit']['#submit'][] = '_callback_on_postsave';

Now you can do your post save functionality in _callback_on_postsave function. E.g:

 * General Callback for form_alter
 * @param type $form
 * @param type $form_state
function _callback_on_postsave($form, &$form_state) {
    $nid = $form_state['values']['nid'];
    $node = node_load($nid);
    // Do your stuff here 

  • I like this approach, but a question: I don't think the "Reverse submit array so newly added submit function should execute at last" line is needed -- won't adding the new callback with the [] operator insure that it's at the end of the execution order? (I've also done some testing that suggest that this is the case.)
    – Jim Miller
    Dec 2, 2016 at 17:29
  • Thanks @JimMiller to point out this, You are right, there is no need to reverse the submit functions, I removed that code.
    – vijay
    Dec 6, 2016 at 9:26
  • This helped me a lot to get hold of the old vid and the new vid at once.
    – leymannx
    Jun 21, 2017 at 11:00

Easiest solution is to use this module:

Hook Post Action

It allows you to use new hooks:

  • hook_entity_postsave
  • hook_entity_postinsert
  • hook_entity_postupdate
  • hook_entity_postdelete
  • hook_node_postsave
  • hook_node_postinsert
  • hook_node_postupdate
  • hook_node_postdelete

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.