function MediaLibraryFieldWidgetOpener::getSelectionResponse

Same name in other branches
  1. 8.9.x core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php \Drupal\media_library\MediaLibraryFieldWidgetOpener::getSelectionResponse()
  2. 10 core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php \Drupal\media_library\MediaLibraryFieldWidgetOpener::getSelectionResponse()
  3. 11.x core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php \Drupal\media_library\MediaLibraryFieldWidgetOpener::getSelectionResponse()

Overrides MediaLibraryOpenerInterface::getSelectionResponse

File

core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php, line 119

Class

MediaLibraryFieldWidgetOpener
The media library opener for field widgets.

Namespace

Drupal\media_library

Code

public function getSelectionResponse(MediaLibraryState $state, array $selected_ids) {
    $response = new AjaxResponse();
    $parameters = $state->getOpenerParameters();
    if (empty($parameters['field_widget_id'])) {
        throw new \InvalidArgumentException('field_widget_id parameter is missing.');
    }
    // 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("[data-media-library-widget-value=\"{$widget_id}\"]", 'val', [
        $ids,
    ]))
        ->addCommand(new InvokeCommand("[data-media-library-widget-update=\"{$widget_id}\"]", 'trigger', [
        'mousedown',
    ]));
    return $response;
}

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