| 1 file.module | file_managed_file_value(&$element, $input = FALSE, $form_state = NULL) |
Render API callback: Determines the value for a managed_file type element.
This function is assigned as a #value_callback in file_element_info().
File
- core/
modules/ file/ file.module, line 1515 - Defines a "managed_file" Form API field and a "file" field for Field module.
Code
function file_managed_file_value(&$element, $input = FALSE, $form_state = NULL) {
// Find the current value of this field. The FID property can be a single
// File ID, or multiple file IDs, which are separated by a space.
$force_default = FALSE;
// Drill down the form state to find the current element value.
$form_state_fid = $form_state['values'];
foreach ($element['#parents'] as $parent) {
$form_state_fid = isset($form_state_fid[$parent]) ? $form_state_fid[$parent] : array();
}
$fid = NULL;
if ($element['#extended'] && isset($form_state_fid['fid'])) {
$fid = $form_state_fid['fid'];
}
elseif (!empty($form_state_fid)) {
$fid = $form_state_fid;
}
// Convert the FID value to an array of FIDs.
if ($element['#multiple'] && is_string($fid)) {
$fids = explode(',', $fid);
}
else {
$fids = !empty($fid) ? (array) $fid : array();
}
// Process any input and save new uploads.
if ($input !== FALSE && isset($input['fid'])) {
$return = $input;
// Uploads take priority over all other values.
if ($file_data = file_managed_file_save_upload($element)) {
if (is_array($file_data)) {
$fids = array_keys($file_data);
}
else {
$fids = array($file_data->fid);
}
}
// Check for #file_value_callback values.
// Because Form API 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 Form API.
if (isset($element['#file_value_callbacks'])) {
foreach ($element['#file_value_callbacks'] as $callback) {
$callback($element, $input, $form_state);
}
}
// If a FID was submitted, load the file (and check access if it's not a
// public file) to confirm it exists and that the current user has access
// to it.
// Parse the input FID value, which may be a comma-separated string for
// multiple file elements.
if (!empty($input['fid'])) {
if ($element['#multiple'] && is_string($input['fid'])) {
$input_fids = array_filter(explode(',', $input['fid']));
}
else {
$input_fids = (array) $input['fid'];
}
}
else {
$input_fids = array();
}
if (!empty($input_fids) && ($files = file_load_multiple($input_fids))) {
foreach ($files as $file) {
if ((file_uri_scheme($file->uri) != 'public') && !file_download_access($file->uri)) {
$force_default = TRUE;
}
// Temporary files that belong to other users should never be allowed.
elseif ($file->status != FILE_STATUS_PERMANENT) {
if ($GLOBALS['user']->uid && $file->uid != $GLOBALS['user']->uid) {
$force_default = TRUE;
}
// Since file ownership can't be determined for anonymous users, they
// are not allowed to reuse temporary files at all. But they do need
// to be able to reuse their own files from earlier submissions of
// the same form, so to allow that, check for the token added by
// file_managed_file_process().
elseif (!$GLOBALS['user']->uid) {
$token = backdrop_array_get_nested_value($form_state['input'], array_merge($element['#parents'], array(
'file_' . $file->fid,
'fid_token',
)));
if ($token !== backdrop_hmac_base64('file-' . $file->fid, backdrop_get_private_key() . backdrop_get_hash_salt())) {
$force_default = TRUE;
}
}
}
// If all checks pass, allow the file to be changed.
if (!$force_default) {
$fids[] = $file->fid;
}
}
}
}
// If there is no input or if the default value was requested above, use the
// default value.
if ($input === FALSE || $force_default) {
if ($element['#extended']) {
$default_fid = $element['#default_value']['fid'] array();
$return = isset($element['#default_value']) ? $element['#default_value'] : array();
}
else {
$default_fid = $element['#default_value'] '';
$return = array();
}
// Confirm that the file exists when used as a default value.
if (!empty($default_fid)) {
$default_fids = is_array($default_fid) ? $default_fid : array($default_fid);
foreach ($default_fids as $default_fid) {
if ($file = file_load($default_fid)) {
$fids[] = $file->fid;
}
}
}
}
$fids = array_unique($fids);
if ($element['#multiple']) {
$return['fid'] = $fids;
}
else {
$return['fid'] = $fids ? reset($fids) : NULL;
}
return $return;
}