ajax_forms_test.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. }