You are here

public static function BlockUploadBuild::blockUploadDeleteFiles in Block Upload 8

Deletes files marked by checkbox in deletion form.

2 calls to BlockUploadBuild::blockUploadDeleteFiles()
BlockUploadForm::submitForm in src/Form/BlockUploadForm.php
Form submission handler.
BlockUploadPluploadForm::submitForm in src/Form/BlockUploadPluploadForm.php
Saves files uploaded via plupload form.

File

src/BlockUploadBuild.php, line 91

Class

BlockUploadBuild
BlockUploadBuild class.

Namespace

Drupal\block_upload

Code

public static function blockUploadDeleteFiles($node, $field_name, &$values) {
  $delete_files = array_values($values['remove_files']);
  $count = 0;
  foreach ($node
    ->get($field_name)
    ->getValue() as $file_field) {
    if (in_array($file_field['target_id'], $delete_files)) {
      $node
        ->get($field_name)
        ->removeItem($count);
      $storage = \Drupal::entityTypeManager()
        ->getStorage('file');
      $entities = $storage
        ->load($file_field['target_id']);
      $storage
        ->delete([
        $entities,
      ]);
    }
    else {
      $count++;
    }
  }
  \Drupal::messenger()
    ->addMessage(t('File(s) was successfully deleted!'));
}