You are here

function fancy_file_delete_batch in Fancy File Delete 8

Batch function.

3 string references to 'fancy_file_delete_batch'
FancyFileDeleteManual::_submitForm in src/Form/FancyFileDeleteManual.php
fancy_file_delete_files in ./fancy_file_delete.module
Normal File Delete Action for hook_action_info.
fancy_file_delete_files_force in ./fancy_file_delete.module
Force File Delete Action for hook_action_info.

File

./fancy_file_delete.batch.inc, line 19
Batch function.

Code

function fancy_file_delete_batch($fid, $force, &$context) {

  // Update our progress information.
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
  }
  $context['sandbox']['progress']++;

  // Manual / Orphan Delete.
  if (is_numeric($fid)) {
    $file = File::load($fid);
    if ($file) {
      if ($force) {

        // Remove these from the DB.
        $connection = Database::getConnection();
        $connection
          ->delete('file_managed')
          ->condition('fid', $fid)
          ->execute();
        $connection
          ->delete('file_usage')
          ->condition('fid', $fid)
          ->execute();

        // Now Delete the file completely.
        // Skip file api and just delete the entity, quicker.
        $controller = \Drupal::entityTypeManager()
          ->getStorage('file');
        $entity = $controller
          ->loadMultiple([
          $fid,
        ]);
        $controller
          ->delete($entity);
      }
      else {
        $file
          ->delete();
      }
      $context['results'][] = $fid;
    }
  }
  else {

    // @todo fix this to be the new way.
    \Drupal::database()
      ->delete('unmanaged_files')
      ->condition('path', $fid)
      ->execute();
    FileSystemInterface::delete($fid);
    $context['results'][] = $fid;
  }

  // @todo Probably need to add some logic in case there were failures, etc.
  $context['message'] = t('Now cleansing the system of fid#%fid', [
    '%fid' => $fid,
  ]);
}