function file_managed_file_value

Render API callback: Determines the value for a managed_file type element.

This function is assigned as a #value_callback in file_element_info().

1 call to file_managed_file_value()
file_field_widget_value in drupal/core/modules/file/file.field.inc
Render API callback: Retrieves the value for the file_generic field element.
1 string reference to 'file_managed_file_value'
file_element_info in drupal/core/modules/file/file.module
Implements hook_element_info().

File

drupal/core/modules/file/file.module, line 999
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.
  $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;
}