function 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
Related topics
16 functions implement hook_entity_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.
- CommentHooks::entityInsert in core/
modules/ comment/ src/ Hook/ CommentHooks.php  - Implements hook_entity_insert().
 - comment_entity_insert in core/
modules/ comment/ comment.module  - Implements hook_entity_insert().
 - ContentModerationHooks::entityInsert in core/
modules/ content_moderation/ src/ Hook/ ContentModerationHooks.php  - Implements hook_entity_insert().
 - ContentModerationTestResaveHooks::entityInsert in core/
modules/ content_moderation/ tests/ modules/ content_moderation_test_resave/ src/ Hook/ ContentModerationTestResaveHooks.php  - Implements hook_entity_insert().
 - content_moderation_entity_insert in core/
modules/ content_moderation/ content_moderation.module  - Implements hook_entity_insert().
 
File
- 
              core/
lib/ Drupal/ Core/ Entity/ entity.api.php, line 1180  
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' => REQUEST_TIME,
    'updated' => REQUEST_TIME,
  ])
    ->execute();
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.