2 * Implements hook_entity_predelete().
4 function {{ machine_name }}_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
5 $connection = \Drupal::database();
6 // Count references to this entity in a custom table before they are removed
7 // upon entity deletion.
9 $type = $entity->getEntityTypeId();
10 $count = \Drupal::database()->select('example_entity_data')
11 ->condition('type', $type)
12 ->condition('id', $id)
17 // Log the count in a table that records this statistic for deleted entities.
18 $connection->merge('example_deleted_entity_statistics')
19 ->key(['type' => $type, 'id' => $id])
20 ->fields(['count' => $count])