You are here

public static function MediaLibrarySelectForm::updateWidget in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php \Drupal\media_library\Plugin\views\field\MediaLibrarySelectForm::updateWidget()

Submit handler for the media library select form.

Parameters

array $form: An associative array containing the structure of the form.

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

\Symfony\Component\HttpFoundation\Request $request: The current request.

Return value

\Drupal\Core\Ajax\AjaxResponse A command to send the selection to the current field widget.

File

core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php, line 116

Class

MediaLibrarySelectForm
Defines a field that outputs a checkbox and form for selecting media.

Namespace

Drupal\media_library\Plugin\views\field

Code

public static function updateWidget(array &$form, FormStateInterface $form_state, Request $request) {
  $field_id = $form_state
    ->getTriggeringElement()['#field_id'];
  $selected_ids = $form_state
    ->getValue($field_id);
  $selected_ids = $selected_ids ? array_filter(explode(',', $selected_ids)) : [];

  // Allow the opener service to handle the selection.
  $state = MediaLibraryState::fromRequest($request);
  return \Drupal::service('media_library.opener_resolver')
    ->get($state)
    ->getSelectionResponse($state, $selected_ids)
    ->addCommand(new CloseDialogCommand());
}