ajax_forms_test.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 '#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. // Shows the Ajax 'add_css' command.
  232. $form['add_css_command_example'] = array(
  233. '#type' => 'submit',
  234. '#value' => t("AJAX 'add_css' command"),
  235. '#ajax' => array(
  236. 'callback' => 'ajax_forms_test_advanced_commands_add_css_callback',
  237. ),
  238. );
  239. $form['submit'] = array(
  240. '#type' => 'submit',
  241. '#value' => t('Submit'),
  242. );
  243. return $form;
  244. }
  245. /**
  246. * Ajax callback for 'after'.
  247. */
  248. function ajax_forms_test_advanced_commands_after_callback($form, $form_state) {
  249. $selector = '#after_div';
  250. $commands = array();
  251. $commands[] = ajax_command_after($selector, "This will be placed after");
  252. return array('#type' => 'ajax', '#commands' => $commands);
  253. }
  254. /**
  255. * Ajax callback for 'alert'.
  256. */
  257. function ajax_forms_test_advanced_commands_alert_callback($form, $form_state) {
  258. $commands = array();
  259. $commands[] = ajax_command_alert("Alert");
  260. return array('#type' => 'ajax', '#commands' => $commands);
  261. }
  262. /**
  263. * Ajax callback for 'append'.
  264. */
  265. function ajax_forms_test_advanced_commands_append_callback($form, $form_state) {
  266. $selector = '#append_div';
  267. $commands = array();
  268. $commands[] = ajax_command_append($selector, "Appended text");
  269. return array('#type' => 'ajax', '#commands' => $commands);
  270. }
  271. /**
  272. * Ajax callback for 'before'.
  273. */
  274. function ajax_forms_test_advanced_commands_before_callback($form, $form_state) {
  275. $selector = '#before_div';
  276. $commands = array();
  277. $commands[] = ajax_command_before($selector, "Before text");
  278. return array('#type' => 'ajax', '#commands' => $commands);
  279. }
  280. /**
  281. * Ajax callback for 'changed'.
  282. */
  283. function ajax_forms_test_advanced_commands_changed_callback($form, $form_state) {
  284. $commands[] = ajax_command_changed('#changed_div');
  285. return array('#type' => 'ajax', '#commands' => $commands);
  286. }
  287. /**
  288. * Ajax callback for 'changed' with asterisk marking inner div.
  289. */
  290. function ajax_forms_test_advanced_commands_changed_asterisk_callback($form, $form_state) {
  291. $commands = array();
  292. $commands[] = ajax_command_changed('#changed_div', '#changed_div_mark_this');
  293. return array('#type' => 'ajax', '#commands' => $commands);
  294. }
  295. /**
  296. * Ajax callback for 'css'.
  297. */
  298. function ajax_forms_test_advanced_commands_css_callback($form, $form_state) {
  299. $selector = '#css_div';
  300. $color = 'blue';
  301. $commands = array();
  302. $commands[] = ajax_command_css($selector, array('background-color' => $color));
  303. return array('#type' => 'ajax', '#commands' => $commands);
  304. }
  305. /**
  306. * Ajax callback for 'data'.
  307. */
  308. function ajax_forms_test_advanced_commands_data_callback($form, $form_state) {
  309. $selector = '#data_div';
  310. $commands = array();
  311. $commands[] = ajax_command_data($selector, 'testkey', 'testvalue');
  312. return array('#type' => 'ajax', '#commands' => $commands);
  313. }
  314. /**
  315. * Ajax callback for 'invoke'.
  316. */
  317. function ajax_forms_test_advanced_commands_invoke_callback($form, $form_state) {
  318. $commands = array();
  319. $commands[] = ajax_command_invoke('#invoke_div', 'addClass', array('error'));
  320. return array('#type' => 'ajax', '#commands' => $commands);
  321. }
  322. /**
  323. * Ajax callback for 'html'.
  324. */
  325. function ajax_forms_test_advanced_commands_html_callback($form, $form_state) {
  326. $commands = array();
  327. $commands[] = ajax_command_html('#html_div', 'replacement text');
  328. return array('#type' => 'ajax', '#commands' => $commands);
  329. }
  330. /**
  331. * Ajax callback for 'insert'.
  332. */
  333. function ajax_forms_test_advanced_commands_insert_callback($form, $form_state) {
  334. $commands = array();
  335. $commands[] = ajax_command_insert('#insert_div', 'insert replacement text');
  336. return array('#type' => 'ajax', '#commands' => $commands);
  337. }
  338. /**
  339. * Ajax callback for 'prepend'.
  340. */
  341. function ajax_forms_test_advanced_commands_prepend_callback($form, $form_state) {
  342. $commands = array();
  343. $commands[] = ajax_command_prepend('#prepend_div', "prepended text");
  344. return array('#type' => 'ajax', '#commands' => $commands);
  345. }
  346. /**
  347. * Ajax callback for 'remove'.
  348. */
  349. function ajax_forms_test_advanced_commands_remove_callback($form, $form_state) {
  350. $commands = array();
  351. $commands[] = ajax_command_remove('#remove_text');
  352. return array('#type' => 'ajax', '#commands' => $commands);
  353. }
  354. /**
  355. * Ajax callback for 'restripe'.
  356. */
  357. function ajax_forms_test_advanced_commands_restripe_callback($form, $form_state) {
  358. $commands = array();
  359. $commands[] = ajax_command_restripe('#restripe_table');
  360. return array('#type' => 'ajax', '#commands' => $commands);
  361. }
  362. /**
  363. * Ajax callback for 'settings'.
  364. */
  365. function ajax_forms_test_advanced_commands_settings_callback($form, $form_state) {
  366. $commands = array();
  367. $setting['ajax_forms_test']['foo'] = 42;
  368. $commands[] = ajax_command_settings($setting);
  369. return array('#type' => 'ajax', '#commands' => $commands);
  370. }
  371. /**
  372. * Ajax callback for 'add_css'.
  373. */
  374. function ajax_forms_test_advanced_commands_add_css_callback($form, $form_state) {
  375. $commands = array();
  376. $commands[] = ajax_command_add_css('my/file.css');
  377. return array('#type' => 'ajax', '#commands' => $commands);
  378. }
  379. /**
  380. * This form and its related submit and callback functions demonstrate
  381. * not validating another form element when a single Ajax element is triggered.
  382. *
  383. * The "drivertext" element is an Ajax-enabled textfield, free-form.
  384. * The "required_field" element is a textfield marked required.
  385. *
  386. * The correct behavior is that the Ajax-enabled drivertext element should
  387. * be able to trigger without causing validation of the "required_field".
  388. */
  389. function ajax_forms_test_validation_form($form, &$form_state) {
  390. $form['drivertext'] = array(
  391. '#title' => t('AJAX-enabled textfield.'),
  392. '#description' => t("When this one AJAX-triggers and the spare required field is empty, you should not get an error."),
  393. '#type' => 'textfield',
  394. '#default_value' => !empty($form_state['values']['drivertext']) ? $form_state['values']['drivertext'] : "",
  395. '#ajax' => array(
  396. 'callback' => 'ajax_forms_test_validation_form_callback',
  397. 'wrapper' => 'message_area',
  398. 'method' => 'replace',
  399. ),
  400. '#suffix' => '<div id="message_area"></div>',
  401. );
  402. $form['spare_required_field'] = array(
  403. '#title' => t("Spare Required Field"),
  404. '#type' => 'textfield',
  405. '#required' => TRUE,
  406. );
  407. $form['submit'] = array(
  408. '#type' => 'submit',
  409. '#value' => t('Submit'),
  410. );
  411. return $form;
  412. }
  413. /**
  414. * Submit handler for the validation form.
  415. */
  416. function ajax_forms_test_validation_form_submit($form, $form_state) {
  417. drupal_set_message(t("Validation form submitted"));
  418. }
  419. /**
  420. * Ajax callback for the 'drivertext' element of the validation form.
  421. */
  422. function ajax_forms_test_validation_form_callback($form, $form_state) {
  423. drupal_set_message("ajax_forms_test_validation_form_callback invoked");
  424. 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'])));
  425. return '<div id="message_area">ajax_forms_test_validation_form_callback at ' . date('c') . '</div>';
  426. }
  427. /**
  428. * Form builder: Builds a form that triggers a simple AJAX callback.
  429. */
  430. function ajax_forms_test_lazy_load_form($form, &$form_state) {
  431. $form['add_files'] = array(
  432. '#type' => 'checkbox',
  433. '#default_value' => FALSE,
  434. );
  435. $form['submit'] = array(
  436. '#type' => 'submit',
  437. '#value' => t('Submit'),
  438. '#ajax' => array(
  439. 'callback' => 'ajax_forms_test_lazy_load_form_ajax',
  440. ),
  441. );
  442. return $form;
  443. }
  444. /**
  445. * Form submit handler: Adds JavaScript and CSS that wasn't on the original form.
  446. */
  447. function ajax_forms_test_lazy_load_form_submit($form, &$form_state) {
  448. if ($form_state['values']['add_files']) {
  449. drupal_add_js(array('ajax_forms_test_lazy_load_form_submit' => 'executed'), 'setting');
  450. drupal_add_css(drupal_get_path('module', 'system') . '/system.admin.css');
  451. drupal_add_js(drupal_get_path('module', 'system') . '/system.js');
  452. }
  453. $form_state['rebuild'] = TRUE;
  454. }
  455. /**
  456. * AJAX callback for the ajax_forms_test_lazy_load_form() form.
  457. *
  458. * This function returns nothing, because all we're interested in testing is
  459. * ajax_render() adding commands for JavaScript and CSS added during the page
  460. * request, such as the ones added in ajax_forms_test_lazy_load_form_submit().
  461. */
  462. function ajax_forms_test_lazy_load_form_ajax($form, &$form_state) {
  463. return NULL;
  464. }