You are here

function hook_entity_predelete in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_predelete()
  2. 10 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

hook_ENTITY_TYPE_predelete()

Related topics

6 functions implement hook_entity_predelete()

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

comment_entity_predelete in core/modules/comment/comment.module
Implements hook_entity_predelete().
entity_crud_hook_test_entity_predelete in core/modules/system/tests/modules/entity_crud_hook_test/entity_crud_hook_test.module
Implements hook_entity_predelete().
entity_test_entity_predelete in core/modules/system/tests/modules/entity_test/entity_test.module
Implements hook_entity_predelete().
jsonapi_test_non_cacheable_methods_entity_predelete in core/modules/jsonapi/tests/modules/jsonapi_test_non_cacheable_methods/jsonapi_test_non_cacheable_methods.module
Implements hook_entity_predelete().
menu_link_content_entity_predelete in core/modules/menu_link_content/menu_link_content.module
Implements hook_entity_predelete().

... See full list

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 1338
Hooks and documentation related to entities.

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();
}