You are here

function multifield_field_widget_remove_item_ajax in Multifield 7.2

Same name and namespace in other branches
  1. 7 multifield.field.inc \multifield_field_widget_remove_item_ajax()

Page callback to handle AJAX for removing a multifield item.

Copied from field_collection_remove_js().

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 'multifield_field_widget_remove_item_ajax'
multifield_menu in ./multifield.module
Implements hook_menu().

File

./multifield.field.inc, line 627
Field integration for the Multifield module.

Code

function multifield_field_widget_remove_item_ajax() {
  require_once DRUPAL_ROOT . '/includes/form.inc';

  // 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) = 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, -4));

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

  // 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;
}