7f7fb6bdf644b48304824d33d595852ef819f020
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / hook / entity_predelete.twig
1 /**
2  * Implements hook_entity_predelete().
3  */
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.
8   $id = $entity->id();
9   $type = $entity->getEntityTypeId();
10   $count = \Drupal::database()->select('example_entity_data')
11     ->condition('type', $type)
12     ->condition('id', $id)
13     ->countQuery()
14     ->execute()
15     ->fetchField();
16
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])
21     ->execute();
22 }