managed_file form element doesn't render correctly

858 Views Asked by At

I have a simple managed file form element that is the only element in my form. It looks like this:

$form['upload'] = array(
    '#type' => 'managed_file',
    '#title' => t('Select a YML file'),
    '#progress_message' => t('Please wait...'),
    '#progress_indicator' => 'bar',
    '#description' => t('Click "Browse..." to select a file to upload.'),
    '#required' => TRUE,
    '#upload_validators' => array('file_validate_extensions' => array('yml txt docx')),
    '#upload_location' => $upload_dest,
);

When I render the form using the drupal_get_form callback in hook_menu I get a perfectly formed managed_file upload field with the browse and upload buttons. Things change when I decide I want to add a table of information underneath the form. This requires building a table using theme functions then adding that to the form by rendering the form and appending the table. I create my table and add it to the form:

$rows = array();
foreach($yml_files as $yml_file){
    $rows[] = array($yml_file->uri, $yml_file->filename);
}
$output = drupal_render($form['upload']);
$output .= theme('table', array('header'=>$header, 'rows'=>$rows));
return $output;

When I generate the form using drupal_render, I get the nice help text, but no upload form. The table renders fine in both scenarios and I'm not seeing any errors.

If Drupal uses drupal_render to render its forms why would the form look different in the second scnenario? Is there a way to get the entire form? I've tried a variety of ways of passing the form and using dpm to print the form at various stages and I'm not sure where to go from here.

Standard file upload fields render correctly as do other form elements. It seems to be limited to the managed_file element.

1

There are 1 best solutions below

2
Mike Vranckx On

When using a drupal_get_form menu callback, you should return your $form array and not an already rendered themeable array. Probably you are missing #attached js files for the managed_file field.

What you could do in your case is to add the table output on a markup field of your form.

$form['upload'] = array(
    '#type' => 'managed_file',
    '#title' => t('Select a YML file'),
    '#progress_message' => t('Please wait...'),
    '#progress_indicator' => 'bar',
    '#description' => t('Click "Browse..." to select a file to upload.'),
    '#required' => TRUE,
    '#upload_validators' => array('file_validate_extensions' => array('yml txt docx')),
    '#upload_location' => $upload_dest,
);

$form['table'] = array(
    '#markup' => theme('table', array('header' => $header, 'rows' => $rows)),
);

return $form;