Chris@0: /** Chris@0: * Implements hook_entity_predelete(). Chris@0: */ Chris@0: function {{ machine_name }}_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) { Chris@4: $connection = \Drupal::database(); Chris@0: // Count references to this entity in a custom table before they are removed Chris@0: // upon entity deletion. Chris@0: $id = $entity->id(); Chris@0: $type = $entity->getEntityTypeId(); Chris@4: $count = \Drupal::database()->select('example_entity_data') Chris@0: ->condition('type', $type) Chris@0: ->condition('id', $id) Chris@0: ->countQuery() Chris@0: ->execute() Chris@0: ->fetchField(); Chris@0: Chris@0: // Log the count in a table that records this statistic for deleted entities. Chris@4: $connection->merge('example_deleted_entity_statistics') Chris@0: ->key(['type' => $type, 'id' => $id]) Chris@0: ->fields(['count' => $count]) Chris@0: ->execute(); Chris@0: }