You are here

public function AddBannerForm::dynamic_banner_image_delete in Dynamic Banner 8

This function will split the csv fid variable if it needs to be split And then delete those images from the file system and thier values in the db

1 call to AddBannerForm::dynamic_banner_image_delete()
AddBannerForm::validateForm in src/forms/AddBannerForm.php
Form validation handler.

File

src/forms/AddBannerForm.php, line 451

Class

AddBannerForm

Namespace

Drupal\dynamic_banner\forms

Code

public function dynamic_banner_image_delete($fid) {
  if (strrpos($fid, ',')) {

    // split the plain string into an array
    $all_fids = explode(",", $fid);

    // load all files at once

    //$all_files = file_load_multiple($all_fids);
    $all_files = \Drupal\file\Entity\File::loadMultiple($all_fids);
    if (is_array($all_files) && !empry($all_files)) {
      foreach ($all_files as $file) {
        if ($file) {

          // When a module is managing a file, it must manage the usage count.
          // Here we decrement the usage count with file_usage_delete().
          file_usage_delete($file, 'dynamic_banner', 'banner', 1);

          // The file_delete() function takes a file object and checks to see if
          // the file is being used by any other modules. If it is the delete
          // operation is cancelled, otherwise the file is deleted.

          //TODO:

          //file_delete($file);
        }
        $this
          ->messenger()
          ->addStatus(t('The image @image_name was removed.', array(
          '@image_name' => $file->filename,
        )));
      }
    }
  }
  else {
    $file = $fid ? \Drupal::service('entity_type.manager')
      ->getStorage('file')
      ->load($fid) : FALSE;
    if ($file) {

      // When a module is managing a file, it must manage the usage count.
      // Here we decrement the usage count with file_usage_delete().
      file_usage_delete($file, 'dynamic_banner', 'banner', 1);

      // The file_delete() function takes a file object and checks to see if
      // the file is being used by any other modules. If it is the delete
      // operation is cancelled, otherwise the file is deleted.

      //TODO:

      //file_delete($file);
    }
    if (isset($file->filename)) {
      $fileName = $file->filename;
    }
    else {
      $fileName = '';
    }
    $this
      ->messenger()
      ->addStatus(t('The image @image_name was removed.', array(
      '@image_name' => $fileName,
    )));
  }
}