Making Drupal's FileField output HTML5 compliant

136 views Asked by At

I'm trying to remove the "size" attribute from Drupal's FileField output. Right now, it outputs a tag like this:

 <input type="file" name="files[image]"  class="form-file" id="edit-image" size="40" />

I tried a number of permutations of using unset() in a #pre_render callback in my custom module, but I just wind up with size="". The attribute itself never goes away, so HTML5 validators continue to complain. Is there another way to accomplish this, or a way to truly ensure that your callback runs last? $form['mystuff'][] = 'mycallback' is not doing the trick, and plain old unset($form['mystuff']['#size']) runs far too late.

1

There are 1 answers

2
SpaceBeers On

Have you tried hook_form_alter()?

function YOUR_THEME_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'YOUR_FORM_ID') { // Selects the form you want        
    $form['YOUR_FIELD_NAME']['#size'] = NULL;
  } 
}

With this I've been able to remove size and add HTML5 bits like placeholders etc:

$form['name']['#attributes'] = array('placeholder' => t('username'));