ajax.inc 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. <?php
  2. /**
  3. * @file
  4. * Functions for use with Drupal's Ajax framework.
  5. */
  6. /**
  7. * @defgroup ajax Ajax framework
  8. * @{
  9. * Functions for Drupal's Ajax framework.
  10. *
  11. * Drupal's Ajax framework is used to dynamically update parts of a page's HTML
  12. * based on data from the server. Upon a specified event, such as a button
  13. * click, a callback function is triggered which performs server-side logic and
  14. * may return updated markup, which is then replaced on-the-fly with no page
  15. * refresh necessary.
  16. *
  17. * This framework creates a PHP macro language that allows the server to
  18. * instruct JavaScript to perform actions on the client browser. When using
  19. * forms, it can be used with the #ajax property.
  20. * The #ajax property can be used to bind events to the Ajax framework. By
  21. * default, #ajax uses 'system/ajax' as its path for submission and thus calls
  22. * ajax_form_callback() and a defined #ajax['callback'] function.
  23. * However, you may optionally specify a different path to request or a
  24. * different callback function to invoke, which can return updated HTML or can
  25. * also return a richer set of
  26. * @link ajax_commands Ajax framework commands @endlink.
  27. *
  28. * Standard form handling is as follows:
  29. * - A form element has a #ajax property that includes #ajax['callback'] and
  30. * omits #ajax['path']. See below about using #ajax['path'] to implement
  31. * advanced use-cases that require something other than standard form
  32. * handling.
  33. * - On the specified element, Ajax processing is triggered by a change to
  34. * that element.
  35. * - The browser submits an HTTP POST request to the 'system/ajax' Drupal
  36. * path.
  37. * - The menu page callback for 'system/ajax', ajax_form_callback(), calls
  38. * drupal_process_form() to process the form submission and rebuild the
  39. * form if necessary. The form is processed in much the same way as if it
  40. * were submitted without Ajax, with the same #process functions and
  41. * validation and submission handlers called in either case, making it easy
  42. * to create Ajax-enabled forms that degrade gracefully when JavaScript is
  43. * disabled.
  44. * - After form processing is complete, ajax_form_callback() calls the
  45. * function named by #ajax['callback'], which returns the form element that
  46. * has been updated and needs to be returned to the browser, or
  47. * alternatively, an array of custom Ajax commands.
  48. * - The page delivery callback for 'system/ajax', ajax_deliver(), renders the
  49. * element returned by #ajax['callback'], and returns the JSON string
  50. * created by ajax_render() to the browser.
  51. * - The browser unserializes the returned JSON string into an array of
  52. * command objects and executes each command, resulting in the old page
  53. * content within and including the HTML element specified by
  54. * #ajax['wrapper'] being replaced by the new content returned by
  55. * #ajax['callback'], using a JavaScript animation effect specified by
  56. * #ajax['effect'].
  57. *
  58. * A simple example of basic Ajax use from the
  59. * @link http://drupal.org/project/examples Examples module @endlink follows:
  60. * @code
  61. * function main_page() {
  62. * return drupal_get_form('ajax_example_simplest');
  63. * }
  64. *
  65. * function ajax_example_simplest($form, &$form_state) {
  66. * $form = array();
  67. * $form['changethis'] = array(
  68. * '#type' => 'select',
  69. * '#options' => array(
  70. * 'one' => 'one',
  71. * 'two' => 'two',
  72. * 'three' => 'three',
  73. * ),
  74. * '#ajax' => array(
  75. * 'callback' => 'ajax_example_simplest_callback',
  76. * 'wrapper' => 'replace_textfield_div',
  77. * ),
  78. * );
  79. * // This entire form element will be replaced with an updated value.
  80. * $form['replace_textfield'] = array(
  81. * '#type' => 'textfield',
  82. * '#title' => t("The default value will be changed"),
  83. * '#description' => t("Say something about why you chose") . "'" .
  84. * (!empty($form_state['values']['changethis'])
  85. * ? $form_state['values']['changethis'] : t("Not changed yet")) . "'",
  86. * '#prefix' => '<div id="replace_textfield_div">',
  87. * '#suffix' => '</div>',
  88. * );
  89. * return $form;
  90. * }
  91. *
  92. * function ajax_example_simplest_callback($form, $form_state) {
  93. * // The form has already been submitted and updated. We can return the replaced
  94. * // item as it is.
  95. * return $form['replace_textfield'];
  96. * }
  97. * @endcode
  98. *
  99. * In the above example, the 'changethis' element is Ajax-enabled. The default
  100. * #ajax['event'] is 'change', so when the 'changethis' element changes,
  101. * an Ajax call is made. The form is submitted and reprocessed, and then the
  102. * callback is called. In this case, the form has been automatically
  103. * built changing $form['replace_textfield']['#description'], so the callback
  104. * just returns that part of the form.
  105. *
  106. * To implement Ajax handling in a form, add '#ajax' to the form
  107. * definition of a field. That field will trigger an Ajax event when it is
  108. * clicked (or changed, depending on the kind of field). #ajax supports
  109. * the following parameters (either 'path' or 'callback' is required at least):
  110. * - #ajax['callback']: The callback to invoke to handle the server side of the
  111. * Ajax event, which will receive a $form and $form_state as arguments, and
  112. * returns a renderable array (most often a form or form fragment), an HTML
  113. * string, or an array of Ajax commands. If returning a renderable array or
  114. * a string, the value will replace the original element named in
  115. * #ajax['wrapper'], and
  116. * theme_status_messages()
  117. * will be prepended to that
  118. * element. (If the status messages are not wanted, return an array
  119. * of Ajax commands instead.)
  120. * #ajax['wrapper']. If an array of Ajax commands is returned, it will be
  121. * executed by the calling code.
  122. * - #ajax['path']: The menu path to use for the request. This is often omitted
  123. * and the default is used. This path should map
  124. * to a menu page callback that returns data using ajax_render(). Defaults to
  125. * 'system/ajax', which invokes ajax_form_callback(), eventually calling
  126. * the function named in #ajax['callback']. If you use a custom
  127. * path, you must set up the menu entry and handle the entire callback in your
  128. * own code.
  129. * - #ajax['wrapper']: The CSS ID of the area to be replaced by the content
  130. * returned by the #ajax['callback'] function. The content returned from
  131. * the callback will replace the entire element named by #ajax['wrapper'].
  132. * The wrapper is usually created using #prefix and #suffix properties in the
  133. * form. Note that this is the wrapper ID, not a CSS selector. So to replace
  134. * the element referred to by the CSS selector #some-selector on the page,
  135. * use #ajax['wrapper'] = 'some-selector', not '#some-selector'.
  136. * - #ajax['effect']: The jQuery effect to use when placing the new HTML.
  137. * Defaults to no effect. Valid options are 'none', 'slide', or 'fade'.
  138. * - #ajax['speed']: The effect speed to use. Defaults to 'slow'. May be
  139. * 'slow', 'fast' or a number in milliseconds which represents the length
  140. * of time the effect should run.
  141. * - #ajax['event']: The JavaScript event to respond to. This is normally
  142. * selected automatically for the type of form widget being used, and
  143. * is only needed if you need to override the default behavior.
  144. * - #ajax['prevent']: A JavaScript event to prevent when 'event' is triggered.
  145. * Defaults to 'click' for #ajax on #type 'submit', 'button', and
  146. * 'image_button'. Multiple events may be specified separated by spaces.
  147. * For example, when binding #ajax behaviors to form buttons, pressing the
  148. * ENTER key within a textfield triggers the 'click' event of the form's first
  149. * submit button. Triggering Ajax in this situation leads to problems, like
  150. * breaking autocomplete textfields. Because of that, Ajax behaviors are bound
  151. * to the 'mousedown' event on form buttons by default. However, binding to
  152. * 'mousedown' rather than 'click' means that it is possible to trigger a
  153. * click by pressing the mouse, holding the mouse button down until the Ajax
  154. * request is complete and the button is re-enabled, and then releasing the
  155. * mouse button. For this case, 'prevent' can be set to 'click', so an
  156. * additional event handler is bound to prevent such a click from triggering a
  157. * non-Ajax form submission. This also prevents a textfield's ENTER press
  158. * triggering a button's non-Ajax form submission behavior.
  159. * - #ajax['method']: The jQuery method to use to place the new HTML.
  160. * Defaults to 'replaceWith'. May be: 'replaceWith', 'append', 'prepend',
  161. * 'before', 'after', or 'html'. See the
  162. * @link http://api.jquery.com/category/manipulation/ jQuery manipulators documentation @endlink
  163. * for more information on these methods.
  164. * - #ajax['progress']: Choose either a throbber or progress bar that is
  165. * displayed while awaiting a response from the callback, and add an optional
  166. * message. Possible keys: 'type', 'message', 'url', 'interval'.
  167. * More information is available in the
  168. * @link forms_api_reference.html Form API Reference @endlink
  169. *
  170. * In addition to using Form API for doing in-form modification, Ajax may be
  171. * enabled by adding classes to buttons and links. By adding the 'use-ajax'
  172. * class to a link, the link will be loaded via an Ajax call. When using this
  173. * method, the href of the link can contain '/nojs/' as part of the path. When
  174. * the Ajax framework makes the request, it will convert this to '/ajax/'.
  175. * The server is then able to easily tell if this request was made through an
  176. * actual Ajax request or in a degraded state, and respond appropriately.
  177. *
  178. * Similarly, submit buttons can be given the class 'use-ajax-submit'. The
  179. * form will then be submitted via Ajax to the path specified in the #action.
  180. * Like the ajax-submit class above, this path will have '/nojs/' replaced with
  181. * '/ajax/' so that the submit handler can tell if the form was submitted
  182. * in a degraded state or not.
  183. *
  184. * When responding to Ajax requests, the server should do what it needs to do
  185. * for that request, then create a commands array. This commands array will
  186. * be converted to a JSON object and returned to the client, which will then
  187. * iterate over the array and process it like a macro language.
  188. *
  189. * Each command item is an associative array which will be converted to a
  190. * command object on the JavaScript side. $command_item['command'] is the type
  191. * of command, e.g. 'alert' or 'replace', and will correspond to a method in the
  192. * Drupal.ajax[command] space. The command array may contain any other data that
  193. * the command needs to process, e.g. 'method', 'selector', 'settings', etc.
  194. *
  195. * Commands are usually created with a couple of helper functions, so they
  196. * look like this:
  197. * @code
  198. * $commands = array();
  199. * // Replace the content of '#object-1' on the page with 'some html here'.
  200. * $commands[] = ajax_command_replace('#object-1', 'some html here');
  201. * // Add a visual "changed" marker to the '#object-1' element.
  202. * $commands[] = ajax_command_changed('#object-1');
  203. * // Menu 'page callback' and #ajax['callback'] functions are supposed to
  204. * // return render arrays. If returning an Ajax commands array, it must be
  205. * // encapsulated in a render array structure.
  206. * return array('#type' => 'ajax', '#commands' => $commands);
  207. * @endcode
  208. *
  209. * When returning an Ajax command array, it is often useful to have
  210. * status messages rendered along with other tasks in the command array.
  211. * In that case the the Ajax commands array may be constructed like this:
  212. * @code
  213. * $commands = array();
  214. * $commands[] = ajax_command_replace(NULL, $output);
  215. * $commands[] = ajax_command_prepend(NULL, theme('status_messages'));
  216. * return array('#type' => 'ajax', '#commands' => $commands);
  217. * @endcode
  218. *
  219. * See @link ajax_commands Ajax framework commands @endlink
  220. */
  221. /**
  222. * Renders a commands array into JSON.
  223. *
  224. * @param $commands
  225. * A list of macro commands generated by the use of ajax_command_*()
  226. * functions.
  227. */
  228. function ajax_render($commands = array()) {
  229. // Ajax responses aren't rendered with html.tpl.php, so we have to call
  230. // drupal_get_css() and drupal_get_js() here, in order to have new files added
  231. // during this request to be loaded by the page. We only want to send back
  232. // files that the page hasn't already loaded, so we implement simple diffing
  233. // logic using array_diff_key().
  234. foreach (array('css', 'js') as $type) {
  235. // It is highly suspicious if $_POST['ajax_page_state'][$type] is empty,
  236. // since the base page ought to have at least one JS file and one CSS file
  237. // loaded. It probably indicates an error, and rather than making the page
  238. // reload all of the files, instead we return no new files.
  239. if (empty($_POST['ajax_page_state'][$type])) {
  240. $items[$type] = array();
  241. }
  242. else {
  243. $function = 'drupal_add_' . $type;
  244. $items[$type] = $function();
  245. drupal_alter($type, $items[$type]);
  246. // @todo Inline CSS and JS items are indexed numerically. These can't be
  247. // reliably diffed with array_diff_key(), since the number can change
  248. // due to factors unrelated to the inline content, so for now, we strip
  249. // the inline items from Ajax responses, and can add support for them
  250. // when drupal_add_css() and drupal_add_js() are changed to use a hash
  251. // of the inline content as the array key.
  252. foreach ($items[$type] as $key => $item) {
  253. if (is_numeric($key)) {
  254. unset($items[$type][$key]);
  255. }
  256. }
  257. // Ensure that the page doesn't reload what it already has.
  258. $items[$type] = array_diff_key($items[$type], $_POST['ajax_page_state'][$type]);
  259. }
  260. }
  261. // Render the HTML to load these files, and add AJAX commands to insert this
  262. // HTML in the page. We pass TRUE as the $skip_alter argument to prevent the
  263. // data from being altered again, as we already altered it above. Settings are
  264. // handled separately, afterwards.
  265. if (isset($items['js']['settings'])) {
  266. unset($items['js']['settings']);
  267. }
  268. $styles = drupal_get_css($items['css'], TRUE);
  269. $scripts_footer = drupal_get_js('footer', $items['js'], TRUE);
  270. $scripts_header = drupal_get_js('header', $items['js'], TRUE);
  271. $extra_commands = array();
  272. if (!empty($styles)) {
  273. $extra_commands[] = ajax_command_prepend('head', $styles);
  274. }
  275. if (!empty($scripts_header)) {
  276. $extra_commands[] = ajax_command_prepend('head', $scripts_header);
  277. }
  278. if (!empty($scripts_footer)) {
  279. $extra_commands[] = ajax_command_append('body', $scripts_footer);
  280. }
  281. if (!empty($extra_commands)) {
  282. $commands = array_merge($extra_commands, $commands);
  283. }
  284. // Now add a command to merge changes and additions to Drupal.settings.
  285. $scripts = drupal_add_js();
  286. if (!empty($scripts['settings'])) {
  287. $settings = $scripts['settings'];
  288. array_unshift($commands, ajax_command_settings(call_user_func_array('array_merge_recursive', $settings['data']), TRUE));
  289. }
  290. // Allow modules to alter any Ajax response.
  291. drupal_alter('ajax_render', $commands);
  292. return drupal_json_encode($commands);
  293. }
  294. /**
  295. * Gets a form submitted via #ajax during an Ajax callback.
  296. *
  297. * This will load a form from the form cache used during Ajax operations. It
  298. * pulls the form info from $_POST.
  299. *
  300. * @return
  301. * An array containing the $form and $form_state. Use the list() function
  302. * to break these apart:
  303. * @code
  304. * list($form, $form_state, $form_id, $form_build_id) = ajax_get_form();
  305. * @endcode
  306. */
  307. function ajax_get_form() {
  308. $form_state = form_state_defaults();
  309. $form_build_id = $_POST['form_build_id'];
  310. // Get the form from the cache.
  311. $form = form_get_cache($form_build_id, $form_state);
  312. if (!$form) {
  313. // If $form cannot be loaded from the cache, the form_build_id in $_POST
  314. // must be invalid, which means that someone performed a POST request onto
  315. // system/ajax without actually viewing the concerned form in the browser.
  316. // This is likely a hacking attempt as it never happens under normal
  317. // circumstances, so we just do nothing.
  318. watchdog('ajax', 'Invalid form POST data.', array(), WATCHDOG_WARNING);
  319. drupal_exit();
  320. }
  321. // Since some of the submit handlers are run, redirects need to be disabled.
  322. $form_state['no_redirect'] = TRUE;
  323. // When a form is rebuilt after Ajax processing, its #build_id and #action
  324. // should not change.
  325. // @see drupal_rebuild_form()
  326. $form_state['rebuild_info']['copy']['#build_id'] = TRUE;
  327. $form_state['rebuild_info']['copy']['#action'] = TRUE;
  328. // The form needs to be processed; prepare for that by setting a few internal
  329. // variables.
  330. $form_state['input'] = $_POST;
  331. $form_id = $form['#form_id'];
  332. return array($form, $form_state, $form_id, $form_build_id);
  333. }
  334. /**
  335. * Menu callback; handles Ajax requests for the #ajax Form API property.
  336. *
  337. * This rebuilds the form from cache and invokes the defined #ajax['callback']
  338. * to return an Ajax command structure for JavaScript. In case no 'callback' has
  339. * been defined, nothing will happen.
  340. *
  341. * The Form API #ajax property can be set both for buttons and other input
  342. * elements.
  343. *
  344. * This function is also the canonical example of how to implement
  345. * #ajax['path']. If processing is required that cannot be accomplished with
  346. * a callback, re-implement this function and set #ajax['path'] to the
  347. * enhanced function.
  348. *
  349. * @see system_menu()
  350. */
  351. function ajax_form_callback() {
  352. list($form, $form_state) = ajax_get_form();
  353. drupal_process_form($form['#form_id'], $form, $form_state);
  354. // We need to return the part of the form (or some other content) that needs
  355. // to be re-rendered so the browser can update the page with changed content.
  356. // Since this is the generic menu callback used by many Ajax elements, it is
  357. // up to the #ajax['callback'] function of the element (may or may not be a
  358. // button) that triggered the Ajax request to determine what needs to be
  359. // rendered.
  360. if (!empty($form_state['triggering_element'])) {
  361. $callback = $form_state['triggering_element']['#ajax']['callback'];
  362. }
  363. if (!empty($callback) && function_exists($callback)) {
  364. return $callback($form, $form_state);
  365. }
  366. }
  367. /**
  368. * Theme callback for Ajax requests.
  369. *
  370. * Many different pages can invoke an Ajax request to system/ajax or another
  371. * generic Ajax path. It is almost always desired for an Ajax response to be
  372. * rendered using the same theme as the base page, because most themes are built
  373. * with the assumption that they control the entire page, so if the CSS for two
  374. * themes are both loaded for a given page, they may conflict with each other.
  375. * For example, Bartik is Drupal's default theme, and Seven is Drupal's default
  376. * administration theme. Depending on whether the "Use the administration theme
  377. * when editing or creating content" checkbox is checked, the node edit form may
  378. * be displayed in either theme, but the Ajax response to the Field module's
  379. * "Add another item" button should be rendered using the same theme as the rest
  380. * of the page. Therefore, system_menu() sets the 'theme callback' for
  381. * 'system/ajax' to this function, and it is recommended that modules
  382. * implementing other generic Ajax paths do the same.
  383. *
  384. * @see system_menu()
  385. * @see file_menu()
  386. */
  387. function ajax_base_page_theme() {
  388. if (!empty($_POST['ajax_page_state']['theme']) && !empty($_POST['ajax_page_state']['theme_token'])) {
  389. $theme = $_POST['ajax_page_state']['theme'];
  390. $token = $_POST['ajax_page_state']['theme_token'];
  391. // Prevent a request forgery from giving a person access to a theme they
  392. // shouldn't be otherwise allowed to see. However, since everyone is allowed
  393. // to see the default theme, token validation isn't required for that, and
  394. // bypassing it allows most use-cases to work even when accessed from the
  395. // page cache.
  396. if ($theme === variable_get('theme_default', 'bartik') || drupal_valid_token($token, $theme)) {
  397. return $theme;
  398. }
  399. }
  400. }
  401. /**
  402. * Packages and sends the result of a page callback as an Ajax response.
  403. *
  404. * This function is the equivalent of drupal_deliver_html_page(), but for Ajax
  405. * requests. Like that function, it:
  406. * - Adds needed HTTP headers.
  407. * - Prints rendered output.
  408. * - Performs end-of-request tasks.
  409. *
  410. * @param $page_callback_result
  411. * The result of a page callback. Can be one of:
  412. * - NULL: to indicate no content.
  413. * - An integer menu status constant: to indicate an error condition.
  414. * - A string of HTML content.
  415. * - A renderable array of content.
  416. *
  417. * @see drupal_deliver_html_page()
  418. */
  419. function ajax_deliver($page_callback_result) {
  420. // Browsers do not allow JavaScript to read the contents of a user's local
  421. // files. To work around that, the jQuery Form plugin submits forms containing
  422. // a file input element to an IFRAME, instead of using XHR. Browsers do not
  423. // normally expect JSON strings as content within an IFRAME, so the response
  424. // must be customized accordingly.
  425. // @see http://malsup.com/jquery/form/#file-upload
  426. // @see Drupal.ajax.prototype.beforeSend()
  427. $iframe_upload = !empty($_POST['ajax_iframe_upload']);
  428. // Emit a Content-Type HTTP header if none has been added by the page callback
  429. // or by a wrapping delivery callback.
  430. if (is_null(drupal_get_http_header('Content-Type'))) {
  431. if (!$iframe_upload) {
  432. // Standard JSON can be returned to a browser's XHR object, and to
  433. // non-browser user agents.
  434. // @see http://www.ietf.org/rfc/rfc4627.txt?number=4627
  435. drupal_add_http_header('Content-Type', 'application/json; charset=utf-8');
  436. }
  437. else {
  438. // Browser IFRAMEs expect HTML. With most other content types, Internet
  439. // Explorer presents the user with a download prompt.
  440. drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
  441. }
  442. }
  443. // Print the response.
  444. $commands = ajax_prepare_response($page_callback_result);
  445. $json = ajax_render($commands);
  446. if (!$iframe_upload) {
  447. // Standard JSON can be returned to a browser's XHR object, and to
  448. // non-browser user agents.
  449. print $json;
  450. }
  451. else {
  452. // Browser IFRAMEs expect HTML. Browser extensions, such as Linkification
  453. // and Skype's Browser Highlighter, convert URLs, phone numbers, etc. into
  454. // links. This corrupts the JSON response. Protect the integrity of the
  455. // JSON data by making it the value of a textarea.
  456. // @see http://malsup.com/jquery/form/#file-upload
  457. // @see http://drupal.org/node/1009382
  458. print '<textarea>' . $json . '</textarea>';
  459. }
  460. // Perform end-of-request tasks.
  461. ajax_footer();
  462. }
  463. /**
  464. * Converts the return value of a page callback into an Ajax commands array.
  465. *
  466. * @param $page_callback_result
  467. * The result of a page callback. Can be one of:
  468. * - NULL: to indicate no content.
  469. * - An integer menu status constant: to indicate an error condition.
  470. * - A string of HTML content.
  471. * - A renderable array of content.
  472. *
  473. * @return
  474. * An Ajax commands array that can be passed to ajax_render().
  475. */
  476. function ajax_prepare_response($page_callback_result) {
  477. $commands = array();
  478. if (!isset($page_callback_result)) {
  479. // Simply delivering an empty commands array is sufficient. This results
  480. // in the Ajax request being completed, but nothing being done to the page.
  481. }
  482. elseif (is_int($page_callback_result)) {
  483. switch ($page_callback_result) {
  484. case MENU_NOT_FOUND:
  485. $commands[] = ajax_command_alert(t('The requested page could not be found.'));
  486. break;
  487. case MENU_ACCESS_DENIED:
  488. $commands[] = ajax_command_alert(t('You are not authorized to access this page.'));
  489. break;
  490. case MENU_SITE_OFFLINE:
  491. $commands[] = ajax_command_alert(filter_xss_admin(variable_get('maintenance_mode_message',
  492. t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal'))))));
  493. break;
  494. }
  495. }
  496. elseif (is_array($page_callback_result) && isset($page_callback_result['#type']) && ($page_callback_result['#type'] == 'ajax')) {
  497. // Complex Ajax callbacks can return a result that contains an error message
  498. // or a specific set of commands to send to the browser.
  499. $page_callback_result += element_info('ajax');
  500. $error = $page_callback_result['#error'];
  501. if (isset($error) && $error !== FALSE) {
  502. if ((empty($error) || $error === TRUE)) {
  503. $error = t('An error occurred while handling the request: The server received invalid input.');
  504. }
  505. $commands[] = ajax_command_alert($error);
  506. }
  507. else {
  508. $commands = $page_callback_result['#commands'];
  509. }
  510. }
  511. else {
  512. // Like normal page callbacks, simple Ajax callbacks can return HTML
  513. // content, as a string or render array. This HTML is inserted in some
  514. // relationship to #ajax['wrapper'], as determined by which jQuery DOM
  515. // manipulation method is used. The method used is specified by
  516. // #ajax['method']. The default method is 'replaceWith', which completely
  517. // replaces the old wrapper element and its content with the new HTML.
  518. $html = is_string($page_callback_result) ? $page_callback_result : drupal_render($page_callback_result);
  519. $commands[] = ajax_command_insert(NULL, $html);
  520. // Add the status messages inside the new content's wrapper element, so that
  521. // on subsequent Ajax requests, it is treated as old content.
  522. $commands[] = ajax_command_prepend(NULL, theme('status_messages'));
  523. }
  524. return $commands;
  525. }
  526. /**
  527. * Performs end-of-Ajax-request tasks.
  528. *
  529. * This function is the equivalent of drupal_page_footer(), but for Ajax
  530. * requests.
  531. *
  532. * @see drupal_page_footer()
  533. */
  534. function ajax_footer() {
  535. // Even for Ajax requests, invoke hook_exit() implementations. There may be
  536. // modules that need very fast Ajax responses, and therefore, run Ajax
  537. // requests with an early bootstrap.
  538. if (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update')) {
  539. module_invoke_all('exit');
  540. }
  541. // Commit the user session. See above comment about the possibility of this
  542. // function running without session.inc loaded.
  543. if (function_exists('drupal_session_commit')) {
  544. drupal_session_commit();
  545. }
  546. }
  547. /**
  548. * Form element processing handler for the #ajax form property.
  549. *
  550. * @param $element
  551. * An associative array containing the properties of the element.
  552. *
  553. * @return
  554. * The processed element.
  555. *
  556. * @see ajax_pre_render_element()
  557. */
  558. function ajax_process_form($element, &$form_state) {
  559. $element = ajax_pre_render_element($element);
  560. if (!empty($element['#ajax_processed'])) {
  561. $form_state['cache'] = TRUE;
  562. }
  563. return $element;
  564. }
  565. /**
  566. * Adds Ajax information about an element to communicate with JavaScript.
  567. *
  568. * If #ajax['path'] is set on an element, this additional JavaScript is added
  569. * to the page header to attach the Ajax behaviors. See ajax.js for more
  570. * information.
  571. *
  572. * @param $element
  573. * An associative array containing the properties of the element.
  574. * Properties used:
  575. * - #ajax['event']
  576. * - #ajax['prevent']
  577. * - #ajax['path']
  578. * - #ajax['options']
  579. * - #ajax['wrapper']
  580. * - #ajax['parameters']
  581. * - #ajax['effect']
  582. *
  583. * @return
  584. * The processed element with the necessary JavaScript attached to it.
  585. */
  586. function ajax_pre_render_element($element) {
  587. // Skip already processed elements.
  588. if (isset($element['#ajax_processed'])) {
  589. return $element;
  590. }
  591. // Initialize #ajax_processed, so we do not process this element again.
  592. $element['#ajax_processed'] = FALSE;
  593. // Nothing to do if there is neither a callback nor a path.
  594. if (!(isset($element['#ajax']['callback']) || isset($element['#ajax']['path']))) {
  595. return $element;
  596. }
  597. // Add a reasonable default event handler if none was specified.
  598. if (isset($element['#ajax']) && !isset($element['#ajax']['event'])) {
  599. switch ($element['#type']) {
  600. case 'submit':
  601. case 'button':
  602. case 'image_button':
  603. // Pressing the ENTER key within a textfield triggers the click event of
  604. // the form's first submit button. Triggering Ajax in this situation
  605. // leads to problems, like breaking autocomplete textfields, so we bind
  606. // to mousedown instead of click.
  607. // @see http://drupal.org/node/216059
  608. $element['#ajax']['event'] = 'mousedown';
  609. // Retain keyboard accessibility by setting 'keypress'. This causes
  610. // ajax.js to trigger 'event' when SPACE or ENTER are pressed while the
  611. // button has focus.
  612. $element['#ajax']['keypress'] = TRUE;
  613. // Binding to mousedown rather than click means that it is possible to
  614. // trigger a click by pressing the mouse, holding the mouse button down
  615. // until the Ajax request is complete and the button is re-enabled, and
  616. // then releasing the mouse button. Set 'prevent' so that ajax.js binds
  617. // an additional handler to prevent such a click from triggering a
  618. // non-Ajax form submission. This also prevents a textfield's ENTER
  619. // press triggering this button's non-Ajax form submission behavior.
  620. if (!isset($element['#ajax']['prevent'])) {
  621. $element['#ajax']['prevent'] = 'click';
  622. }
  623. break;
  624. case 'password':
  625. case 'textfield':
  626. case 'textarea':
  627. $element['#ajax']['event'] = 'blur';
  628. break;
  629. case 'radio':
  630. case 'checkbox':
  631. case 'select':
  632. $element['#ajax']['event'] = 'change';
  633. break;
  634. case 'link':
  635. $element['#ajax']['event'] = 'click';
  636. break;
  637. default:
  638. return $element;
  639. }
  640. }
  641. // Attach JavaScript settings to the element.
  642. if (isset($element['#ajax']['event'])) {
  643. $element['#attached']['library'][] = array('system', 'jquery.form');
  644. $element['#attached']['library'][] = array('system', 'drupal.ajax');
  645. $settings = $element['#ajax'];
  646. // Assign default settings.
  647. $settings += array(
  648. 'path' => 'system/ajax',
  649. 'options' => array(),
  650. );
  651. // @todo Legacy support. Remove in Drupal 8.
  652. if (isset($settings['method']) && $settings['method'] == 'replace') {
  653. $settings['method'] = 'replaceWith';
  654. }
  655. // Change path to URL.
  656. $settings['url'] = url($settings['path'], $settings['options']);
  657. unset($settings['path'], $settings['options']);
  658. // Add special data to $settings['submit'] so that when this element
  659. // triggers an Ajax submission, Drupal's form processing can determine which
  660. // element triggered it.
  661. // @see _form_element_triggered_scripted_submission()
  662. if (isset($settings['trigger_as'])) {
  663. // An element can add a 'trigger_as' key within #ajax to make the element
  664. // submit as though another one (for example, a non-button can use this
  665. // to submit the form as though a button were clicked). When using this,
  666. // the 'name' key is always required to identify the element to trigger
  667. // as. The 'value' key is optional, and only needed when multiple elements
  668. // share the same name, which is commonly the case for buttons.
  669. $settings['submit']['_triggering_element_name'] = $settings['trigger_as']['name'];
  670. if (isset($settings['trigger_as']['value'])) {
  671. $settings['submit']['_triggering_element_value'] = $settings['trigger_as']['value'];
  672. }
  673. unset($settings['trigger_as']);
  674. }
  675. elseif (isset($element['#name'])) {
  676. // Most of the time, elements can submit as themselves, in which case the
  677. // 'trigger_as' key isn't needed, and the element's name is used.
  678. $settings['submit']['_triggering_element_name'] = $element['#name'];
  679. // If the element is a (non-image) button, its name may not identify it
  680. // uniquely, in which case a match on value is also needed.
  681. // @see _form_button_was_clicked()
  682. if (isset($element['#button_type']) && empty($element['#has_garbage_value'])) {
  683. $settings['submit']['_triggering_element_value'] = $element['#value'];
  684. }
  685. }
  686. // Convert a simple #ajax['progress'] string into an array.
  687. if (isset($settings['progress']) && is_string($settings['progress'])) {
  688. $settings['progress'] = array('type' => $settings['progress']);
  689. }
  690. // Change progress path to a full URL.
  691. if (isset($settings['progress']['path'])) {
  692. $settings['progress']['url'] = url($settings['progress']['path']);
  693. unset($settings['progress']['path']);
  694. }
  695. $element['#attached']['js'][] = array(
  696. 'type' => 'setting',
  697. 'data' => array('ajax' => array($element['#id'] => $settings)),
  698. );
  699. // Indicate that Ajax processing was successful.
  700. $element['#ajax_processed'] = TRUE;
  701. }
  702. return $element;
  703. }
  704. /**
  705. * @} End of "defgroup ajax".
  706. */
  707. /**
  708. * @defgroup ajax_commands Ajax framework commands
  709. * @{
  710. * Functions to create various Ajax commands.
  711. *
  712. * These functions can be used to create arrays for use with the
  713. * ajax_render() function.
  714. */
  715. /**
  716. * Creates a Drupal Ajax 'alert' command.
  717. *
  718. * The 'alert' command instructs the client to display a JavaScript alert
  719. * dialog box.
  720. *
  721. * This command is implemented by Drupal.ajax.prototype.commands.alert()
  722. * defined in misc/ajax.js.
  723. *
  724. * @param $text
  725. * The message string to display to the user.
  726. *
  727. * @return
  728. * An array suitable for use with the ajax_render() function.
  729. */
  730. function ajax_command_alert($text) {
  731. return array(
  732. 'command' => 'alert',
  733. 'text' => $text,
  734. );
  735. }
  736. /**
  737. * Creates a Drupal Ajax 'insert' command using the method in #ajax['method'].
  738. *
  739. * This command instructs the client to insert the given HTML using whichever
  740. * jQuery DOM manipulation method has been specified in the #ajax['method']
  741. * variable of the element that triggered the request.
  742. *
  743. * This command is implemented by Drupal.ajax.prototype.commands.insert()
  744. * defined in misc/ajax.js.
  745. *
  746. * @param $selector
  747. * A jQuery selector string. If the command is a response to a request from
  748. * an #ajax form element then this value can be NULL.
  749. * @param $html
  750. * The data to use with the jQuery method.
  751. * @param $settings
  752. * An optional array of settings that will be used for this command only.
  753. *
  754. * @return
  755. * An array suitable for use with the ajax_render() function.
  756. */
  757. function ajax_command_insert($selector, $html, $settings = NULL) {
  758. return array(
  759. 'command' => 'insert',
  760. 'method' => NULL,
  761. 'selector' => $selector,
  762. 'data' => $html,
  763. 'settings' => $settings,
  764. );
  765. }
  766. /**
  767. * Creates a Drupal Ajax 'insert/replaceWith' command.
  768. *
  769. * The 'insert/replaceWith' command instructs the client to use jQuery's
  770. * replaceWith() method to replace each element matched matched by the given
  771. * selector with the given HTML.
  772. *
  773. * This command is implemented by Drupal.ajax.prototype.commands.insert()
  774. * defined in misc/ajax.js.
  775. *
  776. * @param $selector
  777. * A jQuery selector string. If the command is a response to a request from
  778. * an #ajax form element then this value can be NULL.
  779. * @param $html
  780. * The data to use with the jQuery replaceWith() method.
  781. * @param $settings
  782. * An optional array of settings that will be used for this command only.
  783. *
  784. * @return
  785. * An array suitable for use with the ajax_render() function.
  786. *
  787. * See
  788. * @link http://docs.jquery.com/Manipulation/replaceWith#content jQuery replaceWith command @endlink
  789. */
  790. function ajax_command_replace($selector, $html, $settings = NULL) {
  791. return array(
  792. 'command' => 'insert',
  793. 'method' => 'replaceWith',
  794. 'selector' => $selector,
  795. 'data' => $html,
  796. 'settings' => $settings,
  797. );
  798. }
  799. /**
  800. * Creates a Drupal Ajax 'insert/html' command.
  801. *
  802. * The 'insert/html' command instructs the client to use jQuery's html()
  803. * method to set the HTML content of each element matched by the given
  804. * selector while leaving the outer tags intact.
  805. *
  806. * This command is implemented by Drupal.ajax.prototype.commands.insert()
  807. * defined in misc/ajax.js.
  808. *
  809. * @param $selector
  810. * A jQuery selector string. If the command is a response to a request from
  811. * an #ajax form element then this value can be NULL.
  812. * @param $html
  813. * The data to use with the jQuery html() method.
  814. * @param $settings
  815. * An optional array of settings that will be used for this command only.
  816. *
  817. * @return
  818. * An array suitable for use with the ajax_render() function.
  819. *
  820. * @see http://docs.jquery.com/Attributes/html#val
  821. */
  822. function ajax_command_html($selector, $html, $settings = NULL) {
  823. return array(
  824. 'command' => 'insert',
  825. 'method' => 'html',
  826. 'selector' => $selector,
  827. 'data' => $html,
  828. 'settings' => $settings,
  829. );
  830. }
  831. /**
  832. * Creates a Drupal Ajax 'insert/prepend' command.
  833. *
  834. * The 'insert/prepend' command instructs the client to use jQuery's prepend()
  835. * method to prepend the given HTML content to the inside each element matched
  836. * by the given selector.
  837. *
  838. * This command is implemented by Drupal.ajax.prototype.commands.insert()
  839. * defined in misc/ajax.js.
  840. *
  841. * @param $selector
  842. * A jQuery selector string. If the command is a response to a request from
  843. * an #ajax form element then this value can be NULL.
  844. * @param $html
  845. * The data to use with the jQuery prepend() method.
  846. * @param $settings
  847. * An optional array of settings that will be used for this command only.
  848. *
  849. * @return
  850. * An array suitable for use with the ajax_render() function.
  851. *
  852. * @see http://docs.jquery.com/Manipulation/prepend#content
  853. */
  854. function ajax_command_prepend($selector, $html, $settings = NULL) {
  855. return array(
  856. 'command' => 'insert',
  857. 'method' => 'prepend',
  858. 'selector' => $selector,
  859. 'data' => $html,
  860. 'settings' => $settings,
  861. );
  862. }
  863. /**
  864. * Creates a Drupal Ajax 'insert/append' command.
  865. *
  866. * The 'insert/append' command instructs the client to use jQuery's append()
  867. * method to append the given HTML content to the inside of each element matched
  868. * by the given selector.
  869. *
  870. * This command is implemented by Drupal.ajax.prototype.commands.insert()
  871. * defined in misc/ajax.js.
  872. *
  873. * @param $selector
  874. * A jQuery selector string. If the command is a response to a request from
  875. * an #ajax form element then this value can be NULL.
  876. * @param $html
  877. * The data to use with the jQuery append() method.
  878. * @param $settings
  879. * An optional array of settings that will be used for this command only.
  880. *
  881. * @return
  882. * An array suitable for use with the ajax_render() function.
  883. *
  884. * @see http://docs.jquery.com/Manipulation/append#content
  885. */
  886. function ajax_command_append($selector, $html, $settings = NULL) {
  887. return array(
  888. 'command' => 'insert',
  889. 'method' => 'append',
  890. 'selector' => $selector,
  891. 'data' => $html,
  892. 'settings' => $settings,
  893. );
  894. }
  895. /**
  896. * Creates a Drupal Ajax 'insert/after' command.
  897. *
  898. * The 'insert/after' command instructs the client to use jQuery's after()
  899. * method to insert the given HTML content after each element matched by
  900. * the given selector.
  901. *
  902. * This command is implemented by Drupal.ajax.prototype.commands.insert()
  903. * defined in misc/ajax.js.
  904. *
  905. * @param $selector
  906. * A jQuery selector string. If the command is a response to a request from
  907. * an #ajax form element then this value can be NULL.
  908. * @param $html
  909. * The data to use with the jQuery after() method.
  910. * @param $settings
  911. * An optional array of settings that will be used for this command only.
  912. *
  913. * @return
  914. * An array suitable for use with the ajax_render() function.
  915. *
  916. * @see http://docs.jquery.com/Manipulation/after#content
  917. */
  918. function ajax_command_after($selector, $html, $settings = NULL) {
  919. return array(
  920. 'command' => 'insert',
  921. 'method' => 'after',
  922. 'selector' => $selector,
  923. 'data' => $html,
  924. 'settings' => $settings,
  925. );
  926. }
  927. /**
  928. * Creates a Drupal Ajax 'insert/before' command.
  929. *
  930. * The 'insert/before' command instructs the client to use jQuery's before()
  931. * method to insert the given HTML content before each of elements matched by
  932. * the given selector.
  933. *
  934. * This command is implemented by Drupal.ajax.prototype.commands.insert()
  935. * defined in misc/ajax.js.
  936. *
  937. * @param $selector
  938. * A jQuery selector string. If the command is a response to a request from
  939. * an #ajax form element then this value can be NULL.
  940. * @param $html
  941. * The data to use with the jQuery before() method.
  942. * @param $settings
  943. * An optional array of settings that will be used for this command only.
  944. *
  945. * @return
  946. * An array suitable for use with the ajax_render() function.
  947. *
  948. * @see http://docs.jquery.com/Manipulation/before#content
  949. */
  950. function ajax_command_before($selector, $html, $settings = NULL) {
  951. return array(
  952. 'command' => 'insert',
  953. 'method' => 'before',
  954. 'selector' => $selector,
  955. 'data' => $html,
  956. 'settings' => $settings,
  957. );
  958. }
  959. /**
  960. * Creates a Drupal Ajax 'remove' command.
  961. *
  962. * The 'remove' command instructs the client to use jQuery's remove() method
  963. * to remove each of elements matched by the given selector, and everything
  964. * within them.
  965. *
  966. * This command is implemented by Drupal.ajax.prototype.commands.remove()
  967. * defined in misc/ajax.js.
  968. *
  969. * @param $selector
  970. * A jQuery selector string. If the command is a response to a request from
  971. * an #ajax form element then this value can be NULL.
  972. *
  973. * @return
  974. * An array suitable for use with the ajax_render() function.
  975. *
  976. * @see http://docs.jquery.com/Manipulation/remove#expr
  977. */
  978. function ajax_command_remove($selector) {
  979. return array(
  980. 'command' => 'remove',
  981. 'selector' => $selector,
  982. );
  983. }
  984. /**
  985. * Creates a Drupal Ajax 'changed' command.
  986. *
  987. * This command instructs the client to mark each of the elements matched by the
  988. * given selector as 'ajax-changed'.
  989. *
  990. * This command is implemented by Drupal.ajax.prototype.commands.changed()
  991. * defined in misc/ajax.js.
  992. *
  993. * @param $selector
  994. * A jQuery selector string. If the command is a response to a request from
  995. * an #ajax form element then this value can be NULL.
  996. * @param $asterisk
  997. * An optional CSS selector which must be inside $selector. If specified,
  998. * an asterisk will be appended to the HTML inside the $asterisk selector.
  999. *
  1000. * @return
  1001. * An array suitable for use with the ajax_render() function.
  1002. */
  1003. function ajax_command_changed($selector, $asterisk = '') {
  1004. return array(
  1005. 'command' => 'changed',
  1006. 'selector' => $selector,
  1007. 'asterisk' => $asterisk,
  1008. );
  1009. }
  1010. /**
  1011. * Creates a Drupal Ajax 'css' command.
  1012. *
  1013. * The 'css' command will instruct the client to use the jQuery css() method
  1014. * to apply the CSS arguments to elements matched by the given selector.
  1015. *
  1016. * This command is implemented by Drupal.ajax.prototype.commands.css()
  1017. * defined in misc/ajax.js.
  1018. *
  1019. * @param $selector
  1020. * A jQuery selector string. If the command is a response to a request from
  1021. * an #ajax form element then this value can be NULL.
  1022. * @param $argument
  1023. * An array of key/value pairs to set in the CSS for the selector.
  1024. *
  1025. * @return
  1026. * An array suitable for use with the ajax_render() function.
  1027. *
  1028. * @see http://docs.jquery.com/CSS/css#properties
  1029. */
  1030. function ajax_command_css($selector, $argument) {
  1031. return array(
  1032. 'command' => 'css',
  1033. 'selector' => $selector,
  1034. 'argument' => $argument,
  1035. );
  1036. }
  1037. /**
  1038. * Creates a Drupal Ajax 'settings' command.
  1039. *
  1040. * The 'settings' command instructs the client either to use the given array as
  1041. * the settings for ajax-loaded content or to extend Drupal.settings with the
  1042. * given array, depending on the value of the $merge parameter.
  1043. *
  1044. * This command is implemented by Drupal.ajax.prototype.commands.settings()
  1045. * defined in misc/ajax.js.
  1046. *
  1047. * @param $argument
  1048. * An array of key/value pairs to add to the settings. This will be utilized
  1049. * for all commands after this if they do not include their own settings
  1050. * array.
  1051. * @param $merge
  1052. * Whether or not the passed settings in $argument should be merged into the
  1053. * global Drupal.settings on the page. By default (FALSE), the settings that
  1054. * are passed to Drupal.attachBehaviors will not include the global
  1055. * Drupal.settings.
  1056. *
  1057. * @return
  1058. * An array suitable for use with the ajax_render() function.
  1059. */
  1060. function ajax_command_settings($argument, $merge = FALSE) {
  1061. return array(
  1062. 'command' => 'settings',
  1063. 'settings' => $argument,
  1064. 'merge' => $merge,
  1065. );
  1066. }
  1067. /**
  1068. * Creates a Drupal Ajax 'data' command.
  1069. *
  1070. * The 'data' command instructs the client to attach the name=value pair of
  1071. * data to the selector via jQuery's data cache.
  1072. *
  1073. * This command is implemented by Drupal.ajax.prototype.commands.data()
  1074. * defined in misc/ajax.js.
  1075. *
  1076. * @param $selector
  1077. * A jQuery selector string. If the command is a response to a request from
  1078. * an #ajax form element then this value can be NULL.
  1079. * @param $name
  1080. * The name or key (in the key value pair) of the data attached to this
  1081. * selector.
  1082. * @param $value
  1083. * The value of the data. Not just limited to strings can be any format.
  1084. *
  1085. * @return
  1086. * An array suitable for use with the ajax_render() function.
  1087. *
  1088. * @see http://docs.jquery.com/Core/data#namevalue
  1089. */
  1090. function ajax_command_data($selector, $name, $value) {
  1091. return array(
  1092. 'command' => 'data',
  1093. 'selector' => $selector,
  1094. 'name' => $name,
  1095. 'value' => $value,
  1096. );
  1097. }
  1098. /**
  1099. * Creates a Drupal Ajax 'invoke' command.
  1100. *
  1101. * The 'invoke' command will instruct the client to invoke the given jQuery
  1102. * method with the supplied arguments on the elements matched by the given
  1103. * selector. Intended for simple jQuery commands, such as attr(), addClass(),
  1104. * removeClass(), toggleClass(), etc.
  1105. *
  1106. * This command is implemented by Drupal.ajax.prototype.commands.invoke()
  1107. * defined in misc/ajax.js.
  1108. *
  1109. * @param $selector
  1110. * A jQuery selector string. If the command is a response to a request from
  1111. * an #ajax form element then this value can be NULL.
  1112. * @param $method
  1113. * The jQuery method to invoke.
  1114. * @param $arguments
  1115. * (optional) A list of arguments to the jQuery $method, if any.
  1116. *
  1117. * @return
  1118. * An array suitable for use with the ajax_render() function.
  1119. */
  1120. function ajax_command_invoke($selector, $method, array $arguments = array()) {
  1121. return array(
  1122. 'command' => 'invoke',
  1123. 'selector' => $selector,
  1124. 'method' => $method,
  1125. 'arguments' => $arguments,
  1126. );
  1127. }
  1128. /**
  1129. * Creates a Drupal Ajax 'restripe' command.
  1130. *
  1131. * The 'restripe' command instructs the client to restripe a table. This is
  1132. * usually used after a table has been modified by a replace or append command.
  1133. *
  1134. * This command is implemented by Drupal.ajax.prototype.commands.restripe()
  1135. * defined in misc/ajax.js.
  1136. *
  1137. * @param $selector
  1138. * A jQuery selector string.
  1139. *
  1140. * @return
  1141. * An array suitable for use with the ajax_render() function.
  1142. */
  1143. function ajax_command_restripe($selector) {
  1144. return array(
  1145. 'command' => 'restripe',
  1146. 'selector' => $selector,
  1147. );
  1148. }