You are here

function mfw_managed_file_value in Multiupload Filefield Widget 7

The #value_callback for a mfw_managed_file type element.

Mostly copied from file.module.

1 call to mfw_managed_file_value()
mfw_field_widget_value in ./multiupload_filefield_widget.field.inc
The #value_callback for the file_mfw field element.
1 string reference to 'mfw_managed_file_value'
multiupload_filefield_widget_element_info in ./multiupload_filefield_widget.module
Implements hook_element_info().

File

./multiupload_filefield_widget.module, line 60

Code

function mfw_managed_file_value(&$element, $input = FALSE, $form_state = NULL) {
  $fid = 0;

  // Find the current value of this field from the form state.
  $form_state_fid = $form_state['values'];
  foreach ($element['#parents'] as $parent) {
    $form_state_fid = isset($form_state_fid[$parent]) ? $form_state_fid[$parent] : 0;
  }
  if ($element['#extended'] && isset($form_state_fid['fid'])) {
    $fid = $form_state_fid['fid'];
  }
  elseif (is_numeric($form_state_fid)) {
    $fid = $form_state_fid;
  }

  // Process any input and save new uploads.
  if ($input !== FALSE) {
    $return = $input;
    $element_parent = drupal_array_get_nested_value($form_state['complete form'], array_slice($element['#parents'], 0, -1));
    $element['#file_upload_delta_original'] = isset($element_parent['#file_upload_delta']) ? $element_parent['#file_upload_delta'] : 0;

    // Uploads take priority over all other values.
    if ($file = mfw_managed_file_save_upload($element)) {
      $fid = $file->fid;
    }
    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 file if the FID has changed to confirm it exists.
      if (isset($input['fid']) && ($file = file_load($input['fid']))) {
        $fid = $file->fid;
      }
    }
  }
  else {
    if ($element['#extended']) {
      $default_fid = isset($element['#default_value']['fid']) ? $element['#default_value']['fid'] : 0;
      $return = isset($element['#default_value']) ? $element['#default_value'] : array(
        'fid' => 0,
      );
    }
    else {
      $default_fid = isset($element['#default_value']) ? $element['#default_value'] : 0;
      $return = array(
        'fid' => 0,
      );
    }

    // Confirm that the file exists when used as a default value.
    if ($default_fid && ($file = file_load($default_fid))) {
      $fid = $file->fid;
    }
  }
  $return['fid'] = $fid;
  return $return;
}