function hook_ENTITY_TYPE_insert

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

Respond to creation of a new entity of a particular type.

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_insert()

Related topics

9 functions implement hook_ENTITY_TYPE_insert()

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

delay_cache_tags_invalidation_entity_test_insert in core/modules/system/tests/modules/delay_cache_tags_invalidation/delay_cache_tags_invalidation.module
Implements hook_ENTITY_TYPE_insert().
entity_test_entity_test_insert in core/modules/system/tests/modules/entity_test/entity_test.module
Implements hook_ENTITY_TYPE_insert() for 'entity_test'.
entity_test_entity_test_mulrev_changed_translation_insert in core/modules/system/tests/modules/entity_test/entity_test.module
Implements hook_ENTITY_TYPE_translation_insert() for 'entity_test_mulrev'.
entity_test_entity_test_mulrev_translation_insert in core/modules/system/tests/modules/entity_test/entity_test.module
Implements hook_ENTITY_TYPE_translation_insert().
entity_test_entity_test_mul_changed_translation_insert in core/modules/system/tests/modules/entity_test/entity_test.module
Implements hook_ENTITY_TYPE_translation_insert() for 'entity_test_mul_changed'.

... See full list

File

core/lib/Drupal/Core/Entity/entity.api.php, line 1197

Code

function hook_ENTITY_TYPE_insert(\Drupal\Core\Entity\EntityInterface $entity) {
    // Insert the new entity into a fictional table of this type of entity.
    \Drupal::database()->insert('example_entity')
        ->fields([
        'id' => $entity->id(),
        'created' => \Drupal::time()->getRequestTime(),
        'updated' => \Drupal::time()->getRequestTime(),
    ])
        ->execute();
}

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