// Always clear the textfield selection to prevent duplicate additions.
$element['media_library_selection'
]['#value'
] = '';
$field_state =
static::
getFieldState($element,
$form_state);
// Announce the updated content to screen readers.
if ($is_remove_button) { $media_item = Media::
load($field_state['removed_item_id'
]);
$announcement =
$media_item->
access('view label'
) ?
new TranslatableMarkup('@label has been removed.',
['@label' =>
$media_item->
label()]) :
new TranslatableMarkup('Media has been removed.'
);
} else { $new_items =
count(static::
getNewMediaItems($element,
$form_state));
$announcement = \Drupal::
translation()->
formatPlural($new_items, 'Added one media item.', 'Added @count media items.'
);
} $response =
new AjaxResponse();
$response->
addCommand(new ReplaceCommand("#
$wrapper_id",
$element));
$response->
addCommand(new AnnounceCommand($announcement));
// When the remove button is clicked, shift focus to the next remove button.
// When the last item is deleted, we no longer have a selection and shift
// the focus to the open button.
$removed_last =
$is_remove_button && !
count($field_state['items'
]);