You are here

public function AddMediaFormBase::removeButtonSubmit in Media Directories 8

Same name and namespace in other branches
  1. 2.x modules/media_directories_ui/src/Form/AddMediaFormBase.php \Drupal\media_directories_ui\Form\AddMediaFormBase::removeButtonSubmit()

Submit handler for the remove button.

Parameters

array $form: The form render array.

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

File

modules/media_directories_ui/src/Form/AddMediaFormBase.php, line 725

Class

AddMediaFormBase
Class AddMediaFormBase.

Namespace

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