You are here

public function AddFormBase::updateWidget in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::updateWidget()
  2. 10 core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::updateWidget()

AJAX callback to send the new media item(s) to the calling code.

Parameters

array $form: The complete form.

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

Return value

array|\Drupal\Core\Ajax\AjaxResponse The form array when there are form errors or a AJAX response to select the created items in the media library.

File

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

Class

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

Namespace

Drupal\media_library\Form

Code

public function updateWidget(array &$form, FormStateInterface $form_state) {
  if ($form_state::hasAnyErrors()) {
    return $form;
  }

  // The added media items get an ID when they are saved in ::submitForm().
  // For that reason the added media items are keyed by delta in the form
  // state and we have to do an array map to get each media ID.
  $current_media_ids = array_map(function (MediaInterface $media) {
    return $media
      ->id();
  }, $this
    ->getCurrentMediaItems($form_state));

  // Allow the opener service to respond to the selection.
  $state = $this
    ->getMediaLibraryState($form_state);
  return $this->openerResolver
    ->get($state)
    ->getSelectionResponse($state, $current_media_ids)
    ->addCommand(new CloseDialogCommand());
}