ajax_form_callback

  1. drupal
    1. 8
    2. 7
Versions
7 – 8 ajax_form_callback()

Menu 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.

Related topics

Code

includes/ajax.inc, line 372

<?php
function ajax_form_callback() {
  list($form, $form_state) = ajax_get_form();
  drupal_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) && function_exists($callback)) {
    return $callback($form, $form_state);
  }
}
?>