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'
));
} // When there are still more items, update the form and shift the focus to
// the next media item. If the last list item is removed, shift focus to
// the previous item.
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.