I would like to change the label on the button named "Add media" on the media field widget. For instance to write "Add image" or "Add document". How can I change it?
1 Answer
During the form construction process, there are a bunch of times that Drupal allows your own modules to hook in and alter things like this.
Here's one, which you would implement in a custom module, that hooks in when the media library widget is being prepared and alters it everywhere it's used:
function MYMODULE_field_widget_media_library_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
$element["open_button"]["#value"] = t('Add image');
}
Here's another one that hooks in during the construction of any node form, which might be more appropriate if I only want to make this change for a particular field or entity type:
function MYMODULE_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if (isset($form["field_my_media"])) {
$form["field_my_media"]["widget"]["open_button"]["#value"] = t('Add image');
}
}