Render API callback: Determines the value for a managed_file type element.
This function is assigned as a #value_callback in file_element_info().
function file_managed_file_value(&$element, $input = FALSE, $form_state = NULL) {
// Find the current value of this field.
$fids = !empty($input['fids']) ? explode(' ', $input['fids']) : array();
foreach ($fids as $key => $fid) {
$fids[$key] = (int) $fid;
}
// Process any input and save new uploads.
if ($input !== FALSE) {
$input['fids'] = $fids;
$return = $input;
// Uploads take priority over all other values.
if ($files = file_managed_file_save_upload($element)) {
if ($element['#multiple']) {
$fids = array_merge($fids, array_keys($files));
}
else {
$fids = array_keys($files);
}
}
else {
// Check for #filefield_value_callback values.
// Because FAPI does not allow multiple #value_callback values like it
// does for #element_validate and #process, this fills the missing
// functionality to allow File fields to be extended through FAPI.
if (isset($element['#file_value_callbacks'])) {
foreach ($element['#file_value_callbacks'] as $callback) {
$callback($element, $input, $form_state);
}
}
// Load files if the FIDs have changed to confirm they exist.
if (!empty($input['fids'])) {
$fids = array();
foreach ($input['fids'] as $key => $fid) {
if ($file = file_load($fid)) {
$fids[] = $file->fid;
}
}
}
}
}
else {
if ($element['#extended']) {
$default_fids = isset($element['#default_value']['fids']) ? $element['#default_value']['fids'] : array();
$return = isset($element['#default_value']) ? $element['#default_value'] : array(
'fids' => array(),
);
}
else {
$default_fids = isset($element['#default_value']) ? $element['#default_value'] : array();
$return = array(
'fids' => array(),
);
}
// Confirm that the file exists when used as a default value.
if (!empty($default_fids)) {
$fids = array();
foreach ($default_fids as $key => $fid) {
if ($file = file_load($fid)) {
$fids[] = $file->fid;
}
}
}
}
$return['fids'] = $fids;
return $return;
}