You are here

function media_file_value in D7 Media 7.3

Same name and namespace in other branches
  1. 7.4 media.module \media_file_value()
  2. 7.2 media.module \media_file_value()

The #value_callback for a media type element.

1 call to media_file_value()
media_field_widget_value in includes/media.fields.inc
The #value_callback for the media field element.
1 string reference to 'media_file_value'
media_element_info in ./media.module
Implements hook_element_info().

File

./media.module, line 892
Media API

Code

function media_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 attach files.
  if ($input !== FALSE) {
    $return = $input;

    // Attachments take priority over all other values.
    if ($file = media_attach_file($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;
}