function managed_file_element_value in Managed File 7
Determine the value of managed file element.
@internal
Parameters
array $element: Form element.
array|bool $input: User input data.
array $form_state: Drupal form state.
Return value
string[]|int An array with "style" and "fid" keys or file ID.
See also
managed_file_element_info_alter()
1 string reference to 'managed_file_element_value'
- managed_file_element_info_alter in ./
managed_file.module - Implements hook_element_info_alter().
File
- includes/
managed_file.common.inc, line 150 - Auxiliary functionality.
Code
function managed_file_element_value(array &$element, $input, array $form_state) {
if (!isset($element['#default_value'])) {
$element['#default_value'] = 0;
}
if (!isset($element['#upload_location'])) {
$element['#upload_location'] = file_default_scheme() . '://';
}
$default_value_callback = $element['#default_value_callback'];
$default_value =& $element['#default_value'];
if ($element['#styles']) {
// When this property is TRUE, then $element will be processed as it
// have the #tree property with a TRUE value. In other words, we will
// have an array of values instead of file ID.
//
// @see file_element_info()
//
// @example
// @code
// [
// 'fid' => 1,
// 'style' => '',
// 'upload' => NULL,
// ]
// @endcode
$element['#extended'] = TRUE;
if (!is_array($default_value)) {
$default_value = [
'fid' => $default_value,
];
}
// Ensure that "style" property exists.
$default_value += [
'style' => '',
];
if (FALSE !== $input && isset($input['style'])) {
$default_value['style'] = $input['style'];
}
}
// When element is extended then the type of default value will be an array.
// In another case an array could be here from previous form submission or
// passed directly as default value.
$default_value_array = $element['#extended'] || is_array($default_value) && array_key_exists('fid', $default_value);
// Unable to use ternary operator and assignment by reference.
if ($default_value_array) {
$fid =& $default_value['fid'];
}
else {
$fid =& $default_value;
}
if (NULL === $fid) {
$fid = 0;
}
// Convert MD5 hash to file ID.
if (0 !== $fid && function_exists('file_md5_load_file')) {
$file = file_md5_load_file($fid);
if (FALSE !== $file) {
$fid = $file->fid;
}
}
// If user is input nothing then use default value.
if (FALSE === $input) {
return $default_value_array ? $default_value : [
'fid' => $default_value,
];
}
// Get value from a $form_state to replace MD5 by file ID.
$value =& drupal_array_get_nested_value($form_state['values'], $element['#parents']);
if (NULL !== $value && array_key_exists('fid', $input)) {
if (empty($input['fid'])) {
$fid = 0;
}
if (is_array($value)) {
$value['fid'] = $fid;
}
else {
$value = $fid;
}
}
return $default_value_callback($element, $input, $form_state);
}