You are here

function content_add_more_js in Content Construction Kit (CCK) 6

Same name and namespace in other branches
  1. 6.3 includes/content.node_form.inc \content_add_more_js()
  2. 6.2 includes/content.node_form.inc \content_add_more_js()

Menu callback for AHAH addition of new empty widgets.

1 string reference to 'content_add_more_js'
content_menu in ./content.module
Implementation of hook_menu().

File

includes/content.node_form.inc, line 241

Code

function content_add_more_js($type_name_url, $field_name) {
  $type = content_types($type_name_url);
  $field = content_fields($field_name, $type['type']);
  if ($field['multiple'] != 1) {

    // TODO : is that correct ?
    drupal_json(array(
      'status' => 0,
    ));
  }

  // Reorder values to account for drag-n-drop reordering
  $_POST[$field_name] = _content_sort_items($field, $_POST[$field_name]);
  $delta = max(array_keys($_POST[$field_name])) + 1;

  // Retrieve the cached form.
  $form_state = array(
    'values' => $_POST,
  );
  $form = form_get_cache($_POST['form_build_id'], $form_state);

  // Build our new form element for the whole field,
  // and let other modules alter it.
  $form_element = content_field_form($form, $form_state, $field);
  drupal_alter('form', $form_element, array(), 'content_add_more_js');

  // Add the new element at the right place in the form.
  if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type['type'], $field_name))) {
    $form[$group_name][$field_name] = $form_element[$field_name];
  }
  else {
    $form[$field_name] = $form_element[$field_name];
  }

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

  // Build the new form so that we can render the new element.
  $_POST[$field_name][$delta]['_weight'] = $delta;

  // TODO : Hack !!
  $form_state = array(
    'submitted' => FALSE,
  );
  $form += array(
    '#post' => $_POST,
    '#programmed' => FALSE,
  );
  $form = form_builder($_POST['form_id'], $form, $form_state);

  // TODO : bug - the new element gets an empty delta value
  // Render the new output.
  $field_form = !empty($group_name) ? $form[$group_name][$field_name] : $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>';
  $output = theme('status_messages') . drupal_render($field_form);
  drupal_json(array(
    'status' => TRUE,
    'data' => $output,
  ));
}