You are here

public function FileUploadForm::removeButtonSubmit in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media_library/src/Form/FileUploadForm.php \Drupal\media_library\Form\FileUploadForm::removeButtonSubmit()

Submit handler for the remove button.

Parameters

array $form: The form render array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Overrides AddFormBase::removeButtonSubmit

File

core/modules/media_library/src/Form/FileUploadForm.php, line 363

Class

FileUploadForm
Creates a form to create media entities from uploaded files.

Namespace

Drupal\media_library\Form

Code

public function removeButtonSubmit(array $form, FormStateInterface $form_state) {

  // Retrieve the delta of the media item from the parents of the remove
  // button.
  $triggering_element = $form_state
    ->getTriggeringElement();
  $delta = array_slice($triggering_element['#array_parents'], -2, 1)[0];

  /** @var \Drupal\media\MediaInterface $removed_media */
  $removed_media = $form_state
    ->get([
    'media',
    $delta,
  ]);
  $file = $removed_media
    ->get($this
    ->getSourceFieldName($removed_media->bundle->entity))->entity;
  if ($file instanceof FileInterface && empty($this->fileUsage
    ->listUsage($file))) {
    $file
      ->delete();
  }
  parent::removeButtonSubmit($form, $form_state);
}