2

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
  • What do you mean by making it a select field instead? Do you want to have some sort of predefined values and let the users choose one? Commented Nov 6, 2012 at 7:30

1 Answer 1

1

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.