function AddFormBase::removeButtonSubmit

Same name in other branches
  1. 9 core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::removeButtonSubmit()
  2. 8.9.x core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::removeButtonSubmit()
  3. 11.x core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::removeButtonSubmit()

Submit handler for the remove button.

Parameters

array $form: The form render array.

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

1 method overrides AddFormBase::removeButtonSubmit()
FileUploadForm::removeButtonSubmit in core/modules/media_library/src/Form/FileUploadForm.php
Submit handler for the remove button.

File

core/modules/media_library/src/Form/AddFormBase.php, line 560

Class

AddFormBase
Provides a base class for creating media items from within the media library.

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];
    $added_media = $form_state->get('media');
    $removed_media = $added_media[$delta];
    // Update the list of added media items in the form state.
    unset($added_media[$delta]);
    // Update the media items in the form state.
    $form_state->set('media', $added_media)
        ->setRebuild();
    // Show a message to the user to confirm the media is removed.
    $this->messenger()
        ->addStatus($this->t('The media item %label has been removed.', [
        '%label' => $removed_media->label(),
    ]));
}

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