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

2 functions implement hook_entity_predelete()

File

drupal/core/includes/entity.api.php, line 251
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);
}