1

I'd like to add "accept" attributes to some of the input fields for my site's content types -- to end up with something like <input type="file" id="edit-field-whatever-und-0-upload" name="files[field_whatever_und_0]" size="22" class="form-file" accept="image/*">, so that (most) browsers will limit the selectable items to .jpg, .png, etc. I've tried doing this by adding an #attributes clause to a form's hook_form_alter handler, but haven't been able to find the right way to write the clause -- I can't get the accept clause to show up in the html.

In place of this, I'm currently adding it through some jQuery that runs on document.ready, but it would feel cleaner if I could do it through the forms api. Is this possible? Any hints out there? Thanks!

2

You can override theme_file to achieve above if you would like to alter file input element in template.php ..

function YOURTHEMENAME_file($variables) {
  $element = $variables['element'];
  $element['#attributes']['type'] = 'file';
  element_set_attributes($element, array('id', 'name', 'size'));
  _form_set_class($element, array('form-file'));

  return '<input' . drupal_attributes($element['#attributes']) . ' accept="image/*" />';
}

below code works for me

function seven_file($variables) {
  $element = $variables['element'];
  $element['#attributes']['type'] = 'file';
  element_set_attributes($element, array('id', 'name', 'size'));
  _form_set_class($element, array('form-file'));

  return '<input' . drupal_attributes($element['#attributes']) . ' accept="image/*" />';
}

enter image description here

  • Interesting/thanks, but I think it's not general enough -- some fields need to be restricted to image/*, while others (sometimes in the same content type) need to be restricted to video/*. But it looks like you're confirming (so far) that there's no clear way to do it with an #attributes clause... – Jim Miller Apr 14 '14 at 14:52
0

Old question but i wanted to solve this same issue and this led me to come up with a general solution to set the accept attribute for all drupal file and image widgets.

/**
* Implements hook_field_widget_form_alter().
*/
function MODULENAME_field_widget_form_alter(&$element, &$form_state, $context)
{
  switch ($context['field']['type'])
  {
    case 'file':
    case 'image':
    {
      // add custom file widget process callback
      $element[$context['delta']]['#process'][] = '_MODULENAME_file_process';
      break;
    }
  }
}

/**
* Custom element process callback for file widgets
**/
function _MODULENAME_file_process($element, &$form_state, $form)
{
  // set accept attribute from allowed extensions
  $exts_string = @$element['#upload_validators']['file_validate_extensions'][0];
  if ($exts_string)
  {
    $exts = explode(' ', $exts_string);
    $element['upload']['#attributes']['accept'] = '.' . implode(', .', $exts);
  }

  return $element;
}
0

There is now a module that does this: File Accept

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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