Functions for Drupal's Ajax framework.
Drupal's Ajax framework is used to dynamically update parts of a page's HTML based on data from the server. Upon a specified event, such as a button click, a callback function is triggered which performs server-side logic and may return updated markup, which is then replaced on-the-fly with no page refresh necessary.
This framework creates a PHP macro language that allows the server to instruct JavaScript to perform actions on the client browser. When using forms, it can be used with the #ajax property. The #ajax property can be used to bind events to the Ajax framework. By default, #ajax uses 'system/ajax' as its path for submission and thus calls ajax_form_callback() and a defined #ajax['callback'] function. However, you may optionally specify a different path to request or a different callback function to invoke, which can return updated HTML or can also return a richer set of Ajax framework commands.
Standard form handling is as follows:
A simple example of basic Ajax use from the Examples module follows:
function main_page() {
return drupal_get_form('ajax_example_simplest');
}
function ajax_example_simplest($form, &$form_state) {
$form = array();
$form['changethis'] = array(
'#type' => 'select',
'#options' => array(
'one' => 'one',
'two' => 'two',
'three' => 'three',
),
'#ajax' => array(
'callback' => 'ajax_example_simplest_callback',
'wrapper' => 'replace_textfield_div',
),
);
// This entire form element will be replaced with an updated value.
$form['replace_textfield'] = array(
'#type' => 'textfield',
'#title' => t("The default value will be changed"),
'#description' => t("Say something about why you chose") . "'" . (!empty($form_state['values']['changethis']) ? $form_state['values']['changethis'] : t("Not changed yet")) . "'",
'#prefix' => '<div id="replace_textfield_div">',
'#suffix' => '</div>',
);
return $form;
}
function ajax_example_simplest_callback($form, $form_state) {
// The form has already been submitted and updated. We can return the replaced
// item as it is.
return $form['replace_textfield'];
}
In the above example, the 'changethis' element is Ajax-enabled. The default #ajax['event'] is 'change', so when the 'changethis' element changes, an Ajax call is made. The form is submitted and reprocessed, and then the callback is called. In this case, the form has been automatically built changing $form['replace_textfield']['#description'], so the callback just returns that part of the form.
To implement Ajax handling in a form, add '#ajax' to the form definition of a field. That field will trigger an Ajax event when it is clicked (or changed, depending on the kind of field). #ajax supports the following parameters (either 'path' or 'callback' is required at least):
In addition to using Form API for doing in-form modification, Ajax may be enabled by adding classes to buttons and links. By adding the 'use-ajax' class to a link, the link will be loaded via an Ajax call. When using this method, the href of the link can contain '/nojs/' as part of the path. When the Ajax framework makes the request, it will convert this to '/ajax/'. The server is then able to easily tell if this request was made through an actual Ajax request or in a degraded state, and respond appropriately.
Similarly, submit buttons can be given the class 'use-ajax-submit'. The form will then be submitted via Ajax to the path specified in the #action. Like the ajax-submit class above, this path will have '/nojs/' replaced with '/ajax/' so that the submit handler can tell if the form was submitted in a degraded state or not.
When responding to Ajax requests, the server should do what it needs to do for that request, then create a commands array. This commands array will be converted to a JSON object and returned to the client, which will then iterate over the array and process it like a macro language.
Each command item is an associative array which will be converted to a command object on the JavaScript side. $command_item['command'] is the type of command, e.g. 'alert' or 'replace', and will correspond to a method in the Drupal.ajax[command] space. The command array may contain any other data that the command needs to process, e.g. 'method', 'selector', 'settings', etc.
Commands are usually created with a couple of helper functions, so they look like this:
$commands = array();
// Replace the content of '#object-1' on the page with 'some html here'.
$commands[] = ajax_command_replace('#object-1', 'some html here');
// Add a visual "changed" marker to the '#object-1' element.
$commands[] = ajax_command_changed('#object-1');
// Menu 'page callback' and #ajax['callback'] functions are supposed to
// return render arrays. If returning an Ajax commands array, it must be
// encapsulated in a render array structure.
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
When returning an Ajax command array, it is often useful to have status messages rendered along with other tasks in the command array. In that case the the Ajax commands array may be constructed like this:
$commands = array();
$commands[] = ajax_command_replace(NULL, $output);
$commands[] = ajax_command_prepend(NULL, theme('status_messages'));
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
Name | Location | Description |
---|---|---|
ajax_base_page_theme |
drupal/ |
Theme callback: Returns the correct theme for an Ajax request. |
ajax_form_callback |
drupal/ |
Page callback: Handles Ajax requests for the #ajax Form API property. |
ajax_get_form |
drupal/ |
Gets a form submitted via #ajax during an Ajax callback. |
ajax_prepare_response |
drupal/ |
Converts the return value of a page callback into an Ajax commands array. |
ajax_pre_render_element |
drupal/ |
Adds Ajax information about an element to communicate with JavaScript. |
ajax_process_form |
drupal/ |
Form element processing handler for the #ajax form property. |
ajax_render |
drupal/ |
Renders a commands array into JSON. |