You are here

public function AddFormBase::updateLibrary 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::updateLibrary()

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

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 if there are validation errors, or an AJAX response to add the created items to the current selection.

File

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

Class

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

Namespace

Drupal\media_library\Form

Code

public function updateLibrary(array &$form, FormStateInterface $form_state) {
  if ($form_state::hasAnyErrors()) {
    return $form;
  }
  $media_ids = array_map(function (MediaInterface $media) {
    return $media
      ->id();
  }, $this
    ->getAddedMediaItems($form_state));
  $response = new AjaxResponse();
  $response
    ->addCommand(new UpdateSelectionCommand($media_ids));
  $media_id_to_focus = array_pop($media_ids);
  $response
    ->addCommand(new ReplaceCommand('#media-library-add-form-wrapper', $this
    ->buildMediaLibraryUi($form_state)));
  $response
    ->addCommand(new InvokeCommand("#media-library-content [value={$media_id_to_focus}]", 'focus'));
  return $response;
}