Render API callback: Expands the managed_file element type.
Expands the file type to include Upload and Remove buttons, as well as support for a default value.
This function is assigned as a #process callback in file_element_info().
function file_managed_file_process($element, &$form_state, $form) {
// Append the '-upload' to the #id so the field label's 'for' attribute
// corresponds with the file element.
$element['#id'] .= '-upload';
// This is used sometimes so let's implode it just once.
$parents_prefix = implode('_', $element['#parents']);
$fids = isset($element['#value']['fids']) ? $element['#value']['fids'] : array();
// Set some default element properties.
$element['#progress_indicator'] = empty($element['#progress_indicator']) ? 'none' : $element['#progress_indicator'];
$element['#files'] = !empty($fids) ? file_load_multiple($fids) : FALSE;
$element['#tree'] = TRUE;
$ajax_settings = array(
'path' => 'file/ajax/' . implode('/', $element['#array_parents']) . '/' . $form['form_build_id']['#value'],
'wrapper' => $element['#id'] . '-ajax-wrapper',
'effect' => 'fade',
'progress' => array(
'type' => $element['#progress_indicator'],
'message' => $element['#progress_message'],
),
);
// Set up the buttons first since we need to check if they were clicked.
$element['upload_button'] = array(
'#name' => $parents_prefix . '_upload_button',
'#type' => 'submit',
'#value' => t('Upload'),
'#validate' => array(),
'#submit' => array(
'file_managed_file_submit',
),
'#limit_validation_errors' => array(
$element['#parents'],
),
'#ajax' => $ajax_settings,
'#weight' => -5,
);
// Force the progress indicator for the remove button to be either 'none' or
// 'throbber', even if the upload button is using something else.
$ajax_settings['progress']['type'] = $element['#progress_indicator'] == 'none' ? 'none' : 'throbber';
$ajax_settings['progress']['message'] = NULL;
$ajax_settings['effect'] = 'none';
$element['remove_button'] = array(
'#name' => $parents_prefix . '_remove_button',
'#type' => 'submit',
'#value' => $element['#multiple'] ? t('Remove selected') : t('Remove'),
'#validate' => array(),
'#submit' => array(
'file_managed_file_submit',
),
'#limit_validation_errors' => array(
$element['#parents'],
),
'#ajax' => $ajax_settings,
'#weight' => 1,
);
$element['fids'] = array(
'#type' => 'hidden',
'#value' => $fids,
);
// Add progress bar support to the upload if possible.
if ($element['#progress_indicator'] == 'bar' && ($implementation = file_progress_implementation())) {
$upload_progress_key = mt_rand();
if ($implementation == 'uploadprogress') {
$element['UPLOAD_IDENTIFIER'] = array(
'#type' => 'hidden',
'#value' => $upload_progress_key,
'#attributes' => array(
'class' => array(
'file-progress',
),
),
// Uploadprogress extension requires this field to be at the top of the
// form.
'#weight' => -20,
);
}
elseif ($implementation == 'apc') {
$element['APC_UPLOAD_PROGRESS'] = array(
'#type' => 'hidden',
'#value' => $upload_progress_key,
'#attributes' => array(
'class' => array(
'file-progress',
),
),
// Uploadprogress extension requires this field to be at the top of the
// form.
'#weight' => -20,
);
}
// Add the upload progress callback.
$element['upload_button']['#ajax']['progress']['path'] = 'file/progress/' . $upload_progress_key;
}
// The file upload field itself.
$element['upload'] = array(
'#name' => 'files[' . $parents_prefix . ']',
'#type' => 'file',
'#title' => t('Choose a file'),
'#title_display' => 'invisible',
'#size' => $element['#size'],
'#multiple' => $element['#multiple'],
'#theme_wrappers' => array(),
'#weight' => -10,
);
if (!empty($fids) && $element['#files']) {
foreach ($element['#files'] as $delta => $file) {
if ($element['#multiple']) {
$element['file_' . $delta]['selected'] = array(
'#type' => 'checkbox',
'#title' => theme('file_link', array(
'file' => $file,
)) . ' ',
);
}
else {
$element['file_' . $delta]['filename'] = array(
'#type' => 'markup',
'#markup' => theme('file_link', array(
'file' => $file,
)) . ' ',
'#weight' => -10,
);
}
}
}
// Add the extension list to the page as JavaScript settings.
if (isset($element['#upload_validators']['file_validate_extensions'][0])) {
$extension_list = implode(',', array_filter(explode(' ', $element['#upload_validators']['file_validate_extensions'][0])));
$element['upload']['#attached']['js'] = array(
array(
'type' => 'setting',
'data' => array(
'file' => array(
'elements' => array(
'#' . $element['#id'] . '-upload' => $extension_list,
),
),
),
),
);
}
// Prefix and suffix used for Ajax replacement.
$element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">';
$element['#suffix'] = '</div>';
return $element;
}