ajax_forms_test.module

You are here

  1. 7 modules/simpletest/tests/ajax_forms_test.module
  2. 8 core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module

Simpletest mock module for Ajax forms testing.

Functions

Namesort descending Description
ajax_forms_test_advanced_commands_after_callback Ajax callback for 'after'.
ajax_forms_test_advanced_commands_alert_callback Ajax callback for 'alert'.
ajax_forms_test_advanced_commands_append_callback Ajax callback for 'append'.
ajax_forms_test_advanced_commands_before_callback Ajax callback for 'before'.
ajax_forms_test_advanced_commands_changed_asterisk_callback Ajax callback for 'changed' with asterisk marking inner div.
ajax_forms_test_advanced_commands_changed_callback Ajax callback for 'changed'.
ajax_forms_test_advanced_commands_css_callback Ajax callback for 'css'.
ajax_forms_test_advanced_commands_data_callback Ajax callback for 'data'.
ajax_forms_test_advanced_commands_html_callback Ajax callback for 'html'.
ajax_forms_test_advanced_commands_insert_callback Ajax callback for 'insert'.
ajax_forms_test_advanced_commands_invoke_callback Ajax callback for 'invoke'.
ajax_forms_test_advanced_commands_prepend_callback Ajax callback for 'prepend'.
ajax_forms_test_advanced_commands_remove_callback Ajax callback for 'remove'.
ajax_forms_test_advanced_commands_restripe_callback Ajax callback for 'restripe'.
ajax_forms_test_advanced_commands_settings_callback Ajax callback for 'settings'.
ajax_forms_test_ajax_commands_form Form to display the Ajax Commands.
ajax_forms_test_lazy_load_form Form builder: Builds a form that triggers a simple AJAX callback.
ajax_forms_test_lazy_load_form_ajax AJAX callback for the ajax_forms_test_lazy_load_form() form.
ajax_forms_test_lazy_load_form_submit Form submit handler: Adds JavaScript and CSS that wasn't on the original form.
ajax_forms_test_menu Implements hook_menu().
ajax_forms_test_simple_form A basic form used to test form_state['values'] during callback.
ajax_forms_test_simple_form_checkbox_callback Ajax callback triggered by checkbox.
ajax_forms_test_simple_form_select_callback Ajax callback triggered by select.
ajax_forms_test_validation_form This form and its related submit and callback functions demonstrate not validating another form element when a single Ajax element is triggered.
ajax_forms_test_validation_form_callback Ajax callback for the 'drivertext' element of the validation form.
ajax_forms_test_validation_form_submit Submit handler for the validation form.

File

modules/simpletest/tests/ajax_forms_test.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Simpletest mock module for Ajax forms testing.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function ajax_forms_test_menu() {
  10. $items = array();
  11. $items['ajax_forms_test_get_form'] = array(
  12. 'title' => 'AJAX forms simple form test',
  13. 'page callback' => 'drupal_get_form',
  14. 'page arguments' => array('ajax_forms_test_simple_form'),
  15. 'access callback' => TRUE,
  16. );
  17. $items['ajax_forms_test_ajax_commands_form'] = array(
  18. 'title' => 'AJAX forms AJAX commands test',
  19. 'page callback' => 'drupal_get_form',
  20. 'page arguments' => array('ajax_forms_test_ajax_commands_form'),
  21. 'access callback' => TRUE,
  22. );
  23. $items['ajax_validation_test'] = array(
  24. 'title' => 'AJAX Validation Test',
  25. 'page callback' => 'drupal_get_form',
  26. 'page arguments' => array('ajax_forms_test_validation_form'),
  27. 'access callback' => TRUE,
  28. );
  29. $items['ajax_forms_test_lazy_load_form'] = array(
  30. 'title' => 'AJAX forms lazy load test',
  31. 'page callback' => 'drupal_get_form',
  32. 'page arguments' => array('ajax_forms_test_lazy_load_form'),
  33. 'access callback' => TRUE,
  34. );
  35. return $items;
  36. }
  37. /**
  38. * A basic form used to test form_state['values'] during callback.
  39. */
  40. function ajax_forms_test_simple_form($form, &$form_state) {
  41. $form = array();
  42. $form['select'] = array(
  43. '#type' => 'select',
  44. '#options' => array(
  45. 'red' => 'red',
  46. 'green' => 'green',
  47. 'blue' => 'blue'),
  48. '#ajax' => array(
  49. 'callback' => 'ajax_forms_test_simple_form_select_callback',
  50. ),
  51. '#suffix' => '<div id="ajax_selected_color">No color yet selected</div>',
  52. );
  53. $form['checkbox'] = array(
  54. '#type' => 'checkbox',
  55. '#title' => t('Test checkbox'),
  56. '#ajax' => array(
  57. 'callback' => 'ajax_forms_test_simple_form_checkbox_callback',
  58. ),
  59. '#suffix' => '<div id="ajax_checkbox_value">No action yet</div>',
  60. );
  61. $form['submit'] = array(
  62. '#type' => 'submit',
  63. '#value' => t('submit'),
  64. );
  65. return $form;
  66. }
  67. /**
  68. * Ajax callback triggered by select.
  69. */
  70. function ajax_forms_test_simple_form_select_callback($form, $form_state) {
  71. $commands = array();
  72. $commands[] = ajax_command_html('#ajax_selected_color', $form_state['values']['select']);
  73. $commands[] = ajax_command_data('#ajax_selected_color', 'form_state_value_select', $form_state['values']['select']);
  74. return array('#type' => 'ajax', '#commands' => $commands);
  75. }
  76. /**
  77. * Ajax callback triggered by checkbox.
  78. */
  79. function ajax_forms_test_simple_form_checkbox_callback($form, $form_state) {
  80. $commands = array();
  81. $commands[] = ajax_command_html('#ajax_checkbox_value', (int) $form_state['values']['checkbox']);
  82. $commands[] = ajax_command_data('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state['values']['checkbox']);
  83. return array('#type' => 'ajax', '#commands' => $commands);
  84. }
  85. /**
  86. * Form to display the Ajax Commands.
  87. */
  88. function ajax_forms_test_ajax_commands_form($form, &$form_state) {
  89. $form = array();
  90. // Shows the 'after' command with a callback generating commands.
  91. $form['after_command_example'] = array(
  92. '#value' => t("AJAX 'After': Click to put something after the div"),
  93. '#type' => 'submit',
  94. '#ajax' => array(
  95. 'callback' => 'ajax_forms_test_advanced_commands_after_callback',
  96. ),
  97. '#suffix' => '<div id="after_div">Something can be inserted after this</div>',
  98. );
  99. // Shows the 'alert' command.
  100. $form['alert_command_example'] = array(
  101. '#value' => t("AJAX 'Alert': Click to alert"),
  102. '#type' => 'submit',
  103. '#ajax' => array(
  104. 'callback' => 'ajax_forms_test_advanced_commands_alert_callback',
  105. ),
  106. );
  107. // Shows the 'append' command.
  108. $form['append_command_example'] = array(
  109. '#value' => t("AJAX 'Append': Click to append something"),
  110. '#type' => 'submit',
  111. '#ajax' => array(
  112. 'callback' => 'ajax_forms_test_advanced_commands_append_callback',
  113. ),
  114. '#suffix' => '<div id="append_div">Append inside this div</div>',
  115. );
  116. // Shows the 'before' command.
  117. $form['before_command_example'] = array(
  118. '#value' => t("AJAX 'before': Click to put something before the div"),
  119. '#type' => 'submit',
  120. '#ajax' => array(
  121. 'callback' => 'ajax_forms_test_advanced_commands_before_callback',
  122. ),
  123. '#suffix' => '<div id="before_div">Insert something before this.</div>',
  124. );
  125. // Shows the 'changed' command without asterisk.
  126. $form['changed_command_example'] = array(
  127. '#value' => t("AJAX changed: Click to mark div changed."),
  128. '#type' => 'submit',
  129. '#ajax' => array(
  130. 'callback' => 'ajax_forms_test_advanced_commands_changed_callback',
  131. ),
  132. '#suffix' => '<div id="changed_div"> <div id="changed_div_mark_this">This div can be marked as changed or not.</div></div>',
  133. );
  134. // Shows the 'changed' command adding the asterisk.
  135. $form['changed_command_asterisk_example'] = array(
  136. '#value' => t("AJAX changed: Click to mark div changed with asterisk."),
  137. '#type' => 'submit',
  138. '#ajax' => array(
  139. 'callback' => 'ajax_forms_test_advanced_commands_changed_asterisk_callback',
  140. ),
  141. );
  142. // Shows the Ajax 'css' command.
  143. $form['css_command_example'] = array(
  144. '#value' => t("Set the the '#box' div to be blue."),
  145. '#type' => 'submit',
  146. '#ajax' => array(
  147. 'callback' => 'ajax_forms_test_advanced_commands_css_callback',
  148. ),
  149. '#suffix' => '<div id="css_div" style="height: 50px; width: 50px; border: 1px solid black"> box</div>',
  150. );
  151. // Shows the Ajax 'data' command. But there is no use of this information,
  152. // as this would require a javascript client to use the data.
  153. $form['data_command_example'] = array(
  154. '#value' => t("AJAX data command: Issue command."),
  155. '#type' => 'submit',
  156. '#ajax' => array(
  157. 'callback' => 'ajax_forms_test_advanced_commands_data_callback',
  158. ),
  159. '#suffix' => '<div id="data_div">Data attached to this div.</div>',
  160. );
  161. // Shows the Ajax 'invoke' command.
  162. $form['invoke_command_example'] = array(
  163. '#value' => t("AJAX invoke command: Invoke addClass() method."),
  164. '#type' => 'submit',
  165. '#ajax' => array(
  166. 'callback' => 'ajax_forms_test_advanced_commands_invoke_callback',
  167. ),
  168. '#suffix' => '<div id="invoke_div">Original contents</div>',
  169. );
  170. // Shows the Ajax 'html' command.
  171. $form['html_command_example'] = array(
  172. '#value' => t("AJAX html: Replace the HTML in a selector."),
  173. '#type' => 'submit',
  174. '#ajax' => array(
  175. 'callback' => 'ajax_forms_test_advanced_commands_html_callback',
  176. ),
  177. '#suffix' => '<div id="html_div">Original contents</div>',
  178. );
  179. // Shows the Ajax 'insert' command.
  180. $form['insert_command_example'] = array(
  181. '#value' => t("AJAX insert: Let client insert based on #ajax['method']."),
  182. '#type' => 'submit',
  183. '#ajax' => array(
  184. 'callback' => 'ajax_forms_test_advanced_commands_insert_callback',
  185. 'method' => 'prepend',
  186. ),
  187. '#suffix' => '<div id="insert_div">Original contents</div>',
  188. );
  189. // Shows the Ajax 'prepend' command.
  190. $form['prepend_command_example'] = array(
  191. '#value' => t("AJAX 'prepend': Click to prepend something"),
  192. '#type' => 'submit',
  193. '#ajax' => array(
  194. 'callback' => 'ajax_forms_test_advanced_commands_prepend_callback',
  195. ),
  196. '#suffix' => '<div id="prepend_div">Something will be prepended to this div. </div>',
  197. );
  198. // Shows the Ajax 'remove' command.
  199. $form['remove_command_example'] = array(
  200. '#value' => t("AJAX 'remove': Click to remove text"),
  201. '#type' => 'submit',
  202. '#ajax' => array(
  203. 'callback' => 'ajax_forms_test_advanced_commands_remove_callback',
  204. ),
  205. '#suffix' => '<div id="remove_div"><div id="remove_text">text to be removed</div></div>',
  206. );
  207. // Shows the Ajax 'restripe' command.
  208. $form['restripe_command_example'] = array(
  209. '#type' => 'submit',
  210. '#value' => t("AJAX 'restripe' command"),
  211. '#ajax' => array(
  212. 'callback' => 'ajax_forms_test_advanced_commands_restripe_callback',
  213. ),
  214. '#suffix' => '<div id="restripe_div">
  215. <table id="restripe_table" style="border: 1px solid black" >
  216. <tr id="table-first"><td>first row</td></tr>
  217. <tr ><td>second row</td></tr>
  218. </table>
  219. </div>',
  220. );
  221. // Demonstrates the Ajax 'settings' command. The 'settings' command has
  222. // nothing visual to "show", but it can be tested via SimpleTest and via
  223. // Firebug.
  224. $form['settings_command_example'] = array(
  225. '#type' => 'submit',
  226. '#value' => t("AJAX 'settings' command"),
  227. '#ajax' => array(
  228. 'callback' => 'ajax_forms_test_advanced_commands_settings_callback',
  229. ),
  230. );
  231. $form['submit'] = array(
  232. '#type' => 'submit',
  233. '#value' => t('Submit'),
  234. );
  235. return $form;
  236. }
  237. /**
  238. * Ajax callback for 'after'.
  239. */
  240. function ajax_forms_test_advanced_commands_after_callback($form, $form_state) {
  241. $selector = '#after_div';
  242. $commands = array();
  243. $commands[] = ajax_command_after($selector, "This will be placed after");
  244. return array('#type' => 'ajax', '#commands' => $commands);
  245. }
  246. /**
  247. * Ajax callback for 'alert'.
  248. */
  249. function ajax_forms_test_advanced_commands_alert_callback($form, $form_state) {
  250. $commands = array();
  251. $commands[] = ajax_command_alert("Alert");
  252. return array('#type' => 'ajax', '#commands' => $commands);
  253. }
  254. /**
  255. * Ajax callback for 'append'.
  256. */
  257. function ajax_forms_test_advanced_commands_append_callback($form, $form_state) {
  258. $selector = '#append_div';
  259. $commands = array();
  260. $commands[] = ajax_command_append($selector, "Appended text");
  261. return array('#type' => 'ajax', '#commands' => $commands);
  262. }
  263. /**
  264. * Ajax callback for 'before'.
  265. */
  266. function ajax_forms_test_advanced_commands_before_callback($form, $form_state) {
  267. $selector = '#before_div';
  268. $commands = array();
  269. $commands[] = ajax_command_before($selector, "Before text");
  270. return array('#type' => 'ajax', '#commands' => $commands);
  271. }
  272. /**
  273. * Ajax callback for 'changed'.
  274. */
  275. function ajax_forms_test_advanced_commands_changed_callback($form, $form_state) {
  276. $commands[] = ajax_command_changed('#changed_div');
  277. return array('#type' => 'ajax', '#commands' => $commands);
  278. }
  279. /**
  280. * Ajax callback for 'changed' with asterisk marking inner div.
  281. */
  282. function ajax_forms_test_advanced_commands_changed_asterisk_callback($form, $form_state) {
  283. $commands = array();
  284. $commands[] = ajax_command_changed('#changed_div', '#changed_div_mark_this');
  285. return array('#type' => 'ajax', '#commands' => $commands);
  286. }
  287. /**
  288. * Ajax callback for 'css'.
  289. */
  290. function ajax_forms_test_advanced_commands_css_callback($form, $form_state) {
  291. $selector = '#css_div';
  292. $color = 'blue';
  293. $commands = array();
  294. $commands[] = ajax_command_css($selector, array('background-color' => $color));
  295. return array('#type' => 'ajax', '#commands' => $commands);
  296. }
  297. /**
  298. * Ajax callback for 'data'.
  299. */
  300. function ajax_forms_test_advanced_commands_data_callback($form, $form_state) {
  301. $selector = '#data_div';
  302. $commands = array();
  303. $commands[] = ajax_command_data($selector, 'testkey', 'testvalue');
  304. return array('#type' => 'ajax', '#commands' => $commands);
  305. }
  306. /**
  307. * Ajax callback for 'invoke'.
  308. */
  309. function ajax_forms_test_advanced_commands_invoke_callback($form, $form_state) {
  310. $commands = array();
  311. $commands[] = ajax_command_invoke('#invoke_div', 'addClass', array('error'));
  312. return array('#type' => 'ajax', '#commands' => $commands);
  313. }
  314. /**
  315. * Ajax callback for 'html'.
  316. */
  317. function ajax_forms_test_advanced_commands_html_callback($form, $form_state) {
  318. $commands = array();
  319. $commands[] = ajax_command_html('#html_div', 'replacement text');
  320. return array('#type' => 'ajax', '#commands' => $commands);
  321. }
  322. /**
  323. * Ajax callback for 'insert'.
  324. */
  325. function ajax_forms_test_advanced_commands_insert_callback($form, $form_state) {
  326. $commands = array();
  327. $commands[] = ajax_command_insert('#insert_div', 'insert replacement text');
  328. return array('#type' => 'ajax', '#commands' => $commands);
  329. }
  330. /**
  331. * Ajax callback for 'prepend'.
  332. */
  333. function ajax_forms_test_advanced_commands_prepend_callback($form, $form_state) {
  334. $commands = array();
  335. $commands[] = ajax_command_prepend('#prepend_div', "prepended text");
  336. return array('#type' => 'ajax', '#commands' => $commands);
  337. }
  338. /**
  339. * Ajax callback for 'remove'.
  340. */
  341. function ajax_forms_test_advanced_commands_remove_callback($form, $form_state) {
  342. $commands = array();
  343. $commands[] = ajax_command_remove('#remove_text');
  344. return array('#type' => 'ajax', '#commands' => $commands);
  345. }
  346. /**
  347. * Ajax callback for 'restripe'.
  348. */
  349. function ajax_forms_test_advanced_commands_restripe_callback($form, $form_state) {
  350. $commands = array();
  351. $commands[] = ajax_command_restripe('#restripe_table');
  352. return array('#type' => 'ajax', '#commands' => $commands);
  353. }
  354. /**
  355. * Ajax callback for 'settings'.
  356. */
  357. function ajax_forms_test_advanced_commands_settings_callback($form, $form_state) {
  358. $commands = array();
  359. $setting['ajax_forms_test']['foo'] = 42;
  360. $commands[] = ajax_command_settings($setting);
  361. return array('#type' => 'ajax', '#commands' => $commands);
  362. }
  363. /**
  364. * This form and its related submit and callback functions demonstrate
  365. * not validating another form element when a single Ajax element is triggered.
  366. *
  367. * The "drivertext" element is an Ajax-enabled textfield, free-form.
  368. * The "required_field" element is a textfield marked required.
  369. *
  370. * The correct behavior is that the Ajax-enabled drivertext element should
  371. * be able to trigger without causing validation of the "required_field".
  372. */
  373. function ajax_forms_test_validation_form($form, &$form_state) {
  374. $form['drivertext'] = array(
  375. '#title' => t('AJAX-enabled textfield.'),
  376. '#description' => t("When this one AJAX-triggers and the spare required field is empty, you should not get an error."),
  377. '#type' => 'textfield',
  378. '#default_value' => !empty($form_state['values']['drivertext']) ? $form_state['values']['drivertext'] : "",
  379. '#ajax' => array(
  380. 'callback' => 'ajax_forms_test_validation_form_callback',
  381. 'wrapper' => 'message_area',
  382. 'method' => 'replace',
  383. ),
  384. '#suffix' => '<div id="message_area"></div>',
  385. );
  386. $form['spare_required_field'] = array(
  387. '#title' => t("Spare Required Field"),
  388. '#type' => 'textfield',
  389. '#required' => TRUE,
  390. );
  391. $form['submit'] = array(
  392. '#type' => 'submit',
  393. '#value' => t('Submit'),
  394. );
  395. return $form;
  396. }
  397. /**
  398. * Submit handler for the validation form.
  399. */
  400. function ajax_forms_test_validation_form_submit($form, $form_state) {
  401. drupal_set_message(t("Validation form submitted"));
  402. }
  403. /**
  404. * Ajax callback for the 'drivertext' element of the validation form.
  405. */
  406. function ajax_forms_test_validation_form_callback($form, $form_state) {
  407. drupal_set_message("ajax_forms_test_validation_form_callback invoked");
  408. drupal_set_message(t("Callback: drivertext=%drivertext, spare_required_field=%spare_required_field", array('%drivertext' => $form_state['values']['drivertext'], '%spare_required_field' => $form_state['values']['spare_required_field'])));
  409. return '<div id="message_area">ajax_forms_test_validation_form_callback at ' . date('c') . '</div>';
  410. }
  411. /**
  412. * Form builder: Builds a form that triggers a simple AJAX callback.
  413. */
  414. function ajax_forms_test_lazy_load_form($form, &$form_state) {
  415. $form['add_files'] = array(
  416. '#type' => 'checkbox',
  417. '#default_value' => FALSE,
  418. );
  419. $form['submit'] = array(
  420. '#type' => 'submit',
  421. '#value' => t('Submit'),
  422. '#ajax' => array(
  423. 'callback' => 'ajax_forms_test_lazy_load_form_ajax',
  424. ),
  425. );
  426. return $form;
  427. }
  428. /**
  429. * Form submit handler: Adds JavaScript and CSS that wasn't on the original form.
  430. */
  431. function ajax_forms_test_lazy_load_form_submit($form, &$form_state) {
  432. if ($form_state['values']['add_files']) {
  433. drupal_add_js(array('ajax_forms_test_lazy_load_form_submit' => 'executed'), 'setting');
  434. drupal_add_css(drupal_get_path('module', 'system') . '/system.admin.css');
  435. drupal_add_js(drupal_get_path('module', 'system') . '/system.js');
  436. }
  437. $form_state['rebuild'] = TRUE;
  438. }
  439. /**
  440. * AJAX callback for the ajax_forms_test_lazy_load_form() form.
  441. *
  442. * This function returns nothing, because all we're interested in testing is
  443. * ajax_render() adding commands for JavaScript and CSS added during the page
  444. * request, such as the ones added in ajax_forms_test_lazy_load_form_submit().
  445. */
  446. function ajax_forms_test_lazy_load_form_ajax($form, &$form_state) {
  447. return NULL;
  448. }