You are here

function library_item_js in Library 6

Same name and namespace in other branches
  1. 5.2 library.module \library_item_js()
  2. 6.2 library.module \library_item_js()

Menu callback for AHAH additions.

1 string reference to 'library_item_js'
library_menu in ./library.module
Implementation of hook_menu().

File

./library.module, line 588

Code

function library_item_js() {
  $delta = count($_POST['items']);
  $items = $_POST['items'];

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

  // Add the new element to the stored form. Without adding the element to the
  // form, Drupal is not aware of this new elements existence and will not
  // process it. We retreive the cached form, add the element, and resave.
  $form = form_get_cache($form_build_id, $form_state);
  unset($form['item_wrapper']['items'], $form['item_wrapper']['library_more']);
  foreach ($items as $key => $item) {
    $form['item_wrapper']['items'][$key] = _library_item_form($key, $item);
  }
  $form['item_wrapper']['items'][$delta] = _library_item_form($delta);
  form_set_cache($form_build_id, $form, $form_state);
  $form += array(
    '#post' => $_POST,
    '#programmed' => FALSE,
  );

  // Rebuild the form.
  $form = form_builder($form['type']['#value'] . '_node_form', $form, $form_state);

  // Render the new output.
  $item_form = $form['item_wrapper']['items'];
  unset($item_form['#prefix'], $item_form['#suffix']);

  // Prevent duplicate wrappers.
  $item_form['#theme'] = 'library_items_field';
  $item_form[$delta]['#attributes']['class'] = empty($item_form[$delta]['#attributes']['class']) ? 'ahah-new-content' : $item_form[$delta]['#attributes']['class'] . ' ahah-new-content';
  $output = theme('status_messages') . drupal_render($item_form);
  drupal_json(array(
    'status' => TRUE,
    'data' => $output,
  ));
}