Same name and namespace in other branches
- 8.9.x core/lib/Drupal/Core/Entity/entity.api.php \hook_ENTITY_TYPE_predelete()
- 9 core/lib/Drupal/Core/Entity/entity.api.php \hook_ENTITY_TYPE_predelete()
Act before entity deletion of a particular entity type.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The entity object for the entity that is about to be deleted.
See also
Related topics
22 functions implement hook_ENTITY_TYPE_predelete()
Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.
- book_node_predelete in core/
modules/ book/ book.module - Implements hook_ENTITY_TYPE_predelete() for node entities.
- comment_entity_predelete in core/
modules/ comment/ comment.module - Implements hook_entity_predelete().
- comment_user_predelete in core/
modules/ comment/ comment.module - Implements hook_ENTITY_TYPE_predelete() for user entities.
- config_test_config_test_predelete in core/
modules/ config/ tests/ config_test/ config_test.hooks.inc - Implements hook_config_test_predelete().
- entity_crud_hook_test_block_predelete in core/
modules/ system/ tests/ modules/ entity_crud_hook_test/ entity_crud_hook_test.module - Implements hook_ENTITY_TYPE_predelete() for block entities.
File
- core/
lib/ Drupal/ Core/ Entity/ entity.api.php, line 1403 - Hooks and documentation related to entities.
Code
function hook_ENTITY_TYPE_predelete(\Drupal\Core\Entity\EntityInterface $entity) {
$connection = \Drupal::database();
// Count references to this entity in a custom table before they are removed
// upon entity deletion.
$id = $entity
->id();
$type = $entity
->getEntityTypeId();
$count = \Drupal::database()
->select('example_entity_data')
->condition('type', $type)
->condition('id', $id)
->countQuery()
->execute()
->fetchField();
// Log the count in a table that records this statistic for deleted entities.
$connection
->merge('example_deleted_entity_statistics')
->keys([
'type' => $type,
'id' => $id,
])
->fields([
'count' => $count,
])
->execute();
}