1 ajax.inc ajax_form_callback()

Page callback: Handles Ajax requests for the #ajax Form API property.

This rebuilds the form from cache and invokes the defined #ajax['callback'] to return an Ajax command structure for JavaScript. In case no 'callback' has been defined, nothing will happen.

The Form API #ajax property can be set both for buttons and other input elements.

This function is also the canonical example of how to implement #ajax['path']. If processing is required that cannot be accomplished with a callback, re-implement this function and set #ajax['path'] to the enhanced function.

See also

system_menu()

Related topics

File

core/includes/ajax.inc, line 388
Functions for use with Backdrop's Ajax framework.

Code

function ajax_form_callback() {
  list($form, $form_state, $form_id, $form_build_id, $commands) = ajax_get_form();
  backdrop_process_form($form['#form_id'], $form, $form_state);

  // We need to return the part of the form (or some other content) that needs
  // to be re-rendered so the browser can update the page with changed content.
  // Since this is the generic menu callback used by many Ajax elements, it is
  // up to the #ajax['callback'] function of the element (may or may not be a
  // button) that triggered the Ajax request to determine what needs to be
  // rendered.
  if (!empty($form_state['triggering_element'])) {
    $callback = $form_state['triggering_element']['#ajax']['callback'];
  }
  if (!empty($callback) && is_callable($callback)) {
    $result = call_user_func_array($callback, array(&$form, &$form_state));

    if (!(is_array($result) && isset($result['#type']) && $result['#type'] == 'ajax')) {
      // Turn the response into a #type=ajax array if it isn't one already.
      $result = array(
        '#type' => 'ajax',
        '#commands' => ajax_prepare_response($result),
      );
    }

    $result['#commands'] = array_merge($commands, $result['#commands']);

    return $result;
  }
  return NULL;
}