You are here

public function YamlFormManagedFileBase::postDelete in YAML Form 8

Delete any additional value associated with an element.

Currently only applicable to file uploads.

Parameters

array $element: An element.

\Drupal\yamlform\YamlFormSubmissionInterface $yamlform_submission: A form submission.

Overrides YamlFormElementBase::postDelete

File

src/Plugin/YamlFormElement/YamlFormManagedFileBase.php, line 350

Class

YamlFormManagedFileBase
Provides a base class form 'managed_file' elements.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

public function postDelete(array &$element, YamlFormSubmissionInterface $yamlform_submission) {
  $yamlform = $yamlform_submission
    ->getYamlForm();
  $data = $yamlform_submission
    ->getData();
  $key = $element['#yamlform_key'];
  $value = isset($data[$key]) ? $data[$key] : [];
  $fids = is_array($value) ? $value : [
    $value,
  ];

  // Delete managed file record.
  foreach ($fids as $fid) {
    file_delete($fid);
  }

  // Remove the empty directory for all stream wrappers.
  $stream_wrappers = array_keys(\Drupal::service('stream_wrapper_manager')
    ->getNames(StreamWrapperInterface::WRITE_VISIBLE));
  foreach ($stream_wrappers as $stream_wrapper) {
    file_unmanaged_delete_recursive($stream_wrapper . '://yamlform/' . $yamlform
      ->id() . '/' . $yamlform_submission
      ->id());
  }
}