$response =
new AjaxResponse();
$response->
addCommand(new UpdateSelectionCommand($media_ids));
$media_id_to_focus =
array_pop($media_ids);
$response->
addCommand(new ReplaceCommand('#media-library-add-form-wrapper',
$this->
buildMediaLibraryUi($form_state)));
$response->
addCommand(new InvokeCommand("#media-library-content [value=
$media_id_to_focus]", 'focus'
));
$available_slots =
$this->
getMediaLibraryState($form_state)->
getAvailableSlots();
if ($available_slots > 0 &&
$selected_count >
$available_slots) { $warning =
$this->
formatPlural($selected_count -
$available_slots, 'There are currently @total items selected. The maximum number of items for the field is @max. Please remove @count item from the selection.', 'There are currently @total items selected. The maximum number of items for the field is @max. Please remove @count items from the selection.',
[ '@total' =>
$selected_count,
'@max' =>
$available_slots,
]);
$response->
addCommand(new MessageCommand($warning, '#media-library-messages',
['type' => 'warning'
]));
} return $response;
} /**
* Build the render array of the media library UI.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
*
* @return array
* The render array for the media library.
*/