You are here

function bat_options_remove_js in Booking and Availability Management Tools for Drupal 7

Page callback to handle AJAX for removing a bat options item.

This is a direct page callback. The actual job of deleting the item is done in the submit handler for the button, so all we really need to do is process the form and then generate output. We generate this output by doing a replace command on the id of the entire form element.

1 string reference to 'bat_options_remove_js'
bat_options_menu in modules/bat_options/bat_options.module
Implements hook_menu().

File

modules/bat_options/bat_options.module, line 343

Code

function bat_options_remove_js() {

  // drupal_html_id() very helpfully ensures that all html IDS are unique
  // on a page. Unfortunately what it doesn't realize is that the IDs
  // we are generating are going to replace IDs that already exist, so
  // this actually works against us.
  if (isset($_POST['ajax_html_ids'])) {
    unset($_POST['ajax_html_ids']);
  }
  list($form, $form_state, $form_id, $form_build_id, $commands) = ajax_get_form();
  drupal_process_form($form['#form_id'], $form, $form_state);

  // Get the information on what we're removing.
  $button = $form_state['triggering_element'];

  // Go two levels up in the form, to the whole widget.
  $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -3));

  // Now send back the proper AJAX command to replace it.
  $commands[] = ajax_command_replace('#' . $element['#id'], drupal_render($element));
  $return = array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );

  // Because we're doing this ourselves, messages aren't automatic. We have
  // to add them.
  $messages = theme('status_messages');
  if ($messages) {
    $return['#commands'][] = ajax_command_prepend('#' . $element['#id'], $messages);
  }
  return $return;
}