You are here

function slickgrid_filefield_js in Slickgrid 6

Copy of filefield_js() using slickgrid_json() rather than drupal_to_js()

Parameters

unknown_type $type_name:

unknown_type $field_name:

unknown_type $delta:

1 string reference to 'slickgrid_filefield_js'
slickgrid_menu_alter in ./slickgrid.module
Implementation of hook_menu_alter().

File

./slickgrid.ahah.inc, line 121

Code

function slickgrid_filefield_js($type_name, $field_name, $delta) {
  module_load_include('inc', 'node', 'node.pages');
  module_load_include('inc', 'content', 'includes/content.node_form');
  $field = content_fields($field_name, $type_name);
  if (empty($field) || empty($_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');
    print slickgrid_json(array(
      'data' => theme('status_messages'),
    ));
    exit;
  }

  // Build the new form.
  $form_state = array(
    'submitted' => FALSE,
  );
  $form_build_id = $_POST['form_build_id'];
  $form = form_get_cache($form_build_id, $form_state);
  if (!$form) {

    // Invalid form_build_id.
    drupal_set_message(t('An unrecoverable error occurred. This form was missing from the server cache. Try reloading the page and submitting again.'), 'error');
    print slickgrid_json(array(
      'data' => theme('status_messages'),
    ));
    exit;
  }

  // Build the form. This calls the file field's #value_callback function and
  // saves the uploaded file. Since this form is already marked as cached
  // (the #cache property is TRUE), the cache is updated automatically and we
  // don't need to call form_set_cache().
  $args = $form['#parameters'];
  $form_id = array_shift($args);
  $form['#post'] = $_POST;
  $form = form_builder($form_id, $form, $form_state);

  // Update the cached form with the new element at the right place in the form.
  if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type_name, $field_name))) {
    if (isset($form['#multigroups']) && isset($form['#multigroups'][$group_name][$field_name])) {
      $form_element = $form[$group_name][$delta][$field_name];
    }
    else {
      $form_element = $form[$group_name][$field_name][$delta];
    }
  }
  else {
    $form_element = $form[$field_name][$delta];
  }
  if (isset($form_element['_weight'])) {
    unset($form_element['_weight']);
  }
  $output = drupal_render($form_element);

  // AHAH is not being nice to us and doesn't know the "other" button (that is,
  // either "Upload" or "Delete") yet. Which in turn causes it not to attach
  // AHAH behaviours after replacing the element. So we need to tell it first.
  // Loop through the JS settings and find the settings needed for our buttons.
  $javascript = drupal_add_js(NULL, NULL);
  $filefield_ahah_settings = array();
  if (isset($javascript['setting'])) {
    foreach ($javascript['setting'] as $settings) {
      if (isset($settings['ahah'])) {
        foreach ($settings['ahah'] as $id => $ahah_settings) {
          if (strpos($id, 'filefield-upload') || strpos($id, 'filefield-remove')) {
            $filefield_ahah_settings[$id] = $ahah_settings;
          }
        }
      }
    }
  }

  // Add the AHAH settings needed for our new buttons.
  if (!empty($filefield_ahah_settings)) {
    $output .= '<script type="text/javascript">jQuery.extend(Drupal.settings.ahah, ' . drupal_to_js($filefield_ahah_settings) . ');</script>';
  }
  $output = theme('status_messages') . $output;

  // For some reason, file uploads don't like drupal_json() with its manual
  // setting of the text/javascript HTTP header. So use this one instead.
  $GLOBALS['devel_shutdown'] = FALSE;

  // Use slickgrid_json() if removing a file, drupal_to_js() if adding
  if ($_POST[$form_element['filefield_remove']['#name']]) {
    slickgrid_json(array(
      'status' => TRUE,
      'data' => $output,
    ));
  }
  else {
    print drupal_to_js(array(
      'status' => TRUE,
      'data' => $output,
    ));
  }
  exit;
}