function hook_entity_predelete

Act before entity deletion.

This hook runs after the entity type-specific predelete hook.

Parameters

Drupal\Core\Entity\EntityInterface $entity: The entity object for the entity that is about to be deleted.

Related topics

1 function implements 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.

entity_crud_hook_test_entity_predelete in drupal/core/modules/system/tests/modules/entity_crud_hook_test/entity_crud_hook_test.module
Implements hook_entity_predelete().

File

drupal/core/includes/entity.api.php, line 128
Hooks provided the Entity module.

Code

function hook_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {

  // Count references to this entity in a custom table before they are removed
  // upon entity deletion.
  $id = $entity
    ->id();
  $type = $entity
    ->entityType();
  $count = db_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.
  $ref_count_record = (object) array(
    'count' => $count,
    'type' => $type,
    'id' => $id,
  );
  drupal_write_record('example_deleted_entity_statistics', $ref_count_record);
}