You are here

public function AddFormBase::removeButtonSubmit in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::removeButtonSubmit()
  2. 9 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.

File

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

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(),
  ]));
}