Same name and namespace in other branches
  1. 7.x modules/system/system.api.php \hook_entity_insert()
  2. 8.9.x core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_insert()
  3. 9 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_insert()

Respond to creation of a new entity.

This hook runs once the entity has been stored. Note that hook implementations may not alter the stored entity data.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity object.

See also

hook_ENTITY_TYPE_insert()

Related topics

7 functions implement 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.

comment_entity_insert in core/modules/comment/comment.module
Implements hook_entity_insert().
content_moderation_entity_insert in core/modules/content_moderation/content_moderation.module
Implements hook_entity_insert().
content_moderation_test_resave_entity_insert in core/modules/content_moderation/tests/modules/content_moderation_test_resave/content_moderation_test_resave.module
Implements hook_entity_insert().
editor_entity_insert in core/modules/editor/editor.module
Implements hook_entity_insert().
entity_crud_hook_test_entity_insert in core/modules/system/tests/modules/entity_crud_hook_test/entity_crud_hook_test.module
Implements hook_entity_insert().

... See full list

1 invocation of hook_entity_insert()
ConfigEntityStorageTest::testSaveInsert in core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php
@covers ::save @covers ::doSave

File

core/lib/Drupal/Core/Entity/entity.api.php, line 1173
Hooks and documentation related to entities.

Code

function hook_entity_insert(\Drupal\Core\Entity\EntityInterface $entity) {

  // Insert the new entity into a fictional table of all entities.
  \Drupal::database()
    ->insert('example_entity')
    ->fields([
    'type' => $entity
      ->getEntityTypeId(),
    'id' => $entity
      ->id(),
    'created' => \Drupal::time()
      ->getRequestTime(),
    'updated' => \Drupal::time()
      ->getRequestTime(),
  ])
    ->execute();
}