function hook_entity_predelete
Same name and namespace in other branches
- 8.9.x core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_predelete()
- 10 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_predelete()
- 11.x core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_predelete()
Act before entity deletion.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The entity object for the entity that is about to be deleted.
See also
Related topics
12 functions implement hook_entity_predelete()
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::entityPredelete in core/
modules/ comment/ src/ Hook/ CommentHooks.php - Implements hook_entity_predelete().
- comment_entity_predelete in core/
modules/ comment/ comment.module - Implements hook_entity_predelete().
- EntityCrudHookTestHooks::entityPredelete in core/
modules/ system/ tests/ modules/ entity_crud_hook_test/ src/ Hook/ EntityCrudHookTestHooks.php - Implements hook_entity_predelete().
- EntityOperations::entityPredelete in core/
modules/ workspaces/ src/ Hook/ EntityOperations.php - Implements hook_entity_predelete().
- EntityTestHooks::entityPredelete in core/
modules/ system/ tests/ modules/ entity_test/ src/ Hook/ EntityTestHooks.php - Implements hook_entity_predelete().
1 invocation of hook_entity_predelete()
- EntityStorageBase::delete in core/
lib/ Drupal/ Core/ Entity/ EntityStorageBase.php - Deletes permanently saved entities.
File
-
core/
lib/ Drupal/ Core/ Entity/ entity.api.php, line 1381
Code
function hook_entity_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')
->key([
'type' => $type,
'id' => $id,
])
->fields([
'count' => $count,
])
->execute();
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.