function AddFormBase::updateFormCallback

Same name in other branches
  1. 9 core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::updateFormCallback()
  2. 8.9.x core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::updateFormCallback()
  3. 10 core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::updateFormCallback()

AJAX callback to update the entire form based on source field input.

Parameters

array $form: The complete form.

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

Return value

\Drupal\Core\Ajax\AjaxResponse|array The form render array or an AJAX response object.

File

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

Class

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

Namespace

Drupal\media_library\Form

Code

public function updateFormCallback(array &$form, FormStateInterface $form_state) {
    $triggering_element = $form_state->getTriggeringElement();
    $wrapper_id = $triggering_element['#ajax']['wrapper'];
    $added_media = $form_state->get('media');
    $response = new AjaxResponse();
    // When the source field input contains errors, replace the existing form to
    // let the user change the source field input. If the user input is valid,
    // the entire modal is replaced with the second step of the form to show the
    // form fields for each media item.
    if ($form_state::hasAnyErrors()) {
        $response->addCommand(new ReplaceCommand('#media-library-add-form-wrapper', $form));
        return $response;
    }
    // Check if the remove button is clicked.
    if (end($triggering_element['#parents']) === 'remove_button') {
        // When the list of added media is empty, return to the media library and
        // shift focus back to the first tabbable element (which should be the
        // source field).
        if (empty($added_media)) {
            $response->addCommand(new ReplaceCommand('#media-library-add-form-wrapper', $this->buildMediaLibraryUi($form_state)));
            $response->addCommand(new FocusFirstCommand('#media-library-add-form-wrapper'));
        }
        else {
            $response->addCommand(new ReplaceCommand("#{$wrapper_id}", $form));
            // Find the delta of the next media item. If there is no item with a
            // bigger delta, we automatically use the delta of the previous item and
            // shift the focus there.
            $removed_delta = array_slice($triggering_element['#array_parents'], -2, 1)[0];
            $delta_to_focus = 0;
            foreach ($added_media as $delta => $media) {
                $delta_to_focus = $delta;
                if ($delta > $removed_delta) {
                    break;
                }
            }
            $response->addCommand(new InvokeCommand("[data-media-library-added-delta={$delta_to_focus}]", 'focus'));
        }
    }
    else {
        $response->addCommand(new ReplaceCommand("#{$wrapper_id}", $form));
        $response->addCommand(new InvokeCommand('.js-media-library-add-form-added-media', 'focus'));
    }
    return $response;
}

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