You are here

function hook_entity_predelete in Drupal 10

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

2 invocations of hook_entity_predelete()
ConfigEntityStorageTest::testDelete in core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php
@covers ::delete @covers ::doDelete
EntityStorageBase::delete in core/lib/Drupal/Core/Entity/EntityStorageBase.php
Deletes permanently saved entities.

File

core/lib/Drupal/Core/Entity/entity.api.php, line 1347
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();
}