drupal-7form-api

Multiple file upload in custom form with managed_file


I'm building a custom settings form in Drupal 7, with an image upload field. This image field should allow multiple uploads.

After some research I found out you can do this with managed_file and '#attributes' => array('multiple' => 'multiple'). However this doesn't seem to do anything.

This is the code I currently have:

$form['frontpage_banner_images'] = array(
  '#type' => 'managed_file',
  '#title' => t('Frontpage Images'),
  '#name' => 'files[]',
  '#attributes' => array(
    'multiple' => 'multiple',
    'class' => 'testclass',
  ),
  '#upload_location' => 'public://homepage-banners/',
  '#default_value' => variable_get('frontpage_banner_images'),
);

With this as result:

<div class="form-item form-type-managed-file form-item-files-">
  <label for="edit-frontpage-banner-images-upload">Frontpage Images</label>
  <div id="edit-frontpage-banner-images-upload" class="testclass form-managed-file">
    <input type="file" id="edit-frontpage-banner-images-upload" name="files[frontpage_banner_images]" size="22" class="form-file">
    <input type="submit" id="edit-frontpage-banner-images-upload-button" name="frontpage_banner_images_upload_button" value="Upload" class="form-submit ajax-processed">
    <input type="hidden" name="frontpage_banner_images[fid]" value="0">
  </div>
</div>

As you can see, the testclass from my #attributes is being applied on the wrapping div and not on the file input. So the multiple attribute isn't doing anything.

This is what I'm trying to achieve (Photoshopped):

enter image description here

Any help on how to achieve this is appreciated.


Solution

  • The multiple option does not seem to be supported. Then again you are using it under the #attributes. This would cause IMO the html element to have an atttribute multiple, but I don't see anything in /modules/file/file.js to pick up on that, so that's presumably why it's not doing anything. You're probably better off using plupload, as suggested here.