8.5.x entity.api.php hook_entity_insert(Drupal\Core\Entity\EntityInterface $entity)
8.0.x entity.api.php hook_entity_insert(Drupal\Core\Entity\EntityInterface $entity)
8.1.x entity.api.php hook_entity_insert(Drupal\Core\Entity\EntityInterface $entity)
8.2.x entity.api.php hook_entity_insert(Drupal\Core\Entity\EntityInterface $entity)
8.3.x entity.api.php hook_entity_insert(Drupal\Core\Entity\EntityInterface $entity)
8.4.x entity.api.php hook_entity_insert(Drupal\Core\Entity\EntityInterface $entity)
8.6.x entity.api.php hook_entity_insert(Drupal\Core\Entity\EntityInterface $entity)
7.x system.api.php hook_entity_insert($entity, $type)

Act on entities when inserted.


$entity: The entity object.

$type: The type of entity being inserted (i.e. node, user, comment).

Related topics

1 function implements hook_entity_insert()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

entity_crud_hook_test_entity_insert in modules/simpletest/tests/entity_crud_hook_test.module
Implements hook_entity_insert().
2 invocations of hook_entity_insert()
field_attach_insert in modules/field/field.attach.inc
Save field data for a new entity.
file_save in includes/file.inc
Saves a file object to the database.


modules/system/system.api.php, line 328
Hooks provided by Drupal core and the System module.


function hook_entity_insert($entity, $type) {

  // Insert the new entity into a fictional table of all entities.
  $info = entity_get_info($type);
  list($id) = entity_extract_ids($type, $entity);
    'type' => $type,
    'id' => $id,
    'created' => REQUEST_TIME,
    'updated' => REQUEST_TIME,


bobojo’s picture

VERY IMPORTANT! This function is passed the UNSAVED entity, so it will not have an id assigned to it yet, and any attempt to save the entity within this function (or any functions called by it) will result in PDOExceptions due to trying to use the same unique id twice.

The documentation should really say "Act on entities BEFORE they are inserted." That would clarify this and prevent a lot of headaches by developers trying to do crazy stuff using this hook.

halth’s picture

@bobojo, are you sure?
I've made some tests here and the $entity object do have an ID.
Also, you can check it on the sample code provided above...

interdruper’s picture

So @bobojo comment is not accurate. $entity contains the id, or the nid, depending of which type of entity is inserted.

About the commitment to the DB, more information can be found on the hook_node_insert() thread: https://api.drupal.org/api/drupal/modules!node!node.api.php/function/hoo...

Notice that hook_entity_insert() and hook_node_insert() are both triggered when a new node is inserted, so only one of them should be used when adding a node.

kengilb’s picture

Since this hook runs during save, we have the entity id available to us.

If you're running into a case where you need to update the uid or title on the entity during this hook then you might want to run code similar to this to avoid PDOExceptions.

$entity->title .= '-' . $entity->id;
$clone = clone $entity;
entity_save('myentitytype', $clone);

The key is designate the entity as NOT new before attempting to save it via entity_save.

yakoub’s picture

you must use hook_entity_presave to change the title
callingl entity_save inside hook_entity_insert is very bad logic .

kiamlaluno’s picture

The hook is invoked right after the call to drupal_write_record('comment', $comment). What it is true is that it is invoked inside a transaction block, which means the database table could not contain the updated data.

fau’s picture

I tried to figure it out without going down to drupal transactions implementation details and I can't.

What happens inside of drupal_write_record or hook_entity_insert is part of the comment_save transaction, right? Transactions certainly should preserve order of execution.

On my system the database table always contains updated data. Did you mean it could not contain updated data depending on ie db type?

AravindM’s picture

Our product is an existing project.

Few of the variables set has written in module_entity_insert.
Variable get in module_node_insert.

Sometimes after saving the node form also not properly settings up.
Which hook will get call first? Is there any way to make it simple ?