function file_ajax_upload

Ajax callback: Processes file uploads and deletions.

This rebuilds the form element for a particular field item. As long as the form processing is properly encapsulated in the widget element the form should rebuild correctly using FAPI without the need for additional callbacks or processing.

See also

file_menu()

1 string reference to 'file_ajax_upload'
file_menu in drupal/core/modules/file/file.module
Implements hook_menu().

File

drupal/core/modules/file/file.module, line 736
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

function file_ajax_upload() {
  $form_parents = func_get_args();
  $form_build_id = (string) array_pop($form_parents);
  if (empty($_POST['form_build_id']) || $form_build_id != $_POST['form_build_id']) {

    // Invalid request.
    drupal_set_message(t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) that this server supports.', array(
      '@size' => format_size(file_upload_max_size()),
    )), 'error');
    $response = new AjaxResponse();
    return $response
      ->addCommand(new ReplaceCommand(NULL, theme('status_messages')));
  }
  list($form, $form_state) = ajax_get_form();
  if (!$form) {

    // Invalid form_build_id.
    drupal_set_message(t('An unrecoverable error occurred. Use of this form has expired. Try reloading the page and submitting again.'), 'error');
    $response = new AjaxResponse();
    return $response
      ->addCommand(new ReplaceCommand(NULL, theme('status_messages')));
  }

  // Get the current element and count the number of files.
  $current_element = $form;
  foreach ($form_parents as $parent) {
    $current_element = $current_element[$parent];
  }
  $current_file_count = isset($current_element['#file_upload_delta']) ? $current_element['#file_upload_delta'] : 0;

  // Process user input. $form and $form_state are modified in the process.
  drupal_process_form($form['#form_id'], $form, $form_state);

  // Retrieve the element to be rendered.
  foreach ($form_parents as $parent) {
    $form = $form[$parent];
  }

  // Add the special Ajax class if a new file was added.
  if (isset($form['#file_upload_delta']) && $current_file_count < $form['#file_upload_delta']) {
    $form[$current_file_count]['#attributes']['class'][] = 'ajax-new-content';
  }
  else {
    $form['#suffix'] .= '<span class="ajax-new-content"></span>';
  }
  $form['#prefix'] .= theme('status_messages');
  $output = drupal_render($form);
  $js = drupal_add_js();
  $settings = drupal_merge_js_settings($js['settings']['data']);
  $response = new AjaxResponse();
  return $response
    ->addCommand(new ReplaceCommand(NULL, $output, $settings));
}