function FileUploadForm::removeButtonSubmit

Same name in other branches
  1. 8.9.x core/modules/media_library/src/Form/FileUploadForm.php \Drupal\media_library\Form\FileUploadForm::removeButtonSubmit()
  2. 10 core/modules/media_library/src/Form/FileUploadForm.php \Drupal\media_library\Form\FileUploadForm::removeButtonSubmit()
  3. 11.x 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 375

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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.