You are here

function node_gallery_content_add_more_js in Node Gallery 6.3

Menu callback for AHAH addition of new empty widgets, modeled after content_add_more_js() in content.node_form.inc.

See also

content_add_more_js()

1 string reference to 'node_gallery_content_add_more_js'
node_gallery_menu in ./node_gallery.module
Implements hook_menu().

File

./node_gallery.pages.inc, line 562
Handles displaying of Node gallery pages.

Code

function node_gallery_content_add_more_js($type_name_url, $field_name) {
  module_load_include('inc', 'content', 'includes/content.node_form');
  $type = content_types($type_name_url);
  $field = content_fields($field_name, $type['type']);
  if ($field['multiple'] != 1 || empty($_POST['form_build_id'])) {

    // Invalid request.
    drupal_json(array(
      'data' => '',
    ));
    exit;
  }

  // Retrieve the cached 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_json(array(
      'data' => '',
    ));
    exit;
  }
  $form_copy = $form;
  $form_state_copy = $form_state;
  $form_copy['#post'] = array();
  form_builder($_POST['form_id'], $form_copy, $form_state_copy);

  // Just grab the data we need.
  $form_state['values'] = $form_state_copy['values'];

  // Reset cached ids, so that they don't affect the actual form we output.
  form_clean_id(NULL, TRUE);

  // find out which button was pressed
  $button_name = FALSE;
  $nid = 0;
  foreach ($_POST['images'] as $cur_nid => $image) {
    if (isset($_POST[$field_name . '_add_more-' . $cur_nid])) {
      $button_name = $field_name . '_add_more-' . $cur_nid;
      $nid = $cur_nid;
    }
  }
  if (!$button_name) {
    drupal_json(array(
      'data' => '',
    ));
    exit;
  }

  // unset button
  unset($form_state['values']['images'][$nid]['edit_form'][$field_name][$field_name . '_add_more-' . $nid]);

  // now simulate the submission of just this image edit form
  $original_form_state = $form_state;
  $original_post = $_POST;
  $original_form = $form;
  $_POST = $_POST['images'][$nid]['edit_form'];
  $form_state['values'] = $form_state['values']['images'][$nid]['edit_form'];

  // iterate current values
  foreach ($_POST[$field_name] as $delta => $item) {
    $form_state['values'][$field_name][$delta]['_weight'] = $item['_weight'];
  }
  $form_state['values'][$field_name] = _content_sort_items($field, $form_state['values'][$field_name]);
  $_POST[$field_name] = _content_sort_items($field, $_POST[$field_name]);

  // Build our new form element for the whole field, asking for one more element.
  $form_state['item_count'] = array(
    $field_name => count($_POST[$field_name]) + 1,
  );
  $form_element = content_field_form($form, $form_state, $field);

  // Make the same modifications as in building the manage images form!
  $field_name_css = str_replace('_', '-', $field_name);
  $form_element[$field_name]['#prefix'] = '<div id="' . $field_name_css . '-items-' . $nid . '">';
  $form_element[$field_name][$field_name . '_add_more-' . $nid] = $form_element[$field_name][$field_name . '_add_more'];
  $form_element[$field_name][$field_name . '_add_more-' . $nid]['#ahah']['wrapper'] = $field_name_css . '-items-' . $nid;
  $form_element[$field_name][$field_name . '_add_more-' . $nid]['#ahah']['path'] = 'node-gallery/json/js_add_more/' . $form['#node']->type . '/' . $field_name;
  $form_element[$field_name][$field_name . '_add_more-' . $nid]['#name'] = $field_name . '_add_more-' . $nid;
  unset($form_element[$field_name][$field_name . '_add_more']);
  $form_element[$field_name]['#theme'] = 'node_gallery_content_multiple_values';
  $form['images'][$nid]['edit_form'][$field_name] = $form_element[$field_name];

  // merge it again
  $form_state = $original_form_state;
  $form_state['values']['images'][$nid]['edit_form'] = array();

  // Save the new definition of the form.
  form_set_cache($form_build_id, $form, $form_state);

  // Build the new form against the incoming $_POST values so that we can
  // render the new element.
  $delta = max(array_keys($_POST[$field_name])) + 1;
  $_POST[$field_name][$delta]['_weight'] = $delta;
  $tmp = $_POST;
  $_POST = $original_post;
  $_POST['images'][$nid]['edit_form'][$field_name] = $tmp[$field_name];
  $form_state = array(
    'submitted' => FALSE,
  );
  $form += array(
    '#post' => $_POST,
    '#programmed' => FALSE,
  );
  $form = form_builder($_POST['form_id'], $form, $form_state);

  // Render the new output.
  $field_form = $form['images'][$nid]['edit_form'][$field_name];

  // We add a div around the new content to receive the ahah effect.
  $field_form[$delta]['#prefix'] = '<div class="ahah-new-content">' . (isset($field_form[$delta]['#prefix']) ? $field_form[$delta]['#prefix'] : '');
  $field_form[$delta]['#suffix'] = (isset($field_form[$delta]['#suffix']) ? $field_form[$delta]['#suffix'] : '') . '</div>';

  // Prevent duplicate wrapper.
  unset($field_form['#prefix'], $field_form['#suffix']);
  $javascript = drupal_add_js(NULL, NULL);
  $output_js = isset($javascript['setting']) ? '<script type="text/javascript">jQuery.extend(Drupal.settings, ' . drupal_to_js(call_user_func_array('array_merge_recursive', $javascript['setting'])) . ');</script>' : '';
  $output = theme('status_messages') . drupal_render($field_form) . $output_js;
  $GLOBALS['devel_shutdown'] = FALSE;
  print drupal_to_js(array(
    'status' => TRUE,
    'data' => $output,
  ));
  exit;
}