Skip to content

Instantly share code, notes, and snippets.

@johnennewdeeson
Last active May 23, 2019 13:32
Show Gist options
  • Save johnennewdeeson/22196a15b3ea115a2451 to your computer and use it in GitHub Desktop.
Save johnennewdeeson/22196a15b3ea115a2451 to your computer and use it in GitHub Desktop.
Make alt text required when uploading images using media widget (Drupal)
/**
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_file_entity_add_upload_alter(&$form) {
_mymodule_media_form_alter($form);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_file_entity_edit_alter(&$form) {
_mymodule_media_form_alter($form);
}
/**
* Alter the title and description of the alt text field and make it required.
*/
function _mymodule_media_form_alter(&$form) {
$form['field_file_image_alt_text'][LANGUAGE_NONE][0]['value']['#required'] = 1;
$form['field_file_image_alt_text'][LANGUAGE_NONE][0]['value']['#title'] = t('File description (alt text)');
$form['field_file_image_alt_text'][LANGUAGE_NONE][0]['value']['#description'] = t('A useful description of the file so it can be found again and is also required for accessibility');
}
@seamuscampbell
Copy link

In which file(s) would we have to put this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment