upload_js

Versions
4.7 – 7
upload_js()

Menu-callback for JavaScript-based uploads.

Code

modules/upload/upload.module, line 642

<?php
function upload_js() {
  $cached_form_state = array();
  $files = array();

  // Load the form from the Form API cache.
  if (!($cached_form = form_get_cache($_POST['form_build_id'], $cached_form_state)) || !isset($cached_form['#node']) || !isset($cached_form['attachments'])) {
    form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
    $output = theme('status_messages');
    print drupal_json_encode(array('status' => TRUE, 'data' => $output));
    drupal_exit();
  }

  $form_state = array('values' => $_POST);

  // Handle new uploads, and merge tmp files into node-files.
  upload_node_form_submit($cached_form, $form_state);

  if (!empty($form_state['values']['files'])) {
    foreach ($form_state['values']['files'] as $fid => $file) {
      if (isset($cached_form['#node']->files[$fid])) {
        $files[$fid] = $cached_form['#node']->files[$fid];
      }
    }
  }

  $node = $cached_form['#node'];

  $node->files = $files;

  $form = _upload_form($node, $form_state);

  unset($cached_form['attachments']['wrapper']['new']);
  $cached_form['attachments']['wrapper'] = array_merge($cached_form['attachments']['wrapper'], $form);

  $cached_form['attachments']['#collapsed'] = FALSE;

  form_set_cache($_POST['form_build_id'], $cached_form, $cached_form_state);

  foreach ($files as $fid => $file) {
    if (is_numeric($fid)) {
      $form['files'][$fid]['description']['#default_value'] = $form_state['values']['files'][$fid]['description'];
      $form['files'][$fid]['list']['#default_value'] = !empty($form_state['values']['files'][$fid]['list']);
      $form['files'][$fid]['remove']['#default_value'] = !empty($form_state['values']['files'][$fid]['remove']);
      $form['files'][$fid]['weight']['#default_value'] = $form_state['values']['files'][$fid]['weight'];
    }
  }

  // Render the form for output.
  $form += array(
    '#tree' => FALSE,
    '#parents' => array(),
  );
  $form_state = array('submitted' => FALSE, 'programmed' => FALSE, 'process_input' => FALSE, 'complete form' => FALSE);
  $form_id = 'upload_js';
  drupal_alter('form', $form, $form_state, $form_id);
  $form = form_builder('upload_js', $form, $form_state);
  $output = theme('status_messages') . drupal_render($form);

  $commands = array();
  $commands[] = ajax_command_replace(NULL, $output);

  // AJAX uploads use an <iframe> and some browsers have problems with the
  // 'text/javascript' Content-Type header with iframes. Passing FALSE to
  // ajax_render() prevents the header from being sent.
  ajax_render($commands, FALSE);
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.