Newer
Older
<?php
/**
* @file
* Functions for use with Drupal's Ajax framework.
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
* @defgroup ajax Ajax framework
* 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
* \Drupal\system\FormAjaxController::content() 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

catch
committed
* also return a richer set of
* @link ajax_commands Ajax framework commands @endlink.
* Standard form handling is as follows:

Dries Buytaert
committed
* - A form element has a #ajax property that includes #ajax['callback'] and
* omits #ajax['path']. See below about using #ajax['path'] to implement
* advanced use-cases that require something other than standard form
* handling.
* - On the specified element, Ajax processing is triggered by a change to

Dries Buytaert
committed
* - The browser submits an HTTP POST request to the 'system/ajax' Drupal
* path.
* - The controller for the route '/system/ajax',
* \Drupal\system\FormAjaxController::content(), calls drupal_process_form()
* to process the form submission and rebuild the form if necessary. The
* form is processed in much the same way as if it were submitted without
* Ajax, with the same #process functions and validation and submission
* handlers called in either case, making it easy to create Ajax-enabled
* forms that degrade gracefully when JavaScript is disabled.
* - After form processing is complete,
* \Drupal\system\FormAjaxController::content() calls the function named by
* #ajax['callback'], which returns the form element that has been updated
* and needs to be returned to the browser, or alternatively, an array of
* custom Ajax commands.

Dries Buytaert
committed
* - The array is serialized using ajax_render() and sent to the browser.

Dries Buytaert
committed
* - The browser unserializes the returned JSON string into an array of
* command objects and executes each command, resulting in the old page
* content within and including the HTML element specified by
* #ajax['wrapper'] being replaced by the new content returned by
* #ajax['callback'], using a JavaScript animation effect specified by
* #ajax['effect'].
* A simple example of basic Ajax use from the
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
* @link http://drupal.org/project/examples Examples module @endlink follows:
* @code
* 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'];
* }
* @endcode
* 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

catch
committed
* 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):
* - #ajax['callback']: The callback to invoke to handle the server side of the
* Ajax event, which will receive a $form and $form_state as arguments, and
* returns a renderable array (most often a form or form fragment), an HTML
* string, or an array of Ajax commands. If returning a renderable array or
* a string, the value will replace the original element named in
* #ajax['wrapper'], and
* theme_status_messages()
* will be prepended to that
* element. (If the status messages are not wanted, return an array
* of Ajax commands instead.)
* #ajax['wrapper']. If an array of Ajax commands is returned, it will be
* executed by the calling code.
* - #ajax['path']: The menu path to use for the request. This is often omitted
* and the default is used. This path should map

Dries Buytaert
committed
* to a menu page callback that returns data using ajax_render(). Defaults to
* 'system/ajax', which invokes \Drupal\system\FormAjaxController::content(),
* eventually calling the function named in #ajax['callback']. If you use a
* custom path, you must set up the menu entry and handle the entire callback
* in your own code.
* - #ajax['wrapper']: The CSS ID of the area to be replaced by the content
* returned by the #ajax['callback'] function. The content returned from
* the callback will replace the entire element named by #ajax['wrapper'].
* The wrapper is usually created using #prefix and #suffix properties in the
* form. Note that this is the wrapper ID, not a CSS selector. So to replace
* the element referred to by the CSS selector #some-selector on the page,
* use #ajax['wrapper'] = 'some-selector', not '#some-selector'.
* - #ajax['effect']: The jQuery effect to use when placing the new HTML.
* Defaults to no effect. Valid options are 'none', 'slide', or 'fade'.
* - #ajax['speed']: The effect speed to use. Defaults to 'slow'. May be
* 'slow', 'fast' or a number in milliseconds which represents the length
* of time the effect should run.
* - #ajax['event']: The JavaScript event to respond to. This is normally
* selected automatically for the type of form widget being used, and
* is only needed if you need to override the default behavior.

Angie Byron
committed
* - #ajax['prevent']: A JavaScript event to prevent when 'event' is triggered.
* Defaults to 'click' for #ajax on #type 'submit', 'button', and
* 'image_button'. Multiple events may be specified separated by spaces.
* For example, when binding #ajax behaviors to form buttons, pressing the
* ENTER key within a textfield triggers the 'click' event of the form's first
* submit button. Triggering Ajax in this situation leads to problems, like
* breaking autocomplete textfields. Because of that, Ajax behaviors are bound
* to the 'mousedown' event on form buttons by default. However, binding to
* 'mousedown' rather than 'click' means that it is possible to trigger a
* click by pressing the mouse, holding the mouse button down until the Ajax
* request is complete and the button is re-enabled, and then releasing the
* mouse button. For this case, 'prevent' can be set to 'click', so an
* additional event handler is bound to prevent such a click from triggering a
* non-Ajax form submission. This also prevents a textfield's ENTER press
* triggering a button's non-Ajax form submission behavior.
* - #ajax['method']: The jQuery method to use to place the new HTML.

Dries Buytaert
committed
* Defaults to 'replaceWith'. May be: 'replaceWith', 'append', 'prepend',
* 'before', 'after', or 'html'. See the
* @link http://api.jquery.com/category/manipulation/ jQuery manipulators documentation @endlink
* for more information on these methods.
* - #ajax['progress']: Choose either a throbber or progress bar that is
* displayed while awaiting a response from the callback, and add an optional
* message. Possible keys: 'type', 'message', 'url', 'interval'.
* More information is available in the

Jennifer Hodgdon
committed
* @link forms_api_reference.html Form API Reference @endlink
* 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.
*

catch
committed
* 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:
* @code
* $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');
* // #ajax['callback'] functions are supposed to return render arrays. If
* // returning an Ajax commands array, it must be encapsulated in a render
* // array structure.

Dries Buytaert
committed
* return array('#type' => 'ajax', '#commands' => $commands);
* @endcode

Dries Buytaert
committed
*
* 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:

Dries Buytaert
committed
* @code
* $commands = array();
* $commands[] = ajax_command_replace(NULL, $output);
* $commands[] = ajax_command_prepend(NULL, theme('status_messages'));

Dries Buytaert
committed
* return array('#type' => 'ajax', '#commands' => $commands);

Dries Buytaert
committed
* @endcode
*
* See @link ajax_commands Ajax framework commands @endlink
*/
/**

catch
committed
* Renders a commands array into JSON.
*
* @param $commands
* A list of macro commands generated by the use of ajax_command_*()
* functions.
*/

Dries Buytaert
committed
function ajax_render($commands = array()) {

Angie Byron
committed
// Ajax responses aren't rendered with html.html.twig, so we have to call

Dries Buytaert
committed
// drupal_get_css() and drupal_get_js() here, in order to have new files added
// during this request to be loaded by the page. We only want to send back
// files that the page hasn't already loaded, so we implement simple diffing
// logic using array_diff_key().
foreach (array('css', 'js') as $type) {
// It is highly suspicious if $_POST['ajax_page_state'][$type] is empty,
// since the base page ought to have at least one JS file and one CSS file
// loaded. It probably indicates an error, and rather than making the page
// reload all of the files, instead we return no new files.

Angie Byron
committed
if (!\Drupal::request()->request->get("ajax_page_state[$type]", NULL, TRUE)) {

Dries Buytaert
committed
$items[$type] = array();
}
else {

Angie Byron
committed
$function = '_drupal_add_' . $type;

Dries Buytaert
committed
$items[$type] = $function();
drupal_alter($type, $items[$type]);
// @todo Inline CSS and JS items are indexed numerically. These can't be
// reliably diffed with array_diff_key(), since the number can change
// due to factors unrelated to the inline content, so for now, we strip
// the inline items from Ajax responses, and can add support for them

Angie Byron
committed
// when _drupal_add_css() and _drupal_add_js() are changed to use a hash

Jennifer Hodgdon
committed
// of the inline content as the array key.

Dries Buytaert
committed
foreach ($items[$type] as $key => $item) {
if (is_numeric($key)) {
unset($items[$type][$key]);
}
}
// Ensure that the page doesn't reload what it already has.

Alex Pott
committed
$items[$type] = array_diff_key($items[$type], $_POST['ajax_page_state'][$type]);

Dries Buytaert
committed
}
}
// Render the HTML to load these files, and add AJAX commands to insert this
// HTML in the page. We pass TRUE as the $skip_alter argument to prevent the
// data from being altered again, as we already altered it above. Settings are
// handled separately, afterwards.
if (isset($items['js']['settings'])) {
unset($items['js']['settings']);
}

Dries Buytaert
committed
$styles = drupal_get_css($items['css'], TRUE);
$scripts_footer = drupal_get_js('footer', $items['js'], TRUE);
$scripts_header = drupal_get_js('header', $items['js'], TRUE);
$extra_commands = array();
if (!empty($styles)) {
$extra_commands[] = ajax_command_add_css($styles);

Dries Buytaert
committed
}
if (!empty($scripts_header)) {
$extra_commands[] = ajax_command_prepend('head', $scripts_header);
}
if (!empty($scripts_footer)) {
$extra_commands[] = ajax_command_append('body', $scripts_footer);
}
if (!empty($extra_commands)) {
$commands = array_merge($extra_commands, $commands);
}
// Now add a command to merge changes and additions to drupalSettings.

Angie Byron
committed
$scripts = _drupal_add_js();
if (!empty($scripts['settings'])) {
$settings = drupal_merge_js_settings($scripts['settings']['data']);
array_unshift($commands, ajax_command_settings($settings, TRUE));
}
// Allow modules to alter any Ajax response.
drupal_alter('ajax_render', $commands);

Dries Buytaert
committed
return drupal_json_encode($commands);
}
/**
* Converts the return value of a page callback into an Ajax commands array.
*
* @param $page_callback_result
* The result of a page callback. Can be one of:
* - NULL: to indicate no content.
* - An integer menu status constant: to indicate an error condition.
* - A string of HTML content.
* - A renderable array of content.
*
* @return
* An Ajax commands array that can be passed to ajax_render().
*/
function ajax_prepare_response($page_callback_result) {

Angie Byron
committed
$commands = array();

Dries Buytaert
committed
if (!isset($page_callback_result)) {
// Simply delivering an empty commands array is sufficient. This results
// in the Ajax request being completed, but nothing being done to the page.

Dries Buytaert
committed
}
elseif (is_int($page_callback_result)) {
switch ($page_callback_result) {
case MENU_NOT_FOUND:
$commands[] = ajax_command_alert(t('The requested page could not be found.'));
break;

Dries Buytaert
committed
case MENU_ACCESS_DENIED:
$commands[] = ajax_command_alert(t('You are not authorized to access this page.'));
break;
case MENU_SITE_OFFLINE:
$commands[] = ajax_command_alert(filter_xss_admin(t(\Drupal::config('system.maintenance')->get('message'), array('@site' => \Drupal::config('system.site')->get('name')))));

Dries Buytaert
committed
break;

Dries Buytaert
committed
}

Dries Buytaert
committed
elseif (is_array($page_callback_result) && isset($page_callback_result['#type']) && ($page_callback_result['#type'] == 'ajax')) {
// Complex Ajax callbacks can return a result that contains an error message

Dries Buytaert
committed
// or a specific set of commands to send to the browser.
$page_callback_result += element_info('ajax');
$error = $page_callback_result['#error'];
if (isset($error) && $error !== FALSE) {
if ((empty($error) || $error === TRUE)) {
$error = t('An error occurred while handling the request: The server received invalid input.');
}
$commands[] = ajax_command_alert($error);
}
else {
$commands = $page_callback_result['#commands'];
}
}

Dries Buytaert
committed
else {
// Like normal page callbacks, simple Ajax callbacks can return HTML

Dries Buytaert
committed
// content, as a string or render array. This HTML is inserted in some
// relationship to #ajax['wrapper'], as determined by which jQuery DOM
// manipulation method is used. The method used is specified by
// #ajax['method']. The default method is 'replaceWith', which completely
// replaces the old wrapper element and its content with the new HTML.

Dries Buytaert
committed
$html = is_string($page_callback_result) ? $page_callback_result : drupal_render($page_callback_result);

Angie Byron
committed
$commands[] = ajax_command_insert(NULL, $html);

Dries Buytaert
committed
// Add the status messages inside the new content's wrapper element, so that
// on subsequent Ajax requests, it is treated as old content.
$status_messages = array('#theme' => 'status_messages');
$commands[] = ajax_command_prepend(NULL, drupal_render($status_messages));

Dries Buytaert
committed
}

Dries Buytaert
committed
return $commands;

Dries Buytaert
committed
}

catch
committed
* Form element processing handler for the #ajax form property.

Dries Buytaert
committed
*
* @param $element
* An associative array containing the properties of the element.
*
* @return
* The processed element.
*
* @see ajax_pre_render_element()
*/
function ajax_process_form($element, &$form_state) {
$element = ajax_pre_render_element($element);
if (!empty($element['#ajax_processed'])) {
$form_state['cache'] = TRUE;
}
return $element;
}
/**

catch
committed
* Adds Ajax information about an element to communicate with JavaScript.
*
* If #ajax['path'] is set on an element, this additional JavaScript is added
* to the page header to attach the Ajax behaviors. See ajax.js for more
* information.
*
* @param $element
* An associative array containing the properties of the element.
* Properties used:
* - #ajax['event']

Angie Byron
committed
* - #ajax['prevent']
* - #ajax['path']

Dries Buytaert
committed
* - #ajax['options']
* - #ajax['wrapper']
* - #ajax['parameters']
* - #ajax['effect']
* - #ajax['accepts']
*
* @return

Dries Buytaert
committed
* The processed element with the necessary JavaScript attached to it.

Dries Buytaert
committed
function ajax_pre_render_element($element) {
// Skip already processed elements.
if (isset($element['#ajax_processed'])) {
return $element;
}
// Initialize #ajax_processed, so we do not process this element again.
$element['#ajax_processed'] = FALSE;
// Nothing to do if there are no Ajax settings.
if (empty($element['#ajax'])) {
return $element;
}
// Add a reasonable default event handler if none was specified.
if (isset($element['#ajax']) && !isset($element['#ajax']['event'])) {
switch ($element['#type']) {
case 'submit':
case 'button':
case 'image_button':

Angie Byron
committed
// Pressing the ENTER key within a textfield triggers the click event of
// the form's first submit button. Triggering Ajax in this situation
// leads to problems, like breaking autocomplete textfields, so we bind
// to mousedown instead of click.
// @see http://drupal.org/node/216059
$element['#ajax']['event'] = 'mousedown';

Angie Byron
committed
// Retain keyboard accessibility by setting 'keypress'. This causes
// ajax.js to trigger 'event' when SPACE or ENTER are pressed while the
// button has focus.
$element['#ajax']['keypress'] = TRUE;

Angie Byron
committed
// Binding to mousedown rather than click means that it is possible to
// trigger a click by pressing the mouse, holding the mouse button down
// until the Ajax request is complete and the button is re-enabled, and
// then releasing the mouse button. Set 'prevent' so that ajax.js binds
// an additional handler to prevent such a click from triggering a
// non-Ajax form submission. This also prevents a textfield's ENTER
// press triggering this button's non-Ajax form submission behavior.

Angie Byron
committed
if (!isset($element['#ajax']['prevent'])) {
$element['#ajax']['prevent'] = 'click';
}
break;
case 'password':
case 'textfield':

Alex Pott
committed
case 'number':
case 'tel':
case 'textarea':
$element['#ajax']['event'] = 'blur';
break;
case 'radio':
case 'checkbox':
case 'select':
$element['#ajax']['event'] = 'change';
break;

Dries Buytaert
committed
case 'link':
$element['#ajax']['event'] = 'click';
break;
default:
return $element;
}
}

Dries Buytaert
committed
// Attach JavaScript settings to the element.
if (isset($element['#ajax']['event'])) {

Dries Buytaert
committed
$element['#attached']['library'][] = array('system', 'jquery.form');
$element['#attached']['library'][] = array('system', 'drupal.ajax');
$settings = $element['#ajax'];
// Assign default settings. When 'path' is set to NULL, ajax.js submits the
// Ajax request to the same URL as the form or link destination is for
// someone with JavaScript disabled. This is generally preferred as a way to
// ensure consistent server processing for js and no-js users, and Drupal's
// content negotiation takes care of formatting the response appropriately.
// However, 'path' and 'options' may be set when wanting server processing
// to be substantially different for a JavaScript triggered submission.
// One such substantial difference is form elements that use
// #ajax['callback'] for determining which part of the form needs
// re-rendering. For that, we have a special 'system/ajax' route.
$settings += array(
'path' => isset($settings['callback']) ? 'system/ajax' : NULL,

Dries Buytaert
committed
'options' => array(),
'accepts' => 'application/vnd.drupal-ajax'
);

Dries Buytaert
committed
// @todo Legacy support. Remove in Drupal 8.
if (isset($settings['method']) && $settings['method'] == 'replace') {

Dries Buytaert
committed
$settings['method'] = 'replaceWith';
}

Dries Buytaert
committed
// Change path to URL.
$settings['url'] = isset($settings['path']) ? url($settings['path'], $settings['options']) : NULL;

Dries Buytaert
committed
unset($settings['path'], $settings['options']);
// Add special data to $settings['submit'] so that when this element
// triggers an Ajax submission, Drupal's form processing can determine which
// element triggered it.
// @see _form_element_triggered_scripted_submission()
if (isset($settings['trigger_as'])) {
// An element can add a 'trigger_as' key within #ajax to make the element
// submit as though another one (for example, a non-button can use this
// to submit the form as though a button were clicked). When using this,
// the 'name' key is always required to identify the element to trigger
// as. The 'value' key is optional, and only needed when multiple elements
// share the same name, which is commonly the case for buttons.
$settings['submit']['_triggering_element_name'] = $settings['trigger_as']['name'];
if (isset($settings['trigger_as']['value'])) {
$settings['submit']['_triggering_element_value'] = $settings['trigger_as']['value'];
}
unset($settings['trigger_as']);
}

Dries Buytaert
committed
elseif (isset($element['#name'])) {
// Most of the time, elements can submit as themselves, in which case the
// 'trigger_as' key isn't needed, and the element's name is used.
$settings['submit']['_triggering_element_name'] = $element['#name'];
// If the element is a (non-image) button, its name may not identify it
// uniquely, in which case a match on value is also needed.
// @see _form_button_was_clicked()

Angie Byron
committed
if (!empty($element['#is_button']) && empty($element['#has_garbage_value'])) {
$settings['submit']['_triggering_element_value'] = $element['#value'];
}
}
// Convert a simple #ajax['progress'] string into an array.
if (isset($settings['progress']) && is_string($settings['progress'])) {
$settings['progress'] = array('type' => $settings['progress']);
}
// Change progress path to a full URL.
if (isset($settings['progress']['path'])) {
$settings['progress']['url'] = url($settings['progress']['path']);
unset($settings['progress']['path']);
}

Dries Buytaert
committed
$element['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('ajax' => array($element['#id'] => $settings)),
);
// Indicate that Ajax processing was successful.

Dries Buytaert
committed
$element['#ajax_processed'] = TRUE;
}
return $element;
}
/**
* @} End of "defgroup ajax".
*/
/**
* @defgroup ajax_commands Ajax framework commands
* Functions to create various Ajax commands.
*
* These functions can be used to create arrays for use with the
* ajax_render() function.
*/
/**
* Creates a Drupal Ajax 'alert' command.
*
* The 'alert' command instructs the client to display a JavaScript alert
* dialog box.
*
* This command is implemented by Drupal.AjaxCommands.prototype.alert()
* defined in misc/ajax.js.
*
* @param $text
* The message string to display to the user.
*
* @return
* An array suitable for use with the ajax_render() function.
*/
function ajax_command_alert($text) {
return array(
'command' => 'alert',
'text' => $text,
);
}

Dries Buytaert
committed
/**
* Creates a Drupal Ajax 'insert' command using the method in #ajax['method'].

Dries Buytaert
committed
*
* This command instructs the client to insert the given HTML using whichever
* jQuery DOM manipulation method has been specified in the #ajax['method']
* variable of the element that triggered the request.
*
* This command is implemented by Drupal.AjaxCommands.prototype.insert()

Dries Buytaert
committed
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $html
* The data to use with the jQuery method.
* @param $settings
* An optional array of settings that will be used for this command only.
*
* @return
* An array suitable for use with the ajax_render() function.
*/
function ajax_command_insert($selector, $html, $settings = NULL) {
return array(
'command' => 'insert',
'method' => NULL,
'selector' => $selector,
'data' => $html,
'settings' => $settings,
);
}
* Creates a Drupal Ajax 'insert/prepend' command.
*
* The 'insert/prepend' command instructs the client to use jQuery's prepend()
* method to prepend the given HTML content to the inside each element matched
* by the given selector.
*
* This command is implemented by Drupal.AjaxCommands.prototype.insert()
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $html
* The data to use with the jQuery prepend() method.
* @param $settings
* An optional array of settings that will be used for this command only.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/Manipulation/prepend#content
*/
function ajax_command_prepend($selector, $html, $settings = NULL) {
return array(
'command' => 'insert',
'method' => 'prepend',
'selector' => $selector,
'data' => $html,
'settings' => $settings,
);
}
/**
* Creates a Drupal Ajax 'insert/append' command.
*
* The 'insert/append' command instructs the client to use jQuery's append()
* method to append the given HTML content to the inside of each element matched
* by the given selector.
*
* This command is implemented by Drupal.AjaxCommands.prototype.insert()
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $html
* The data to use with the jQuery append() method.
* @param $settings
* An optional array of settings that will be used for this command only.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/Manipulation/append#content
*/
function ajax_command_append($selector, $html, $settings = NULL) {
return array(
'command' => 'insert',
'method' => 'append',
'selector' => $selector,
'data' => $html,
'settings' => $settings,
);
}
/**
* Creates a Drupal Ajax 'remove' command.
*
* The 'remove' command instructs the client to use jQuery's remove() method
* to remove each of elements matched by the given selector, and everything
* within them.
*
* This command is implemented by Drupal.AjaxCommands.prototype.remove()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/Manipulation/remove#expr
*/
function ajax_command_remove($selector) {
return array(
'command' => 'remove',
'selector' => $selector,
);
}
/**
* Creates a Drupal Ajax 'changed' command.
*
* This command instructs the client to mark each of the elements matched by the
* given selector as 'ajax-changed'.
*
* This command is implemented by Drupal.AjaxCommands.prototype.changed()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $asterisk
* An optional CSS selector which must be inside $selector. If specified,
* an asterisk will be appended to the HTML inside the $asterisk selector.
*
* @return
* An array suitable for use with the ajax_render() function.
*/
function ajax_command_changed($selector, $asterisk = '') {
return array(
'command' => 'changed',
'selector' => $selector,
'asterisk' => $asterisk,
);
}
/**
* Creates a Drupal Ajax 'css' command.
*
* The 'css' command will instruct the client to use the jQuery css() method
* to apply the CSS arguments to elements matched by the given selector.
*
* This command is implemented by Drupal.AjaxCommands.prototype.css()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $argument
* An array of key/value pairs to set in the CSS for the selector.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/CSS/css#properties
*/
function ajax_command_css($selector, $argument) {
return array(
'command' => 'css',
'selector' => $selector,
'argument' => $argument,
);
}
/**
* Creates a Drupal Ajax 'settings' command.

Angie Byron
committed
* The 'settings' command instructs the client either to use the given array as
* the settings for ajax-loaded content or to extend drupalSettings with the

Angie Byron
committed
* given array, depending on the value of the $merge parameter.
* This command is implemented by Drupal.AjaxCommands.prototype.settings()
* defined in misc/ajax.js.
*
* @param $argument
* An array of key/value pairs to add to the settings. This will be utilized
* for all commands after this if they do not include their own settings
* array.

Angie Byron
committed
* @param $merge
* Whether or not the passed settings in $argument should be merged into the
* global drupalSettings on the page. By default (FALSE), the settings that

Angie Byron
committed
* are passed to Drupal.attachBehaviors will not include the global
* drupalSettings.
*
* @return
* An array suitable for use with the ajax_render() function.
*/

Angie Byron
committed
function ajax_command_settings($argument, $merge = FALSE) {
return array(
'command' => 'settings',
'settings' => $argument,

Angie Byron
committed
'merge' => $merge,
);
}
/**
* Creates a Drupal Ajax 'add_css' command.
*
* This method will add css via ajax in a cross-browser compatible way.
*
* This command is implemented by Drupal.AjaxCommands.prototype.add_css()
* defined in misc/ajax.js.
*
* @param $styles
* A string that contains the styles to be added.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see misc/ajax.js
*/
function ajax_command_add_css($styles) {
return array(
'command' => 'add_css',
'data' => $styles,
);
}