From 037aba9fba3c23fd24adcc8f1e48acb1ba9b1f63 Mon Sep 17 00:00:00 2001 From: jochemvn Date: Tue, 11 Jul 2017 10:27:12 +0200 Subject: [PATCH] 2890000 by jochemvn: When deleting a comment, also remove any attached files --- .../social_comment_upload.module | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/modules/social_features/social_comment_upload/social_comment_upload.module b/modules/social_features/social_comment_upload/social_comment_upload.module index 3feb8fbfa18..3150579ad49 100644 --- a/modules/social_features/social_comment_upload/social_comment_upload.module +++ b/modules/social_features/social_comment_upload/social_comment_upload.module @@ -52,3 +52,20 @@ function social_comment_upload_preprocess_file_widget_multiple(&$variables) { $variables['table']['#tabledrag'] = []; } } + +/** + * Implements hook_comment_delete(). + */ +function social_comment_upload_comment_delete(Drupal\Core\Entity\EntityInterface $entity) { + // Loop through the files in the comment. + foreach ($entity->field_comment_files as $filefield) { + // Try to load the actual file object. + $file = \Drupal\file\Entity\File::load($filefield->target_id); + // Check if it's an actual file object. + if ($file instanceof \Drupal\file\Entity\File) { + // Delete the file object. + $file->delete(); + } + } + +}