You are here

public function MediaLibraryFormElementOpener::getSelectionResponse in Media Library Form API Element 8

Same name and namespace in other branches
  1. 2.x src/MediaLibraryFormElementOpener.php \Drupal\media_library_form_element\MediaLibraryFormElementOpener::getSelectionResponse()

Generates a response after selecting media items in the media library.

Parameters

\Drupal\media_library\MediaLibraryState $state: The state the media library was in at the time of selection, allowing the response to be customized based on that state.

int[] $selected_ids: The IDs of the selected media items.

Return value

\Drupal\Core\Ajax\AjaxResponse The response to update the page after selecting media.

Overrides MediaLibraryOpenerInterface::getSelectionResponse

File

src/MediaLibraryFormElementOpener.php, line 54

Class

MediaLibraryFormElementOpener
The media library opener for form elements.

Namespace

Drupal\media_library_form_element

Code

public function getSelectionResponse(MediaLibraryState $state, array $selected_ids) {
  $response = new AjaxResponse();
  $parameters = $state
    ->getOpenerParameters();

  // Create a comma-separated list of media IDs, insert them in the hidden
  // field of the widget, and trigger the field update via the hidden submit
  // button.
  $widget_id = $parameters['field_widget_id'];
  $ids = implode(',', $selected_ids);
  $response
    ->addCommand(new InvokeCommand(NULL, 'setMediaUploadFieldValue', [
    $ids,
    "[data-media-library-form-element-value=\"{$widget_id}\"]",
  ]))
    ->addCommand(new InvokeCommand("[data-media-library-form-element-update=\"{$widget_id}\"]", 'trigger', [
    'mousedown',
  ]))
    ->addCommand(new CloseModalDialogCommand(TRUE, '#modal-media-library'));
  return $response;
}