You are here

function media_ajax_upload in D7 Media 7.2

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

Menu callback; Shared Ajax callback for media attachment 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.

1 string reference to 'media_ajax_upload'
media_menu in ./media.module
Implements hook_menu().

File

./media.module, line 247
Media API

Code

function media_ajax_upload() {
  $form_parents = func_get_args();
  $form_build_id = (string) array_pop($form_parents);

  // Sanitize form parents before using them.
  $form_parents = array_filter($form_parents, 'element_child');
  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');
    $commands = array();
    $commands[] = ajax_command_replace(NULL, theme('status_messages'));
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
  list($form, $form_state, $form_id, $form_build_id, $commands) = 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');
    $commands = array();
    $commands[] = ajax_command_replace(NULL, theme('status_messages'));
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }

  // Get the current element and count the number of files.
  $current_element = $form;
  foreach ($form_parents as $parent) {
    if (isset($current_element[$parent])) {
      $current_element = $current_element[$parent];
    }
    else {
      $current_element = NULL;
      break;
    }
  }
  $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>';
  }
  $output = theme('status_messages') . drupal_render($form);
  $js = drupal_add_js();
  $settings = call_user_func_array('array_merge_recursive', $js['settings']['data']);
  $commands[] = ajax_command_replace(NULL, $output, $settings);
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}