1 ajax.inc ajax_get_form()

Gets a form submitted via #ajax during an Ajax callback.

This will load a form from the form cache used during Ajax operations. It pulls the form info from $_POST.

Return value

An array containing the $form, $form_state, $form_id, $form_build_id and an: initial list of Ajax $commands. Use the list() function to break these apart:

    list($form, $form_state, $form_id, $form_build_id, $commands) = ajax_get_form();
  

Related topics

File

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

Code

function ajax_get_form() {
  // Exit early if $_POST is empty.
  if (empty($_POST) || !isset($_POST['form_build_id'])) {
    backdrop_exit();
  }

  $form_state = form_state_defaults();

  $form_build_id = $_POST['form_build_id'];

  // Get the form from the cache.
  $form = form_get_cache($form_build_id, $form_state);
  if (!$form) {
    // If $form cannot be loaded from the cache, the form_build_id in $_POST
    // must be invalid, which means that someone performed a POST request onto
    // system/ajax without actually viewing the concerned form in the browser.
    // This is likely a hacking attempt as it never happens under normal
    // circumstances, so we just do nothing.
    watchdog('ajax', 'Invalid form POST data.', array(), WATCHDOG_WARNING);
    backdrop_exit();
  }

  // When a page level cache is enabled, the form-build id might have been
  // replaced from within form_get_cache. If this is the case, it is also
  // necessary to update it in the browser by issuing an appropriate Ajax
  // command.
  $commands = array();
  if (isset($form['#build_id_old']) && $form['#build_id_old'] != $form['#build_id']) {
    // If the form build ID has changed, issue an Ajax command to update it.
    $commands[] = ajax_command_update_build_id($form);
    $form_build_id = $form['#build_id'];
  }

  // Since some of the submit handlers are run, redirects need to be disabled.
  $form_state['no_redirect'] = TRUE;

  // When a form is rebuilt after Ajax processing, its #build_id and #action
  // should not change.
  // @see backdrop_rebuild_form()
  $form_state['rebuild_info']['copy']['#build_id'] = TRUE;
  $form_state['rebuild_info']['copy']['#action'] = TRUE;

  // The form needs to be processed; prepare for that by setting a few internal
  // variables.
  $form_state['input'] = $_POST;
  $form_id = $form['#form_id'];

  return array($form, $form_state, $form_id, $form_build_id, $commands);
}