function AddFormBase::updateWidget

Same name in other branches
  1. 9 core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::updateWidget()
  2. 8.9.x core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::updateWidget()
  3. 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 an AJAX response to select the created items in the media library.

File

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

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.
    $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);
    $selected_count = $this->getSelectedMediaItemCount($media_ids, $form_state);
    $available_slots = $this->getMediaLibraryState($form_state)
        ->getAvailableSlots();
    if ($available_slots > 0 && $selected_count > $available_slots) {
        // Return to library where we display a warning about the overage.
        return $this->updateLibrary($form, $form_state);
    }
    return $this->openerResolver
        ->get($state)
        ->getSelectionResponse($state, $media_ids)
        ->addCommand(new CloseDialogCommand());
}

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