Error message

You are browsing documentation for drupal 7.x, which is not supported anymore. Read the updated version of this page for drupal 11.x (the latest version).

function hook_node_insert

Respond to creation of a new node.

This hook is invoked from node_save() after the database query that will insert the node into the node table is scheduled for execution, after the type-specific hook_insert() is invoked, and after field_attach_insert() is called.

Note that when this hook is invoked, the changes have not yet been written to the database, because a database transaction is still in progress. The transaction is not finalized until the save operation is entirely completed and node_save() goes out of scope. You should not rely on data in the database at this time as it is not updated yet. You should also note that any write/update database queries executed from this hook are also not committed immediately. Check node_save() and db_transaction() for more info.

Parameters

$node: The node that is being created.

Related topics

Node API Hooks
Functions to define and modify content types.
Hooks
Allow modules to interact with the Drupal core.
18 functions implement hook_node_insert()

Note: the procedural functions in this list are found by pattern matching, so the list may include some functions that are not actually implementations of this hook.

book_node_insert in modules/book/book.module
Implements hook_node_insert().
comment_node_insert in modules/comment/comment.module
Implements hook_node_insert().
EntityCrudHookTestHooks::nodeInsert in core/modules/system/tests/modules/entity_crud_hook_test/src/Hook/EntityCrudHookTestHooks.php
Implements hook_ENTITY_TYPE_insert() for node entities.
entity_crud_hook_test_node_insert in modules/simpletest/tests/entity_crud_hook_test.module
Implements hook_node_insert().
forum_node_insert in modules/forum/forum.module
Implements hook_node_insert().

... See full list

File

modules/node/node.api.php, line 520

Code

function hook_node_insert ($node) {
 db_insert ('mytable')->fields (array(
 'nid' => $node->nid,
 'extra' => $node->extra,
 ))
 ->execute ();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.