You are here

function _social_comment_upload_delete_comment_files in Open Social 8.7

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_comment_upload/social_comment_upload.module \_social_comment_upload_delete_comment_files()
  2. 8 modules/social_features/social_comment_upload/social_comment_upload.module \_social_comment_upload_delete_comment_files()
  3. 8.2 modules/social_features/social_comment_upload/social_comment_upload.module \_social_comment_upload_delete_comment_files()
  4. 8.3 modules/social_features/social_comment_upload/social_comment_upload.module \_social_comment_upload_delete_comment_files()
  5. 8.4 modules/social_features/social_comment_upload/social_comment_upload.module \_social_comment_upload_delete_comment_files()
  6. 8.5 modules/social_features/social_comment_upload/social_comment_upload.module \_social_comment_upload_delete_comment_files()
  7. 8.6 modules/social_features/social_comment_upload/social_comment_upload.module \_social_comment_upload_delete_comment_files()
  8. 8.8 modules/social_features/social_comment_upload/social_comment_upload.module \_social_comment_upload_delete_comment_files()
  9. 10.3.x modules/social_features/social_comment_upload/social_comment_upload.module \_social_comment_upload_delete_comment_files()
  10. 10.0.x modules/social_features/social_comment_upload/social_comment_upload.module \_social_comment_upload_delete_comment_files()
  11. 10.1.x modules/social_features/social_comment_upload/social_comment_upload.module \_social_comment_upload_delete_comment_files()
  12. 10.2.x modules/social_features/social_comment_upload/social_comment_upload.module \_social_comment_upload_delete_comment_files()

Deletes the files associated with a comment.

This can be used to clean up files that were uploaded with a comment once that comment is being removed.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The comment for which attached files should be deleted.

Throws

\Drupal\Core\Entity\EntityStorageException

1 call to _social_comment_upload_delete_comment_files()
social_comment_upload_comment_delete in modules/social_features/social_comment_upload/social_comment_upload.module
Implements hook_comment_delete().

File

modules/social_features/social_comment_upload/social_comment_upload.module, line 129
Module file for Social Comment Upload.

Code

function _social_comment_upload_delete_comment_files(EntityInterface $entity) {

  // If this comment doesn't have any attached files we abort early.
  if (!$entity
    ->hasField('field_comment_files') || $entity
    ->get('field_comment_files')
    ->isEmpty()) {
    return;
  }

  // Extract all the file ids referred.
  $file_ids = array_column($entity
    ->get('field_comment_files')
    ->getValue(), 'target_id');

  // Try to load the actual file objects.
  if ($files = File::loadMultiple($file_ids)) {

    // Iterate and delete individual file objects.
    foreach ($files as $file) {

      // Delete the file object.
      $file
        ->delete();
    }
  }
}