1

I'm trying to do a very simple thing (seems not that simple at all) in my custom form, to add multiple files using the managed_file form api. Like in the admin, select a file, upload, then a new input file appears.

But I can't achieve that... do I need a callback ???

    'gallery' => array(
        '#type' => 'managed_file',  
        '#name' => 'gallery[]',
        '#title' => t('Gallery'),
        '#upload_location' => 'public://',
        '#attributes' => array(
            'multiple' => 'multiple',
        ),
    ),

Thanks

2
  • Could someone also provide a Drupal 7 answer? Aug 30, 2017 at 18:59
  • For drupla7 below code will work $form['file'] = array( '#type' => 'managed_file', '#name' => 'files[]', '#title' => t('Upload some photos'), '#attributes' => array('multiple' => 'multiple'), );
    – sivani
    Jun 6, 2019 at 12:47

2 Answers 2

7

In Drupal 8, add '#multiple' => TRUE

Example:

$form['images'] = array(
  '#type' => 'managed_file',
  '#upload_location' => 'private://images/',
  '#multiple' => TRUE,
  '#upload_validators' => array(
    'file_validate_extensions' => array('png gif jpg jpeg'),
    'file_validate_size' => array(25600000),
    'file_validate_image_resolution' => array('800x600', '400x300'),
  ),
);

More about form api: Introduction to Form API

-1

Complete details of upload multiple images using from API

/**
 * our example system settings form
 */
function mf_settings_form($form,&$form_state) {
  $form = array();

  $form['managed_file_example'] = array(
    '#type' => 'managed_file',
    '#title' => t('Managed File Example'),
    '#default_value' => variable_get('managed_file_example',''),
    '#upload_location' => 'public://managed_file_example/',
  );

  $form = system_settings_form($form);
  $form['#submit'][] = 'mf_settings_submit';

  return $form;
}

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.