In Drupal 7, I need users to enter a file description, how do I make this field required? Also on another note, I would love to make this field a select list, there's no clear way to do this aside writing a custom module. Any ideas?
1 Answer
Code snippet for making a file's description field required:
/**
* Implements hook_field_widget_form_alter().
*/
function MYMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
if ($context['field']['field_name'] == 'field_MYFIELD' && $context['instance']['widget']['type'] == 'file_generic') {
$element_children = element_children($element, TRUE);
foreach($element_children as $key) {
$element[$key]['#process'][] = 'MYMODULE_file_field_description_required';
}
}
}
/**
* Require description to be set.
*/
function MYMODULE_file_field_description_required($element, &$form_state, $form) {
$element['description']['#required'] = TRUE;
return $element;
}