annotate vendor/chi-teck/drupal-code-generator/templates/d8/hook/entity_predelete.twig @ 5:12f9dff5fda9
tip
Update to Drupal core 8.7.1
author |
Chris Cannam |
date |
Thu, 09 May 2019 15:34:47 +0100 |
parents |
a9cd425dd02b |
children |
|
rev |
line source |
Chris@0
|
1 /**
|
Chris@0
|
2 * Implements hook_entity_predelete().
|
Chris@0
|
3 */
|
Chris@0
|
4 function {{ machine_name }}_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
|
Chris@4
|
5 $connection = \Drupal::database();
|
Chris@0
|
6 // Count references to this entity in a custom table before they are removed
|
Chris@0
|
7 // upon entity deletion.
|
Chris@0
|
8 $id = $entity->id();
|
Chris@0
|
9 $type = $entity->getEntityTypeId();
|
Chris@4
|
10 $count = \Drupal::database()->select('example_entity_data')
|
Chris@0
|
11 ->condition('type', $type)
|
Chris@0
|
12 ->condition('id', $id)
|
Chris@0
|
13 ->countQuery()
|
Chris@0
|
14 ->execute()
|
Chris@0
|
15 ->fetchField();
|
Chris@0
|
16
|
Chris@0
|
17 // Log the count in a table that records this statistic for deleted entities.
|
Chris@4
|
18 $connection->merge('example_deleted_entity_statistics')
|
Chris@0
|
19 ->key(['type' => $type, 'id' => $id])
|
Chris@0
|
20 ->fields(['count' => $count])
|
Chris@0
|
21 ->execute();
|
Chris@0
|
22 }
|