Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / hook / entity_predelete.twig
diff --git a/vendor/chi-teck/drupal-code-generator/templates/d8/hook/entity_predelete.twig b/vendor/chi-teck/drupal-code-generator/templates/d8/hook/entity_predelete.twig
new file mode 100644 (file)
index 0000000..5aaf9a5
--- /dev/null
@@ -0,0 +1,21 @@
+/**
+ * Implements hook_entity_predelete().
+ */
+function {{ machine_name }}_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->getEntityTypeId();
+  $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.
+  db_merge('example_deleted_entity_statistics')
+    ->key(['type' => $type, 'id' => $id])
+    ->fields(['count' => $count])
+    ->execute();
+}