function hook_entity_predelete

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

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')
        ->keys([
        '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.