You are here

public static function WebformManagedFileBase::deleteFiles in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformManagedFileBase.php \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::deleteFiles()

Delete a webform submission file's usage and mark it as temporary.

Marks unused webform submission files as temporary. In Drupal 8.4.x+ unused webform managed files are no longer marked as temporary.

Parameters

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

null|array $fids: An array of file ids. If NULL all files are deleted.

3 calls to WebformManagedFileBase::deleteFiles()
WebformCompositeBase::postSave in src/Plugin/WebformElement/WebformCompositeBase.php
Acts on a saved webform submission element before the insert or update hook is invoked.
WebformManagedFileBase::postSave in src/Plugin/WebformElement/WebformManagedFileBase.php
Acts on a saved webform submission element before the insert or update hook is invoked.
WebformSubmissionStorage::delete in src/WebformSubmissionStorage.php
Deletes permanently saved entities.

File

src/Plugin/WebformElement/WebformManagedFileBase.php, line 1134

Class

WebformManagedFileBase
Provides a base class webform 'managed_file' elements.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public static function deleteFiles(WebformSubmissionInterface $webform_submission, array $fids = NULL) {

  // Make sure the file.module is enabled since this method is called from
  // \Drupal\webform\WebformSubmissionStorage::delete.
  if (!\Drupal::moduleHandler()
    ->moduleExists('file')) {
    return;
  }
  if ($fids === NULL) {
    $fids = \Drupal::database()
      ->select('file_usage', 'fu')
      ->fields('fu', [
      'fid',
    ])
      ->condition('module', 'webform')
      ->condition('type', 'webform_submission')
      ->condition('id', $webform_submission
      ->id())
      ->execute()
      ->fetchCol();
  }
  if (empty($fids)) {
    return;
  }

  /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
  $file_usage = \Drupal::service('file.usage');
  $make_unused_managed_files_temporary = \Drupal::config('webform.settings')
    ->get('file.make_unused_managed_files_temporary');
  $delete_temporary_managed_files = \Drupal::config('webform.settings')
    ->get('file.delete_temporary_managed_files');

  /** @var \Drupal\file\FileInterface[] $files */
  $files = File::loadMultiple($fids);
  foreach ($files as $file) {
    $file_usage
      ->delete($file, 'webform', 'webform_submission', $webform_submission
      ->id());

    // Make unused files temporary.
    if ($make_unused_managed_files_temporary && empty($file_usage
      ->listUsage($file)) && !$file
      ->isTemporary()) {
      $file
        ->setTemporary();
      $file
        ->save();
    }

    // Immediately delete temporary files.
    // This makes sure that the webform submission uploaded directory is
    // empty and can be deleted.
    // @see \Drupal\webform\WebformSubmissionStorage::delete
    if ($delete_temporary_managed_files && $file
      ->isTemporary()) {
      $file
        ->delete();
    }
  }
}