form_test.module 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  1. <?php
  2. /**
  3. * @file
  4. * Helper module for the form API tests.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function form_test_menu() {
  10. $items['form-test/alter'] = array(
  11. 'title' => 'Form altering test',
  12. 'page callback' => 'drupal_get_form',
  13. 'page arguments' => array('form_test_alter_form'),
  14. 'access arguments' => array('access content'),
  15. 'type' => MENU_CALLBACK,
  16. );
  17. $items['form-test/validate'] = array(
  18. 'title' => 'Form validation handlers test',
  19. 'page callback' => 'drupal_get_form',
  20. 'page arguments' => array('form_test_validate_form'),
  21. 'access arguments' => array('access content'),
  22. 'type' => MENU_CALLBACK,
  23. );
  24. $items['form-test/validate-required'] = array(
  25. 'title' => 'Form #required validation',
  26. 'page callback' => 'drupal_get_form',
  27. 'page arguments' => array('form_test_validate_required_form'),
  28. 'access callback' => TRUE,
  29. 'type' => MENU_CALLBACK,
  30. );
  31. $items['form-test/validate-required-no-title'] = array(
  32. 'title' => 'Form #required validation without #title',
  33. 'page callback' => 'drupal_get_form',
  34. 'page arguments' => array('form_test_validate_required_form_no_title'),
  35. 'access callback' => TRUE,
  36. 'type' => MENU_CALLBACK,
  37. );
  38. $items['form-test/validate-no-token'] = array(
  39. 'title' => 'Form validation without a CSRF token',
  40. 'page callback' => 'drupal_get_form',
  41. 'page arguments' => array('form_test_validate_no_token'),
  42. 'access callback' => TRUE,
  43. 'type' => MENU_CALLBACK,
  44. );
  45. $items['form-test/limit-validation-errors'] = array(
  46. 'title' => 'Form validation with some error suppression',
  47. 'page callback' => 'drupal_get_form',
  48. 'page arguments' => array('form_test_limit_validation_errors_form'),
  49. 'access arguments' => array('access content'),
  50. 'type' => MENU_CALLBACK,
  51. );
  52. $items['form_test/tableselect/multiple-true'] = array(
  53. 'title' => 'Tableselect checkboxes test',
  54. 'page callback' => 'drupal_get_form',
  55. 'page arguments' => array('_form_test_tableselect_multiple_true_form'),
  56. 'access arguments' => array('access content'),
  57. 'type' => MENU_CALLBACK,
  58. );
  59. $items['form_test/tableselect/multiple-false'] = array(
  60. 'title' => 'Tableselect radio button test',
  61. 'page callback' => 'drupal_get_form',
  62. 'page arguments' => array('_form_test_tableselect_multiple_false_form'),
  63. 'access arguments' => array('access content'),
  64. 'type' => MENU_CALLBACK,
  65. );
  66. $items['form_test/tableselect/empty-text'] = array(
  67. 'title' => 'Tableselect empty text test',
  68. 'page callback' => 'drupal_get_form',
  69. 'page arguments' => array('_form_test_tableselect_empty_form'),
  70. 'access arguments' => array('access content'),
  71. 'type' => MENU_CALLBACK,
  72. );
  73. $items['form_test/tableselect/advanced-select'] = array(
  74. 'title' => 'Tableselect js_select tests',
  75. 'page callback' => 'drupal_get_form',
  76. 'page arguments' => array('_form_test_tableselect_js_select_form'),
  77. 'access arguments' => array('access content'),
  78. 'type' => MENU_CALLBACK,
  79. );
  80. $items['form_test/vertical-tabs'] = array(
  81. 'title' => 'Vertical tabs tests',
  82. 'page callback' => 'drupal_get_form',
  83. 'page arguments' => array('_form_test_vertical_tabs_form'),
  84. 'access arguments' => array('access content'),
  85. 'type' => MENU_CALLBACK,
  86. );
  87. $items['form_test/form-storage'] = array(
  88. 'title' => 'Form storage test',
  89. 'page callback' => 'drupal_get_form',
  90. 'page arguments' => array('form_test_storage_form'),
  91. 'access arguments' => array('access content'),
  92. 'type' => MENU_CALLBACK,
  93. );
  94. $items['form_test/form-storage-legacy'] = array(
  95. 'title' => 'Emulate legacy AHAH-style ajax callback',
  96. 'page callback' => 'form_test_storage_legacy_handler',
  97. 'access arguments' => array('access content'),
  98. 'type' => MENU_CALLBACK,
  99. );
  100. $items['form_test/form-storage-page-cache'] = array(
  101. 'title' => 'Form storage with page cache test',
  102. 'page callback' => 'drupal_get_form',
  103. 'page arguments' => array('form_test_storage_page_cache_form'),
  104. 'access arguments' => array('access content'),
  105. 'type' => MENU_CALLBACK,
  106. );
  107. $items['form_test/wrapper-callback'] = array(
  108. 'title' => 'Form wrapper callback test',
  109. 'page callback' => 'form_test_wrapper_callback',
  110. 'page arguments' => array('form_test_wrapper_callback_form'),
  111. 'access arguments' => array('access content'),
  112. 'type' => MENU_CALLBACK,
  113. );
  114. $items['form_test/form-state-values-clean'] = array(
  115. 'title' => 'Form state values clearance test',
  116. 'page callback' => 'drupal_get_form',
  117. 'page arguments' => array('form_test_form_state_values_clean_form'),
  118. 'access arguments' => array('access content'),
  119. 'type' => MENU_CALLBACK,
  120. );
  121. $items['form_test/form-state-values-clean-advanced'] = array(
  122. 'title' => 'Form state values clearance advanced test',
  123. 'page callback' => 'drupal_get_form',
  124. 'page arguments' => array('form_test_form_state_values_clean_advanced_form'),
  125. 'access arguments' => array('access content'),
  126. 'type' => MENU_CALLBACK,
  127. );
  128. $items['form-test/checkbox'] = array(
  129. 'title' => t('Form test'),
  130. 'page callback' => 'drupal_get_form',
  131. 'page arguments' => array('_form_test_checkbox'),
  132. 'access callback' => TRUE,
  133. 'type' => MENU_CALLBACK,
  134. );
  135. $items['form-test/select'] = array(
  136. 'title' => t('Select'),
  137. 'page callback' => 'drupal_get_form',
  138. 'page arguments' => array('form_test_select'),
  139. 'access callback' => TRUE,
  140. );
  141. $items['form-test/checkboxes-radios'] = array(
  142. 'title' => t('Checkboxes, Radios'),
  143. 'page callback' => 'drupal_get_form',
  144. 'page arguments' => array('form_test_checkboxes_radios'),
  145. 'access callback' => TRUE,
  146. );
  147. $items['form-test/disabled-elements'] = array(
  148. 'title' => t('Form test'),
  149. 'page callback' => 'drupal_get_form',
  150. 'page arguments' => array('_form_test_disabled_elements'),
  151. 'access callback' => TRUE,
  152. 'type' => MENU_CALLBACK,
  153. );
  154. $items['form-test/input-forgery'] = array(
  155. 'title' => t('Form test'),
  156. 'page callback' => 'drupal_get_form',
  157. 'page arguments' => array('_form_test_input_forgery'),
  158. 'access callback' => TRUE,
  159. 'type' => MENU_CALLBACK,
  160. );
  161. $items['form-test/form-rebuild-preserve-values'] = array(
  162. 'title' => 'Form values preservation during rebuild test',
  163. 'page callback' => 'drupal_get_form',
  164. 'page arguments' => array('form_test_form_rebuild_preserve_values_form'),
  165. 'access arguments' => array('access content'),
  166. 'type' => MENU_CALLBACK,
  167. );
  168. $items['form-test/redirect'] = array(
  169. 'title' => 'Redirect test',
  170. 'page callback' => 'drupal_get_form',
  171. 'page arguments' => array('form_test_redirect'),
  172. 'access callback' => TRUE,
  173. 'type' => MENU_CALLBACK,
  174. );
  175. $items['form_test/form-labels'] = array(
  176. 'title' => 'Form label test',
  177. 'page callback' => 'drupal_get_form',
  178. 'page arguments' => array('form_label_test_form'),
  179. 'access arguments' => array('access content'),
  180. 'type' => MENU_CALLBACK,
  181. );
  182. $items['form-test/state-persist'] = array(
  183. 'title' => 'Form state persistence without storage',
  184. 'page callback' => 'drupal_get_form',
  185. 'page arguments' => array('form_test_state_persist'),
  186. 'access callback' => TRUE,
  187. 'type' => MENU_CALLBACK,
  188. );
  189. $items['form-test/clicked-button'] = array(
  190. 'title' => 'Clicked button test',
  191. 'page callback' => 'drupal_get_form',
  192. 'page arguments' => array('form_test_clicked_button'),
  193. 'access callback' => TRUE,
  194. 'type' => MENU_CALLBACK,
  195. );
  196. if (module_exists('node')) {
  197. $items['form-test/two-instances-of-same-form'] = array(
  198. 'title' => 'AJAX test with two form instances',
  199. 'page callback' => 'form_test_two_instances',
  200. 'access callback' => 'node_access',
  201. 'access arguments' => array('create', 'page'),
  202. 'file path' => drupal_get_path('module', 'node'),
  203. 'file' => 'node.pages.inc',
  204. 'type' => MENU_CALLBACK,
  205. );
  206. }
  207. $items['form-test/double-form'] = array(
  208. 'title' => 'Double form test',
  209. 'page callback' => 'form_test_double_form',
  210. 'access callback' => TRUE,
  211. 'type' => MENU_CALLBACK,
  212. );
  213. $items['form-test/load-include-menu'] = array(
  214. 'title' => 'FAPI test loading includes',
  215. 'page callback' => 'drupal_get_form',
  216. 'page arguments' => array('form_test_load_include_menu'),
  217. 'access callback' => TRUE,
  218. 'file' => 'form_test.file.inc',
  219. 'type' => MENU_CALLBACK,
  220. );
  221. $items['form-test/load-include-custom'] = array(
  222. 'title' => 'FAPI test loading includes',
  223. 'page callback' => 'drupal_get_form',
  224. 'page arguments' => array('form_test_load_include_custom'),
  225. 'access callback' => TRUE,
  226. 'type' => MENU_CALLBACK,
  227. );
  228. $items['form-test/checkboxes-zero'] = array(
  229. 'title' => 'FAPI test involving checkboxes and zero',
  230. 'page callback' => 'drupal_get_form',
  231. 'page arguments' => array('form_test_checkboxes_zero'),
  232. 'access callback' => TRUE,
  233. 'type' => MENU_CALLBACK,
  234. );
  235. return $items;
  236. }
  237. /**
  238. * Form submit handler to return form values as JSON.
  239. */
  240. function _form_test_submit_values_json($form, &$form_state) {
  241. drupal_json_output($form_state['values']);
  242. drupal_exit();
  243. }
  244. /**
  245. * Form builder for testing hook_form_alter() and hook_form_FORM_ID_alter().
  246. */
  247. function form_test_alter_form($form, &$form_state) {
  248. // Elements can be added as needed for future testing needs, but for now,
  249. // we're only testing alter hooks that do not require any elements added by
  250. // this function.
  251. return $form;
  252. }
  253. /**
  254. * Implements hook_form_FORM_ID_alter() on behalf of block.module.
  255. */
  256. function block_form_form_test_alter_form_alter(&$form, &$form_state) {
  257. drupal_set_message('block_form_form_test_alter_form_alter() executed.');
  258. }
  259. /**
  260. * Implements hook_form_alter().
  261. */
  262. function form_test_form_alter(&$form, &$form_state, $form_id) {
  263. if ($form_id == 'form_test_alter_form') {
  264. drupal_set_message('form_test_form_alter() executed.');
  265. }
  266. }
  267. /**
  268. * Implements hook_form_FORM_ID_alter().
  269. */
  270. function form_test_form_form_test_alter_form_alter(&$form, &$form_state) {
  271. drupal_set_message('form_test_form_form_test_alter_form_alter() executed.');
  272. }
  273. /**
  274. * Implements hook_form_FORM_ID_alter() on behalf of system.module.
  275. */
  276. function system_form_form_test_alter_form_alter(&$form, &$form_state) {
  277. drupal_set_message('system_form_form_test_alter_form_alter() executed.');
  278. }
  279. /**
  280. * Form builder for testing drupal_validate_form().
  281. *
  282. * Serves for testing form processing and alterations by form validation
  283. * handlers, especially for the case of a validation error:
  284. * - form_set_value() should be able to alter submitted values in
  285. * $form_state['values'] without affecting the form element.
  286. * - #element_validate handlers should be able to alter the $element in the form
  287. * structure and the alterations should be contained in the rebuilt form.
  288. * - #validate handlers should be able to alter the $form and the alterations
  289. * should be contained in the rebuilt form.
  290. */
  291. function form_test_validate_form($form, &$form_state) {
  292. $form['name'] = array(
  293. '#type' => 'textfield',
  294. '#title' => 'Name',
  295. '#default_value' => '',
  296. '#element_validate' => array('form_test_element_validate_name'),
  297. );
  298. $form['submit'] = array(
  299. '#type' => 'submit',
  300. '#value' => 'Save',
  301. );
  302. // To simplify this test, enable form caching and use form storage to
  303. // remember our alteration.
  304. $form_state['cache'] = TRUE;
  305. return $form;
  306. }
  307. /**
  308. * Form element validation handler for 'name' in form_test_validate_form().
  309. */
  310. function form_test_element_validate_name(&$element, &$form_state) {
  311. $triggered = FALSE;
  312. if ($form_state['values']['name'] == 'element_validate') {
  313. // Alter the form element.
  314. $element['#value'] = '#value changed by #element_validate';
  315. // Alter the submitted value in $form_state.
  316. form_set_value($element, 'value changed by form_set_value() in #element_validate', $form_state);
  317. $triggered = TRUE;
  318. }
  319. if ($form_state['values']['name'] == 'element_validate_access') {
  320. $form_state['storage']['form_test_name'] = $form_state['values']['name'];
  321. // Alter the form element.
  322. $element['#access'] = FALSE;
  323. $triggered = TRUE;
  324. }
  325. elseif (!empty($form_state['storage']['form_test_name'])) {
  326. // To simplify this test, just take over the element's value into $form_state.
  327. form_set_value($element, $form_state['storage']['form_test_name'], $form_state);
  328. $triggered = TRUE;
  329. }
  330. if ($triggered) {
  331. // Output the element's value from $form_state.
  332. drupal_set_message(t('@label value: @value', array('@label' => $element['#title'], '@value' => $form_state['values']['name'])));
  333. // Trigger a form validation error to see our changes.
  334. form_set_error('');
  335. }
  336. }
  337. /**
  338. * Form validation handler for form_test_validate_form().
  339. */
  340. function form_test_validate_form_validate(&$form, &$form_state) {
  341. if ($form_state['values']['name'] == 'validate') {
  342. // Alter the form element.
  343. $form['name']['#value'] = '#value changed by #validate';
  344. // Alter the submitted value in $form_state.
  345. form_set_value($form['name'], 'value changed by form_set_value() in #validate', $form_state);
  346. // Output the element's value from $form_state.
  347. drupal_set_message(t('@label value: @value', array('@label' => $form['name']['#title'], '@value' => $form_state['values']['name'])));
  348. // Trigger a form validation error to see our changes.
  349. form_set_error('');
  350. }
  351. }
  352. /**
  353. * Form constructor to test the #required property.
  354. */
  355. function form_test_validate_required_form($form, &$form_state) {
  356. $options = drupal_map_assoc(array('foo', 'bar'));
  357. $form['textfield'] = array(
  358. '#type' => 'textfield',
  359. '#title' => 'Textfield',
  360. '#required' => TRUE,
  361. );
  362. $form['checkboxes'] = array(
  363. '#type' => 'checkboxes',
  364. '#title' => 'Checkboxes',
  365. '#options' => $options,
  366. '#required' => TRUE,
  367. );
  368. $form['select'] = array(
  369. '#type' => 'select',
  370. '#title' => 'Select',
  371. '#options' => $options,
  372. '#required' => TRUE,
  373. );
  374. $form['radios'] = array(
  375. '#type' => 'radios',
  376. '#title' => 'Radios',
  377. '#options' => $options,
  378. '#required' => TRUE,
  379. );
  380. $form['radios_optional'] = array(
  381. '#type' => 'radios',
  382. '#title' => 'Radios (optional)',
  383. '#options' => $options,
  384. );
  385. $form['radios_optional_default_value_false'] = array(
  386. '#type' => 'radios',
  387. '#title' => 'Radios (optional, with a default value of FALSE)',
  388. '#options' => $options,
  389. '#default_value' => FALSE,
  390. );
  391. $form['actions'] = array('#type' => 'actions');
  392. $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  393. return $form;
  394. }
  395. /**
  396. * Form submission handler for form_test_validate_required_form().
  397. */
  398. function form_test_validate_required_form_submit($form, &$form_state) {
  399. drupal_set_message('The form_test_validate_required_form form was submitted successfully.');
  400. }
  401. /**
  402. * Form constructor to test the #required property without #title.
  403. */
  404. function form_test_validate_required_form_no_title($form, &$form_state) {
  405. $form['textfield'] = array(
  406. '#type' => 'textfield',
  407. '#required' => TRUE,
  408. );
  409. $form['actions'] = array('#type' => 'actions');
  410. $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  411. return $form;
  412. }
  413. /**
  414. * Form submission handler for form_test_validate_required_form_no_title().
  415. */
  416. function form_test_validate_required_form_no_title_submit($form, &$form_state) {
  417. drupal_set_message('The form_test_validate_required_form_no_title form was submitted successfully.');
  418. }
  419. /**
  420. * Form builder for testing submission of a form without a CSRF token.
  421. */
  422. function form_test_validate_no_token($form, &$form_state) {
  423. $form['submit'] = array(
  424. '#type' => 'submit',
  425. '#value' => 'Save',
  426. );
  427. $form['#token'] = FALSE;
  428. return $form;
  429. }
  430. /**
  431. * Form submission handler for form_test_validate_no_token().
  432. */
  433. function form_test_validate_no_token_submit($form, &$form_state) {
  434. drupal_set_message('The form_test_validate_no_token form has been submitted successfully.');
  435. }
  436. /**
  437. * Builds a simple form with a button triggering partial validation.
  438. */
  439. function form_test_limit_validation_errors_form($form, &$form_state) {
  440. $form['title'] = array(
  441. '#type' => 'textfield',
  442. '#title' => 'Title',
  443. '#required' => TRUE,
  444. );
  445. $form['test'] = array(
  446. '#title' => 'Test',
  447. '#type' => 'textfield',
  448. '#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
  449. );
  450. $form['test_numeric_index'] = array(
  451. '#tree' => TRUE,
  452. );
  453. $form['test_numeric_index'][0] = array(
  454. '#title' => 'Test (numeric index)',
  455. '#type' => 'textfield',
  456. '#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
  457. );
  458. $form['test_substring'] = array(
  459. '#tree' => TRUE,
  460. );
  461. $form['test_substring']['foo'] = array(
  462. '#title' => 'Test (substring) foo',
  463. '#type' => 'textfield',
  464. '#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
  465. );
  466. $form['test_substring']['foobar'] = array(
  467. '#title' => 'Test (substring) foobar',
  468. '#type' => 'textfield',
  469. '#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
  470. );
  471. $form['actions']['partial'] = array(
  472. '#type' => 'submit',
  473. '#limit_validation_errors' => array(array('test')),
  474. '#submit' => array('form_test_limit_validation_errors_form_partial_submit'),
  475. '#value' => t('Partial validate'),
  476. );
  477. $form['actions']['partial_numeric_index'] = array(
  478. '#type' => 'submit',
  479. '#limit_validation_errors' => array(array('test_numeric_index', 0)),
  480. '#submit' => array('form_test_limit_validation_errors_form_partial_submit'),
  481. '#value' => t('Partial validate (numeric index)'),
  482. );
  483. $form['actions']['substring'] = array(
  484. '#type' => 'submit',
  485. '#limit_validation_errors' => array(array('test_substring', 'foo')),
  486. '#submit' => array('form_test_limit_validation_errors_form_partial_submit'),
  487. '#value' => t('Partial validate (substring)'),
  488. );
  489. $form['actions']['full'] = array(
  490. '#type' => 'submit',
  491. '#value' => t('Full validate'),
  492. );
  493. return $form;
  494. }
  495. /**
  496. * Form element validation handler for the 'test' element.
  497. */
  498. function form_test_limit_validation_errors_element_validate_test(&$element, &$form_state) {
  499. if ($element['#value'] == 'invalid') {
  500. form_error($element, t('@label element is invalid', array('@label' => $element['#title'])));
  501. }
  502. }
  503. /**
  504. * Form submit handler for the partial validation submit button.
  505. */
  506. function form_test_limit_validation_errors_form_partial_submit($form, $form_state) {
  507. // The title has not been validated, thus its value - in case of the test case
  508. // an empty string - may not be set.
  509. if (!isset($form_state['values']['title']) && isset($form_state['values']['test'])) {
  510. drupal_set_message('Only validated values appear in the form values.');
  511. }
  512. }
  513. /**
  514. * Create a header and options array. Helper function for callbacks.
  515. */
  516. function _form_test_tableselect_get_data() {
  517. $header = array(
  518. 'one' => t('One'),
  519. 'two' => t('Two'),
  520. 'three' => t('Three'),
  521. 'four' => t('Four'),
  522. );
  523. $options['row1'] = array(
  524. 'one' => 'row1col1',
  525. 'two' => t('row1col2'),
  526. 'three' => t('row1col3'),
  527. 'four' => t('row1col4'),
  528. );
  529. $options['row2'] = array(
  530. 'one' => 'row2col1',
  531. 'two' => t('row2col2'),
  532. 'three' => t('row2col3'),
  533. 'four' => t('row2col4'),
  534. );
  535. $options['row3'] = array(
  536. 'one' => 'row3col1',
  537. 'two' => t('row3col2'),
  538. 'three' => t('row3col3'),
  539. 'four' => t('row3col4'),
  540. );
  541. return array($header, $options);
  542. }
  543. /**
  544. * Build a form to test the tableselect element.
  545. *
  546. * @param $form_state
  547. * The form_state
  548. * @param $element_properties
  549. * An array of element properties for the tableselect element.
  550. *
  551. * @return
  552. * A form with a tableselect element and a submit button.
  553. */
  554. function _form_test_tableselect_form_builder($form, $form_state, $element_properties) {
  555. list($header, $options) = _form_test_tableselect_get_data();
  556. $form['tableselect'] = $element_properties;
  557. $form['tableselect'] += array(
  558. '#prefix' => '<div id="tableselect-wrapper">',
  559. '#suffix' => '</div>',
  560. '#type' => 'tableselect',
  561. '#header' => $header,
  562. '#options' => $options,
  563. '#multiple' => FALSE,
  564. '#empty' => t('Empty text.'),
  565. '#ajax' => array(
  566. 'callback' => '_form_test_tableselect_ajax_callback',
  567. 'wrapper' => 'tableselect-wrapper',
  568. ),
  569. );
  570. $form['submit'] = array(
  571. '#type' => 'submit',
  572. '#value' => t('Submit'),
  573. );
  574. return $form;
  575. }
  576. /**
  577. * Test the tableselect #multiple = TRUE functionality.
  578. */
  579. function _form_test_tableselect_multiple_true_form($form, $form_state) {
  580. return _form_test_tableselect_form_builder($form, $form_state, array('#multiple' => TRUE));
  581. }
  582. /**
  583. * Process the tableselect #multiple = TRUE submitted values.
  584. */
  585. function _form_test_tableselect_multiple_true_form_submit($form, &$form_state) {
  586. $selected = $form_state['values']['tableselect'];
  587. foreach ($selected as $key => $value) {
  588. drupal_set_message(t('Submitted: @key = @value', array('@key' => $key, '@value' => $value)));
  589. }
  590. }
  591. /**
  592. * Test the tableselect #multiple = FALSE functionality.
  593. */
  594. function _form_test_tableselect_multiple_false_form($form, $form_state) {
  595. return _form_test_tableselect_form_builder($form, $form_state, array('#multiple' => FALSE));
  596. }
  597. /**
  598. * Process the tableselect #multiple = FALSE submitted values.
  599. */
  600. function _form_test_tableselect_multiple_false_form_submit($form, &$form_state) {
  601. drupal_set_message(t('Submitted: @value', array('@value' => $form_state['values']['tableselect'])));
  602. }
  603. /**
  604. * Test functionality of the tableselect #empty property.
  605. */
  606. function _form_test_tableselect_empty_form($form, $form_state) {
  607. return _form_test_tableselect_form_builder($form, $form_state, array('#options' => array()));
  608. }
  609. /**
  610. * Test functionality of the tableselect #js_select property.
  611. */
  612. function _form_test_tableselect_js_select_form($form, $form_state, $action) {
  613. switch ($action) {
  614. case 'multiple-true-default':
  615. $options = array('#multiple' => TRUE);
  616. break;
  617. case 'multiple-false-default':
  618. $options = array('#multiple' => FALSE);
  619. break;
  620. case 'multiple-true-no-advanced-select':
  621. $options = array('#multiple' => TRUE, '#js_select' => FALSE);
  622. break;
  623. case 'multiple-false-advanced-select':
  624. $options = array('#multiple' => FALSE, '#js_select' => TRUE);
  625. break;
  626. }
  627. return _form_test_tableselect_form_builder($form, $form_state, $options);
  628. }
  629. /**
  630. * Tests functionality of vertical tabs.
  631. */
  632. function _form_test_vertical_tabs_form($form, &$form_state) {
  633. $form['vertical_tabs'] = array(
  634. '#type' => 'vertical_tabs',
  635. );
  636. $form['tab1'] = array(
  637. '#type' => 'fieldset',
  638. '#title' => t('Tab 1'),
  639. '#collapsible' => TRUE,
  640. '#group' => 'vertical_tabs',
  641. );
  642. $form['tab1']['field1'] = array(
  643. '#title' => t('Field 1'),
  644. '#type' => 'textfield',
  645. );
  646. $form['tab2'] = array(
  647. '#type' => 'fieldset',
  648. '#title' => t('Tab 2'),
  649. '#collapsible' => TRUE,
  650. '#group' => 'vertical_tabs',
  651. );
  652. $form['tab2']['field2'] = array(
  653. '#title' => t('Field 2'),
  654. '#type' => 'textfield',
  655. );
  656. return $form;
  657. }
  658. /**
  659. * Ajax callback that returns the form element.
  660. */
  661. function _form_test_tableselect_ajax_callback($form, &$form_state) {
  662. return $form['tableselect'];
  663. }
  664. /**
  665. * A multistep form for testing the form storage.
  666. *
  667. * It uses two steps for editing a virtual "thing". Any changes to it are saved
  668. * in the form storage and have to be present during any step. By setting the
  669. * request parameter "cache" the form can be tested with caching enabled, as
  670. * it would be the case, if the form would contain some #ajax callbacks.
  671. *
  672. * @see form_test_storage_form_submit()
  673. */
  674. function form_test_storage_form($form, &$form_state) {
  675. if ($form_state['rebuild']) {
  676. $form_state['input'] = array();
  677. }
  678. // Initialize
  679. if (empty($form_state['storage'])) {
  680. if (empty($form_state['input'])) {
  681. $_SESSION['constructions'] = 0;
  682. }
  683. // Put the initial thing into the storage
  684. $form_state['storage'] = array(
  685. 'thing' => array(
  686. 'title' => 'none',
  687. 'value' => '',
  688. ),
  689. );
  690. }
  691. // Count how often the form is constructed.
  692. $_SESSION['constructions']++;
  693. drupal_set_message("Form constructions: " . $_SESSION['constructions']);
  694. $form['title'] = array(
  695. '#type' => 'textfield',
  696. '#title' => 'Title',
  697. '#default_value' => $form_state['storage']['thing']['title'],
  698. '#required' => TRUE,
  699. );
  700. $form['value'] = array(
  701. '#type' => 'textfield',
  702. '#title' => 'Value',
  703. '#default_value' => $form_state['storage']['thing']['value'],
  704. '#element_validate' => array('form_test_storage_element_validate_value_cached'),
  705. );
  706. $form['continue_button'] = array(
  707. '#type' => 'button',
  708. '#value' => 'Reset',
  709. // Rebuilds the form without keeping the values.
  710. );
  711. $form['continue_submit'] = array(
  712. '#type' => 'submit',
  713. '#value' => 'Continue submit',
  714. '#submit' => array('form_storage_test_form_continue_submit'),
  715. );
  716. $form['submit'] = array(
  717. '#type' => 'submit',
  718. '#value' => 'Save',
  719. );
  720. if (isset($_REQUEST['cache'])) {
  721. // Manually activate caching, so we can test that the storage keeps working
  722. // when it's enabled.
  723. $form_state['cache'] = TRUE;
  724. }
  725. if (isset($_REQUEST['immutable'])) {
  726. $form_state['build_info']['immutable'] = TRUE;
  727. }
  728. return $form;
  729. }
  730. /**
  731. * Emulate legacy AHAH-style ajax callback.
  732. *
  733. * Drupal 6 AHAH callbacks used to operate directly on forms retrieved using
  734. * form_get_cache and stored using form_set_cache after manipulation. This
  735. * callback helps testing whether form_set_cache prevents resaving of immutable
  736. * forms.
  737. */
  738. function form_test_storage_legacy_handler($form_build_id) {
  739. $form_state = array();
  740. $form = form_get_cache($form_build_id, $form_state);
  741. drupal_json_output(array(
  742. 'form' => $form,
  743. 'form_state' => $form_state,
  744. ));
  745. $form['#poisoned'] = TRUE;
  746. $form_state['poisoned'] = TRUE;
  747. form_set_cache($form_build_id, $form, $form_state);
  748. }
  749. /**
  750. * Form element validation handler for 'value' element in form_test_storage_form().
  751. *
  752. * Tests updating of cached form storage during validation.
  753. */
  754. function form_test_storage_element_validate_value_cached($element, &$form_state) {
  755. // If caching is enabled and we receive a certain value, change the storage.
  756. // This presumes that another submitted form value triggers a validation error
  757. // elsewhere in the form. Form API should still update the cached form storage
  758. // though.
  759. if (isset($_REQUEST['cache']) && $form_state['values']['value'] == 'change_title') {
  760. $form_state['storage']['thing']['changed'] = TRUE;
  761. }
  762. }
  763. /**
  764. * Form submit handler to continue multi-step form.
  765. */
  766. function form_storage_test_form_continue_submit($form, &$form_state) {
  767. $form_state['storage']['thing']['title'] = $form_state['values']['title'];
  768. $form_state['storage']['thing']['value'] = $form_state['values']['value'];
  769. $form_state['rebuild'] = TRUE;
  770. }
  771. /**
  772. * Form submit handler to finish multi-step form.
  773. */
  774. function form_test_storage_form_submit($form, &$form_state) {
  775. drupal_set_message("Title: " . check_plain($form_state['values']['title']));
  776. drupal_set_message("Form constructions: " . $_SESSION['constructions']);
  777. if (isset($form_state['storage']['thing']['changed'])) {
  778. drupal_set_message("The thing has been changed.");
  779. }
  780. $form_state['redirect'] = 'node';
  781. }
  782. /**
  783. * A simple form for testing form storage when page caching is enabled.
  784. */
  785. function form_test_storage_page_cache_form($form, &$form_state) {
  786. $form['title'] = array(
  787. '#type' => 'textfield',
  788. '#title' => 'Title',
  789. '#required' => TRUE,
  790. );
  791. $form['test_build_id_old'] = array(
  792. '#type' => 'item',
  793. '#title' => 'Old build id',
  794. '#markup' => 'No old build id',
  795. );
  796. $form['submit'] = array(
  797. '#type' => 'submit',
  798. '#value' => 'Save',
  799. );
  800. $form['rebuild'] = array(
  801. '#type' => 'submit',
  802. '#value' => 'Rebuild',
  803. '#submit' => array('form_test_storage_page_cache_rebuild'),
  804. );
  805. $form['#after_build'] = array('form_test_storage_page_cache_old_build_id');
  806. $form_state['cache'] = TRUE;
  807. return $form;
  808. }
  809. /**
  810. * Form element #after_build callback: output the old form build-id.
  811. */
  812. function form_test_storage_page_cache_old_build_id($form) {
  813. if (isset($form['#build_id_old'])) {
  814. $form['test_build_id_old']['#markup'] = check_plain($form['#build_id_old']);
  815. }
  816. return $form;
  817. }
  818. /**
  819. * Form submit callback: Rebuild the form and continue.
  820. */
  821. function form_test_storage_page_cache_rebuild($form, &$form_state) {
  822. $form_state['rebuild'] = TRUE;
  823. }
  824. /**
  825. * A form for testing form labels and required marks.
  826. */
  827. function form_label_test_form() {
  828. $form['form_checkboxes_test'] = array(
  829. '#type' => 'checkboxes',
  830. '#title' => t('Checkboxes test'),
  831. '#options' => array(
  832. 'first-checkbox' => t('First checkbox'),
  833. 'second-checkbox' => t('Second checkbox'),
  834. 'third-checkbox' => t('Third checkbox'),
  835. '0' => t('0'),
  836. ),
  837. );
  838. $form['form_radios_test'] = array(
  839. '#type' => 'radios',
  840. '#title' => t('Radios test'),
  841. '#options' => array(
  842. 'first-radio' => t('First radio'),
  843. 'second-radio' => t('Second radio'),
  844. 'third-radio' => t('Third radio'),
  845. '0' => t('0'),
  846. ),
  847. // Test #field_prefix and #field_suffix placement.
  848. '#field_prefix' => '<span id="form-test-radios-field-prefix">' . t('Radios #field_prefix element') . '</span>',
  849. '#field_suffix' => '<span id="form-test-radios-field-suffix">' . t('Radios #field_suffix element') . '</span>',
  850. );
  851. $form['form_checkbox_test'] = array(
  852. '#type' => 'checkbox',
  853. '#title' => t('Checkbox test'),
  854. );
  855. $form['form_textfield_test_title_and_required'] = array(
  856. '#type' => 'textfield',
  857. '#title' => t('Textfield test for required with title'),
  858. '#required' => TRUE,
  859. );
  860. $form['form_textfield_test_no_title_required'] = array(
  861. '#type' => 'textfield',
  862. // We use an empty title, since not setting #title suppresses the label
  863. // and required marker.
  864. '#title' => '',
  865. '#required' => TRUE,
  866. );
  867. $form['form_textfield_test_title'] = array(
  868. '#type' => 'textfield',
  869. '#title' => t('Textfield test for title only'),
  870. // Not required.
  871. // Test #prefix and #suffix placement.
  872. '#prefix' => '<div id="form-test-textfield-title-prefix">' . t('Textfield #prefix element') . '</div>',
  873. '#suffix' => '<div id="form-test-textfield-title-suffix">' . t('Textfield #suffix element') . '</div>',
  874. );
  875. $form['form_textfield_test_title_after'] = array(
  876. '#type' => 'textfield',
  877. '#title' => t('Textfield test for title after element'),
  878. '#title_display' => 'after',
  879. );
  880. $form['form_textfield_test_title_invisible'] = array(
  881. '#type' => 'textfield',
  882. '#title' => t('Textfield test for invisible title'),
  883. '#title_display' => 'invisible',
  884. );
  885. // Textfield test for title set not to display.
  886. $form['form_textfield_test_title_no_show'] = array(
  887. '#type' => 'textfield',
  888. );
  889. // Checkboxes & radios with title as attribute.
  890. $form['form_checkboxes_title_attribute'] = array(
  891. '#type' => 'checkboxes',
  892. '#title' => 'Checkboxes test',
  893. '#options' => array(
  894. 'first-checkbox' => 'First checkbox',
  895. 'second-checkbox' => 'Second checkbox',
  896. ),
  897. '#title_display' => 'attribute',
  898. '#required' => TRUE,
  899. );
  900. $form['form_radios_title_attribute'] = array(
  901. '#type' => 'radios',
  902. '#title' => 'Radios test',
  903. '#options' => array(
  904. 'first-radio' => 'First radio',
  905. 'second-radio' => 'Second radio',
  906. ),
  907. '#title_display' => 'attribute',
  908. '#required' => TRUE,
  909. );
  910. return $form;
  911. }
  912. /**
  913. * Menu callback; Invokes a form builder function with a wrapper callback.
  914. */
  915. function form_test_wrapper_callback($form_id) {
  916. $form_state = array(
  917. 'build_info' => array('args' => array()),
  918. 'wrapper_callback' => 'form_test_wrapper_callback_wrapper',
  919. );
  920. return drupal_build_form($form_id, $form_state);
  921. }
  922. /**
  923. * Form wrapper for form_test_wrapper_callback_form().
  924. */
  925. function form_test_wrapper_callback_wrapper($form, &$form_state) {
  926. $form['wrapper'] = array('#markup' => 'Form wrapper callback element output.');
  927. return $form;
  928. }
  929. /**
  930. * Form builder for form wrapper callback test.
  931. */
  932. function form_test_wrapper_callback_form($form, &$form_state) {
  933. $form['builder'] = array('#markup' => 'Form builder element output.');
  934. return $form;
  935. }
  936. /**
  937. * Form builder for form_state_values_clean() test.
  938. */
  939. function form_test_form_state_values_clean_form($form, &$form_state) {
  940. // Build an example form containing multiple submit and button elements; not
  941. // only on the top-level.
  942. $form = array('#tree' => TRUE);
  943. $form['foo'] = array('#type' => 'submit', '#value' => t('Submit'));
  944. $form['bar'] = array('#type' => 'submit', '#value' => t('Submit'));
  945. $form['beer'] = array('#type' => 'value', '#value' => 1000);
  946. $form['baz']['foo'] = array('#type' => 'button', '#value' => t('Submit'));
  947. $form['baz']['baz'] = array('#type' => 'submit', '#value' => t('Submit'));
  948. $form['baz']['beer'] = array('#type' => 'value', '#value' => 2000);
  949. return $form;
  950. }
  951. /**
  952. * Form submit handler for form_state_values_clean() test form.
  953. */
  954. function form_test_form_state_values_clean_form_submit($form, &$form_state) {
  955. form_state_values_clean($form_state);
  956. drupal_json_output($form_state['values']);
  957. exit;
  958. }
  959. /**
  960. * Form constructor for the form_state_values_clean() test.
  961. */
  962. function form_test_form_state_values_clean_advanced_form($form, &$form_state) {
  963. // Build an example form containing a managed file and a submit form element.
  964. $form['image'] = array(
  965. '#type' => 'managed_file',
  966. '#title' => t('Image'),
  967. '#upload_location' => 'public://',
  968. '#default_value' => 0,
  969. );
  970. $form['submit'] = array(
  971. '#type' => 'submit',
  972. '#value' => t('Submit'),
  973. );
  974. return $form;
  975. }
  976. /**
  977. * Form submission handler for form_test_form_state_values_clean_advanced_form().
  978. */
  979. function form_test_form_state_values_clean_advanced_form_submit($form, &$form_state) {
  980. form_state_values_clean($form_state);
  981. print t('You WIN!');
  982. exit;
  983. }
  984. /**
  985. * Build a form to test a checkbox.
  986. */
  987. function _form_test_checkbox($form, &$form_state) {
  988. // A required checkbox.
  989. $form['required_checkbox'] = array(
  990. '#type' => 'checkbox',
  991. '#required' => TRUE,
  992. '#title' => 'required_checkbox',
  993. );
  994. // A disabled checkbox should get its default value back.
  995. $form['disabled_checkbox_on'] = array(
  996. '#type' => 'checkbox',
  997. '#disabled' => TRUE,
  998. '#return_value' => 'disabled_checkbox_on',
  999. '#default_value' => 'disabled_checkbox_on',
  1000. '#title' => 'disabled_checkbox_on',
  1001. );
  1002. $form['disabled_checkbox_off'] = array(
  1003. '#type' => 'checkbox',
  1004. '#disabled' => TRUE,
  1005. '#return_value' => 'disabled_checkbox_off',
  1006. '#default_value' => NULL,
  1007. '#title' => 'disabled_checkbox_off',
  1008. );
  1009. // A checkbox is active when #default_value == #return_value.
  1010. $form['checkbox_on'] = array(
  1011. '#type' => 'checkbox',
  1012. '#return_value' => 'checkbox_on',
  1013. '#default_value' => 'checkbox_on',
  1014. '#title' => 'checkbox_on',
  1015. );
  1016. // But inactive in any other case.
  1017. $form['checkbox_off'] = array(
  1018. '#type' => 'checkbox',
  1019. '#return_value' => 'checkbox_off',
  1020. '#default_value' => 'checkbox_on',
  1021. '#title' => 'checkbox_off',
  1022. );
  1023. // Checkboxes with a #return_value of '0' are supported.
  1024. $form['zero_checkbox_on'] = array(
  1025. '#type' => 'checkbox',
  1026. '#return_value' => '0',
  1027. '#default_value' => '0',
  1028. '#title' => 'zero_checkbox_on',
  1029. );
  1030. // In that case, passing a #default_value != '0' means that the checkbox is off.
  1031. $form['zero_checkbox_off'] = array(
  1032. '#type' => 'checkbox',
  1033. '#return_value' => '0',
  1034. '#default_value' => '1',
  1035. '#title' => 'zero_checkbox_off',
  1036. );
  1037. $form['submit'] = array(
  1038. '#type' => 'submit',
  1039. '#value' => t('Submit')
  1040. );
  1041. return $form;
  1042. }
  1043. /**
  1044. * Return the form values via JSON.
  1045. */
  1046. function _form_test_checkbox_submit($form, &$form_state) {
  1047. drupal_json_output($form_state['values']);
  1048. exit();
  1049. }
  1050. /**
  1051. * Builds a form to test #type 'select' validation.
  1052. */
  1053. function form_test_select($form, &$form_state) {
  1054. $base = array(
  1055. '#type' => 'select',
  1056. '#options' => drupal_map_assoc(array('one', 'two', 'three')),
  1057. );
  1058. $form['select'] = $base + array(
  1059. '#title' => '#default_value one',
  1060. '#default_value' => 'one',
  1061. );
  1062. $form['select_required'] = $base + array(
  1063. '#title' => '#default_value one, #required',
  1064. '#required' => TRUE,
  1065. '#default_value' => 'one',
  1066. );
  1067. $form['select_optional'] = $base + array(
  1068. '#title' => '#default_value one, not #required',
  1069. '#required' => FALSE,
  1070. '#default_value' => 'one',
  1071. );
  1072. $form['empty_value'] = $base + array(
  1073. '#title' => '#default_value one, #required, #empty_value 0',
  1074. '#required' => TRUE,
  1075. '#default_value' => 'one',
  1076. '#empty_value' => 0,
  1077. );
  1078. $form['empty_value_one'] = $base + array(
  1079. '#title' => '#default_value = #empty_value, #required',
  1080. '#required' => TRUE,
  1081. '#default_value' => 'one',
  1082. '#empty_value' => 'one',
  1083. );
  1084. $form['no_default'] = $base + array(
  1085. '#title' => 'No #default_value, #required',
  1086. '#required' => TRUE,
  1087. );
  1088. $form['no_default_optional'] = $base + array(
  1089. '#title' => 'No #default_value, not #required',
  1090. '#required' => FALSE,
  1091. '#description' => 'Should result in "one", because it is not required and there is no #empty_value requested, so default browser behavior of preselecting first option is in effect.',
  1092. );
  1093. $form['no_default_optional_empty_value'] = $base + array(
  1094. '#title' => 'No #default_value, not #required, #empty_value empty string',
  1095. '#empty_value' => '',
  1096. '#required' => FALSE,
  1097. '#description' => 'Should result in an empty string (due to #empty_value), because it is optional.',
  1098. );
  1099. $form['no_default_empty_option'] = $base + array(
  1100. '#title' => 'No #default_value, #required, #empty_option',
  1101. '#required' => TRUE,
  1102. '#empty_option' => '- Choose -',
  1103. );
  1104. $form['no_default_empty_option_optional'] = $base + array(
  1105. '#title' => 'No #default_value, not #required, #empty_option',
  1106. '#empty_option' => '- Dismiss -',
  1107. '#description' => 'Should result in an empty string (default of #empty_value), because it is optional.',
  1108. );
  1109. $form['no_default_empty_value'] = $base + array(
  1110. '#title' => 'No #default_value, #required, #empty_value 0',
  1111. '#required' => TRUE,
  1112. '#empty_value' => 0,
  1113. '#description' => 'Should never result in 0.',
  1114. );
  1115. $form['no_default_empty_value_one'] = $base + array(
  1116. '#title' => 'No #default_value, #required, #empty_value one',
  1117. '#required' => TRUE,
  1118. '#empty_value' => 'one',
  1119. '#description' => 'A mistakenly assigned #empty_value contained in #options should not be valid.',
  1120. );
  1121. $form['no_default_empty_value_optional'] = $base + array(
  1122. '#title' => 'No #default_value, not #required, #empty_value 0',
  1123. '#required' => FALSE,
  1124. '#empty_value' => 0,
  1125. '#description' => 'Should result in 0, because it is optional.',
  1126. );
  1127. $form['multiple'] = $base + array(
  1128. '#title' => '#multiple, #default_value two',
  1129. '#default_value' => array('two'),
  1130. '#multiple' => TRUE,
  1131. );
  1132. $form['multiple_no_default'] = $base + array(
  1133. '#title' => '#multiple, no #default_value',
  1134. '#multiple' => TRUE,
  1135. );
  1136. $form['multiple_no_default_required'] = $base + array(
  1137. '#title' => '#multiple, #required, no #default_value',
  1138. '#required' => TRUE,
  1139. '#multiple' => TRUE,
  1140. );
  1141. $form['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  1142. return $form;
  1143. }
  1144. /**
  1145. * Form submit handler for form_test_select().
  1146. */
  1147. function form_test_select_submit($form, &$form_state) {
  1148. drupal_json_output($form_state['values']);
  1149. exit();
  1150. }
  1151. /**
  1152. * Form constructor to test expansion of #type checkboxes and radios.
  1153. */
  1154. function form_test_checkboxes_radios($form, &$form_state, $customize = FALSE) {
  1155. $form['#submit'] = array('_form_test_submit_values_json');
  1156. // Expand #type checkboxes, setting custom element properties for some but not
  1157. // all options.
  1158. $form['checkboxes'] = array(
  1159. '#type' => 'checkboxes',
  1160. '#title' => 'Checkboxes',
  1161. '#options' => array(
  1162. 0 => 'Zero',
  1163. 'foo' => 'Foo',
  1164. 1 => 'One',
  1165. 'bar' => 'Bar',
  1166. '>' => 'Special Char',
  1167. ),
  1168. );
  1169. if ($customize) {
  1170. $form['checkboxes'] += array(
  1171. 'foo' => array(
  1172. '#description' => 'Enable to foo.',
  1173. ),
  1174. 1 => array(
  1175. '#weight' => 10,
  1176. ),
  1177. );
  1178. }
  1179. // Expand #type radios, setting custom element properties for some but not
  1180. // all options.
  1181. $form['radios'] = array(
  1182. '#type' => 'radios',
  1183. '#title' => 'Radios',
  1184. '#options' => array(
  1185. 0 => 'Zero',
  1186. 'foo' => 'Foo',
  1187. 1 => 'One',
  1188. 'bar' => 'Bar',
  1189. '>' => 'Special Char',
  1190. ),
  1191. );
  1192. if ($customize) {
  1193. $form['radios'] += array(
  1194. 'foo' => array(
  1195. '#description' => 'Enable to foo.',
  1196. ),
  1197. 1 => array(
  1198. '#weight' => 10,
  1199. ),
  1200. );
  1201. }
  1202. $form['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  1203. return $form;
  1204. }
  1205. /**
  1206. * Build a form to test disabled elements.
  1207. */
  1208. function _form_test_disabled_elements($form, &$form_state) {
  1209. // Elements that take a simple default value.
  1210. foreach (array('textfield', 'textarea', 'hidden') as $type) {
  1211. $form[$type] = array(
  1212. '#type' => $type,
  1213. '#title' => $type,
  1214. '#default_value' => $type,
  1215. '#test_hijack_value' => 'HIJACK',
  1216. '#disabled' => TRUE,
  1217. );
  1218. }
  1219. // Multiple values option elements.
  1220. foreach (array('checkboxes', 'select') as $type) {
  1221. $form[$type . '_multiple'] = array(
  1222. '#type' => $type,
  1223. '#title' => $type . ' (multiple)',
  1224. '#options' => array(
  1225. 'test_1' => 'Test 1',
  1226. 'test_2' => 'Test 2',
  1227. ),
  1228. '#multiple' => TRUE,
  1229. '#default_value' => array('test_2' => 'test_2'),
  1230. // The keys of #test_hijack_value need to match the #name of the control.
  1231. // @see FormsTestCase::testDisabledElements()
  1232. '#test_hijack_value' => $type == 'select' ? array('' => 'test_1') : array('test_1' => 'test_1'),
  1233. '#disabled' => TRUE,
  1234. );
  1235. }
  1236. // Single values option elements.
  1237. foreach (array('radios', 'select') as $type) {
  1238. $form[$type . '_single'] = array(
  1239. '#type' => $type,
  1240. '#title' => $type . ' (single)',
  1241. '#options' => array(
  1242. 'test_1' => 'Test 1',
  1243. 'test_2' => 'Test 2',
  1244. ),
  1245. '#multiple' => FALSE,
  1246. '#default_value' => 'test_2',
  1247. '#test_hijack_value' => 'test_1',
  1248. '#disabled' => TRUE,
  1249. );
  1250. }
  1251. // Checkbox and radio.
  1252. foreach (array('checkbox', 'radio') as $type) {
  1253. $form[$type . '_unchecked'] = array(
  1254. '#type' => $type,
  1255. '#title' => $type . ' (unchecked)',
  1256. '#return_value' => 1,
  1257. '#default_value' => 0,
  1258. '#test_hijack_value' => 1,
  1259. '#disabled' => TRUE,
  1260. );
  1261. $form[$type . '_checked'] = array(
  1262. '#type' => $type,
  1263. '#title' => $type . ' (checked)',
  1264. '#return_value' => 1,
  1265. '#default_value' => 1,
  1266. '#test_hijack_value' => NULL,
  1267. '#disabled' => TRUE,
  1268. );
  1269. }
  1270. // Weight.
  1271. $form['weight'] = array(
  1272. '#type' => 'weight',
  1273. '#title' => 'weight',
  1274. '#default_value' => 10,
  1275. '#test_hijack_value' => 5,
  1276. '#disabled' => TRUE,
  1277. );
  1278. // Date.
  1279. $form['date'] = array(
  1280. '#type' => 'date',
  1281. '#title' => 'date',
  1282. '#disabled' => TRUE,
  1283. '#default_value' => array(
  1284. 'day' => 19,
  1285. 'month' => 11,
  1286. 'year' => 1978,
  1287. ),
  1288. '#test_hijack_value' => array(
  1289. 'day' => 20,
  1290. 'month' => 12,
  1291. 'year' => 1979,
  1292. ),
  1293. );
  1294. // The #disabled state should propagate to children.
  1295. $form['disabled_container'] = array(
  1296. '#disabled' => TRUE,
  1297. );
  1298. foreach (array('textfield', 'textarea', 'hidden') as $type) {
  1299. $form['disabled_container']['disabled_container_' . $type] = array(
  1300. '#type' => $type,
  1301. '#title' => $type,
  1302. '#default_value' => $type,
  1303. '#test_hijack_value' => 'HIJACK',
  1304. );
  1305. }
  1306. // Text format.
  1307. $form['text_format'] = array(
  1308. '#type' => 'text_format',
  1309. '#title' => 'Text format',
  1310. '#disabled' => TRUE,
  1311. '#default_value' => 'Text value',
  1312. '#format' => 'plain_text',
  1313. '#expected_value' => array(
  1314. 'value' => 'Text value',
  1315. 'format' => 'plain_text',
  1316. ),
  1317. '#test_hijack_value' => array(
  1318. 'value' => 'HIJACK',
  1319. 'format' => 'filtered_html',
  1320. ),
  1321. );
  1322. // Password fields.
  1323. $form['password'] = array(
  1324. '#type' => 'password',
  1325. '#title' => 'Password',
  1326. '#disabled' => TRUE,
  1327. );
  1328. $form['password_confirm'] = array(
  1329. '#type' => 'password_confirm',
  1330. '#title' => 'Password confirm',
  1331. '#disabled' => TRUE,
  1332. );
  1333. // Files.
  1334. $form['file'] = array(
  1335. '#type' => 'file',
  1336. '#title' => 'File',
  1337. '#disabled' => TRUE,
  1338. );
  1339. $form['managed_file'] = array(
  1340. '#type' => 'managed_file',
  1341. '#title' => 'Managed file',
  1342. '#disabled' => TRUE,
  1343. );
  1344. // Buttons.
  1345. $form['image_button'] = array(
  1346. '#type' => 'image_button',
  1347. '#value' => 'Image button',
  1348. '#disabled' => TRUE,
  1349. );
  1350. $form['button'] = array(
  1351. '#type' => 'button',
  1352. '#value' => 'Button',
  1353. '#disabled' => TRUE,
  1354. );
  1355. $form['submit_disabled'] = array(
  1356. '#type' => 'submit',
  1357. '#value' => 'Submit',
  1358. '#disabled' => TRUE,
  1359. );
  1360. $form['submit'] = array(
  1361. '#type' => 'submit',
  1362. '#value' => t('Submit'),
  1363. );
  1364. return $form;
  1365. }
  1366. /**
  1367. * Return the form values via JSON.
  1368. */
  1369. function _form_test_disabled_elements_submit($form, &$form_state) {
  1370. drupal_json_output($form_state['values']);
  1371. exit();
  1372. }
  1373. /**
  1374. * Build a form to test input forgery of enabled elements.
  1375. */
  1376. function _form_test_input_forgery($form, &$form_state) {
  1377. // For testing that a user can't submit a value not matching one of the
  1378. // allowed options.
  1379. $form['checkboxes'] = array(
  1380. '#type' => 'checkboxes',
  1381. '#options' => array(
  1382. 'one' => 'One',
  1383. 'two' => 'Two',
  1384. ),
  1385. );
  1386. $form['submit'] = array(
  1387. '#type' => 'submit',
  1388. '#value' => t('Submit'),
  1389. );
  1390. return $form;
  1391. }
  1392. /**
  1393. * Return the form values via JSON.
  1394. */
  1395. function _form_test_input_forgery_submit($form, &$form_state) {
  1396. drupal_json_output($form_state['values']);
  1397. exit();
  1398. }
  1399. /**
  1400. * Form builder for testing preservation of values during a rebuild.
  1401. */
  1402. function form_test_form_rebuild_preserve_values_form($form, &$form_state) {
  1403. // Start the form with two checkboxes, to test different defaults, and a
  1404. // textfield, to test more than one element type.
  1405. $form = array(
  1406. 'checkbox_1_default_off' => array(
  1407. '#type' => 'checkbox',
  1408. '#title' => t('This checkbox defaults to unchecked.'),
  1409. '#default_value' => FALSE,
  1410. ),
  1411. 'checkbox_1_default_on' => array(
  1412. '#type' => 'checkbox',
  1413. '#title' => t('This checkbox defaults to checked.'),
  1414. '#default_value' => TRUE,
  1415. ),
  1416. 'text_1' => array(
  1417. '#type' => 'textfield',
  1418. '#title' => t('This textfield has a non-empty default value.'),
  1419. '#default_value' => 'DEFAULT 1',
  1420. ),
  1421. );
  1422. // Provide an 'add more' button that rebuilds the form with an additional two
  1423. // checkboxes and a textfield. The test is to make sure that the rebuild
  1424. // triggered by this button preserves the user input values for the initial
  1425. // elements and initializes the new elements with the correct default values.
  1426. if (empty($form_state['storage']['add_more'])) {
  1427. $form['add_more'] = array(
  1428. '#type' => 'submit',
  1429. '#value' => 'Add more',
  1430. '#submit' => array('form_test_form_rebuild_preserve_values_form_add_more'),
  1431. );
  1432. }
  1433. else {
  1434. $form += array(
  1435. 'checkbox_2_default_off' => array(
  1436. '#type' => 'checkbox',
  1437. '#title' => t('This checkbox defaults to unchecked.'),
  1438. '#default_value' => FALSE,
  1439. ),
  1440. 'checkbox_2_default_on' => array(
  1441. '#type' => 'checkbox',
  1442. '#title' => t('This checkbox defaults to checked.'),
  1443. '#default_value' => TRUE,
  1444. ),
  1445. 'text_2' => array(
  1446. '#type' => 'textfield',
  1447. '#title' => t('This textfield has a non-empty default value.'),
  1448. '#default_value' => 'DEFAULT 2',
  1449. ),
  1450. );
  1451. }
  1452. // A submit button that finishes the form workflow (does not rebuild).
  1453. $form['submit'] = array(
  1454. '#type' => 'submit',
  1455. '#value' => 'Submit',
  1456. );
  1457. return $form;
  1458. }
  1459. /**
  1460. * Button submit handler for form_test_form_rebuild_preserve_values_form().
  1461. */
  1462. function form_test_form_rebuild_preserve_values_form_add_more($form, &$form_state) {
  1463. // Rebuild, to test preservation of input values.
  1464. $form_state['storage']['add_more'] = TRUE;
  1465. $form_state['rebuild'] = TRUE;
  1466. }
  1467. /**
  1468. * Form submit handler for form_test_form_rebuild_preserve_values_form().
  1469. */
  1470. function form_test_form_rebuild_preserve_values_form_submit($form, &$form_state) {
  1471. // Finish the workflow. Do not rebuild.
  1472. drupal_set_message(t('Form values: %values', array('%values' => var_export($form_state['values'], TRUE))));
  1473. }
  1474. /**
  1475. * Form constructor for testing form state persistence.
  1476. */
  1477. function form_test_state_persist($form, &$form_state) {
  1478. $form['title'] = array(
  1479. '#type' => 'textfield',
  1480. '#title' => 'title',
  1481. '#default_value' => 'DEFAULT',
  1482. '#required' => TRUE,
  1483. );
  1484. $form_state['value'] = 'State persisted.';
  1485. $form['submit'] = array(
  1486. '#type' => 'submit',
  1487. '#value' => t('Submit'),
  1488. );
  1489. return $form;
  1490. }
  1491. /**
  1492. * Submit handler.
  1493. *
  1494. * @see form_test_state_persist()
  1495. */
  1496. function form_test_state_persist_submit($form, &$form_state) {
  1497. drupal_set_message($form_state['value']);
  1498. $form_state['rebuild'] = TRUE;
  1499. }
  1500. /**
  1501. * Implements hook_form_FORM_ID_alter().
  1502. *
  1503. * @see form_test_state_persist()
  1504. */
  1505. function form_test_form_form_test_state_persist_alter(&$form, &$form_state) {
  1506. // Simulate a form alter implementation inserting form elements that enable
  1507. // caching of the form, e.g. elements having #ajax.
  1508. if (!empty($_REQUEST['cache'])) {
  1509. $form_state['cache'] = TRUE;
  1510. }
  1511. }
  1512. /**
  1513. * Form builder to test programmatic form submissions.
  1514. */
  1515. function form_test_programmatic_form($form, &$form_state) {
  1516. $form['textfield'] = array(
  1517. '#title' => 'Textfield',
  1518. '#type' => 'textfield',
  1519. );
  1520. $form['checkboxes'] = array(
  1521. '#type' => 'checkboxes',
  1522. '#options' => array(
  1523. 1 => 'First checkbox',
  1524. 2 => 'Second checkbox',
  1525. ),
  1526. // Both checkboxes are selected by default so that we can test the ability
  1527. // of programmatic form submissions to uncheck them.
  1528. '#default_value' => array(1, 2),
  1529. );
  1530. // This is used to test that programmatic form submissions can bypass #access
  1531. // restrictions.
  1532. $form['textfield_no_access'] = array(
  1533. '#type' => 'textfield',
  1534. '#title' => 'Textfield no access',
  1535. '#default_value' => 'default value',
  1536. '#access' => FALSE,
  1537. );
  1538. $form['field_to_validate'] = array(
  1539. '#type' => 'radios',
  1540. '#title' => 'Field to validate (in the case of limited validation)',
  1541. '#description' => 'If the form is submitted by clicking the "Submit with limited validation" button, then validation can be limited based on the value of this radio button.',
  1542. '#options' => array(
  1543. 'all' => 'Validate all fields',
  1544. 'textfield' => 'Validate the "Textfield" field',
  1545. 'field_to_validate' => 'Validate the "Field to validate" field',
  1546. ),
  1547. '#default_value' => 'all',
  1548. );
  1549. // The main submit button for the form.
  1550. $form['submit'] = array(
  1551. '#type' => 'submit',
  1552. '#value' => 'Submit',
  1553. );
  1554. // A secondary submit button that allows validation to be limited based on
  1555. // the value of the above radio selector.
  1556. $form['submit_limit_validation'] = array(
  1557. '#type' => 'submit',
  1558. '#value' => 'Submit with limited validation',
  1559. // Use the same submit handler for this button as for the form itself.
  1560. // (This must be set explicitly or otherwise the form API will ignore the
  1561. // #limit_validation_errors property.)
  1562. '#submit' => array('form_test_programmatic_form_submit'),
  1563. );
  1564. if (!empty($form_state['input']['field_to_validate']) && $form_state['input']['field_to_validate'] != 'all') {
  1565. $form['submit_limit_validation']['#limit_validation_errors'] = array(
  1566. array($form_state['input']['field_to_validate']),
  1567. );
  1568. }
  1569. return $form;
  1570. }
  1571. /**
  1572. * Form validation handler for programmatic form submissions.
  1573. *
  1574. * To test that the validation handler is correctly executed, the field value is
  1575. * explicitly required here.
  1576. */
  1577. function form_test_programmatic_form_validate($form, &$form_state) {
  1578. if (empty($form_state['values']['textfield'])) {
  1579. form_set_error('textfield', t('Textfield is required.'));
  1580. }
  1581. }
  1582. /**
  1583. * Form submit handler for programmatic form submissions.
  1584. *
  1585. * To test that the submission handler is correctly executed, we store the
  1586. * submitted values in a place we can access from the caller context.
  1587. */
  1588. function form_test_programmatic_form_submit($form, &$form_state) {
  1589. $form_state['storage']['programmatic_form_submit'] = $form_state['values'];
  1590. }
  1591. /**
  1592. * Form builder to test button click detection.
  1593. */
  1594. function form_test_clicked_button($form, &$form_state) {
  1595. // A single text field. In IE, when a form has only one non-button input field
  1596. // and the ENTER key is pressed while that field has focus, the form is
  1597. // submitted without any information identifying the button responsible for
  1598. // the submission. In other browsers, the form is submitted as though the
  1599. // first button were clicked.
  1600. $form['text'] = array(
  1601. '#title' => 'Text',
  1602. '#type' => 'textfield',
  1603. );
  1604. // Loop through each path argument, addding buttons based on the information
  1605. // in the argument. For example, if the path is
  1606. // form-test/clicked-button/s/i/rb, then 3 buttons are added: a 'submit', an
  1607. // 'image_button', and a 'button' with #access=FALSE. This enables form.test
  1608. // to test a variety of combinations.
  1609. $i=0;
  1610. $args = array_slice(arg(), 2);
  1611. foreach ($args as $arg) {
  1612. $name = 'button' . ++$i;
  1613. // 's', 'b', or 'i' in the argument define the button type wanted.
  1614. if (strpos($arg, 's') !== FALSE) {
  1615. $type = 'submit';
  1616. }
  1617. elseif (strpos($arg, 'b') !== FALSE) {
  1618. $type = 'button';
  1619. }
  1620. elseif (strpos($arg, 'i') !== FALSE) {
  1621. $type = 'image_button';
  1622. }
  1623. else {
  1624. $type = NULL;
  1625. }
  1626. if (isset($type)) {
  1627. $form[$name] = array(
  1628. '#type' => $type,
  1629. '#name' => $name,
  1630. );
  1631. // Image buttons need a #src; the others need a #value.
  1632. if ($type == 'image_button') {
  1633. $form[$name]['#src'] = 'misc/druplicon.png';
  1634. }
  1635. else {
  1636. $form[$name]['#value'] = $name;
  1637. }
  1638. // 'r' for restricted, so we can test that button click detection code
  1639. // correctly takes #access security into account.
  1640. if (strpos($arg, 'r') !== FALSE) {
  1641. $form[$name]['#access'] = FALSE;
  1642. }
  1643. }
  1644. }
  1645. return $form;
  1646. }
  1647. /**
  1648. * Form validation handler for the form_test_clicked_button() form.
  1649. */
  1650. function form_test_clicked_button_validate($form, &$form_state) {
  1651. if (isset($form_state['triggering_element'])) {
  1652. drupal_set_message(t('The clicked button is %name.', array('%name' => $form_state['triggering_element']['#name'])));
  1653. }
  1654. else {
  1655. drupal_set_message('There is no clicked button.');
  1656. }
  1657. }
  1658. /**
  1659. * Form submit handler for the form_test_clicked_button() form.
  1660. */
  1661. function form_test_clicked_button_submit($form, &$form_state) {
  1662. drupal_set_message('Submit handler for form_test_clicked_button executed.');
  1663. }
  1664. /**
  1665. * Form builder to detect form redirect.
  1666. */
  1667. function form_test_redirect($form, &$form_state) {
  1668. $form['redirection'] = array(
  1669. '#type' => 'checkbox',
  1670. '#title' => t('Use redirection'),
  1671. );
  1672. $form['destination'] = array(
  1673. '#type' => 'textfield',
  1674. '#title' => t('Redirect destination'),
  1675. '#states' => array(
  1676. 'visible' => array(
  1677. ':input[name="redirection"]' => array('checked' => TRUE),
  1678. ),
  1679. ),
  1680. );
  1681. $form['submit'] = array(
  1682. '#type' => 'submit',
  1683. '#value' => t('Submit'),
  1684. );
  1685. return $form;
  1686. }
  1687. /**
  1688. * Form submit handler to test different redirect behaviours.
  1689. */
  1690. function form_test_redirect_submit(&$form, &$form_state) {
  1691. if (!empty($form_state['values']['redirection'])) {
  1692. $form_state['redirect'] = !empty($form_state['values']['destination']) ? $form_state['values']['destination'] : NULL;
  1693. }
  1694. else {
  1695. $form_state['redirect'] = FALSE;
  1696. }
  1697. }
  1698. /**
  1699. * Implements hook_form_FORM_ID_alter() for the registration form.
  1700. */
  1701. function form_test_form_user_register_form_alter(&$form, &$form_state) {
  1702. $form['test_rebuild'] = array(
  1703. '#type' => 'submit',
  1704. '#value' => t('Rebuild'),
  1705. '#submit' => array('form_test_user_register_form_rebuild'),
  1706. );
  1707. // If requested, add the test field by attaching the node page form.
  1708. if (!empty($_REQUEST['field'])) {
  1709. $node = (object)array('type' => 'page');
  1710. field_attach_form('node', $node, $form, $form_state);
  1711. }
  1712. }
  1713. /**
  1714. * Submit callback that just lets the form rebuild.
  1715. */
  1716. function form_test_user_register_form_rebuild($form, &$form_state) {
  1717. drupal_set_message('Form rebuilt.');
  1718. $form_state['rebuild'] = TRUE;
  1719. }
  1720. /**
  1721. * Menu callback that returns two instances of the node form.
  1722. */
  1723. function form_test_two_instances() {
  1724. global $user;
  1725. $node1 = (object) array(
  1726. 'uid' => $user->uid,
  1727. 'name' => (isset($user->name) ? $user->name : ''),
  1728. 'type' => 'page',
  1729. 'language' => LANGUAGE_NONE,
  1730. );
  1731. $node2 = clone($node1);
  1732. $return['node_form_1'] = drupal_get_form('page_node_form', $node1);
  1733. $return['node_form_2'] = drupal_get_form('page_node_form', $node2);
  1734. return $return;
  1735. }
  1736. /**
  1737. * Menu callback for testing custom form includes.
  1738. */
  1739. function form_test_load_include_custom($form, &$form_state) {
  1740. $form['button'] = array(
  1741. '#type' => 'submit',
  1742. '#value' => t('Save'),
  1743. '#submit' => array('form_test_load_include_submit'),
  1744. );
  1745. // Specify the include file and enable form caching. That way the form is
  1746. // cached when it is submitted, but needs to find the specified submit handler
  1747. // in the include.
  1748. // Filename is a bit weird here: modules/simpletest/tests/form_test.file.inc
  1749. form_load_include($form_state, 'inc', 'form_test', 'form_test.file');
  1750. $form_state['cache'] = TRUE;
  1751. return $form;
  1752. }
  1753. function form_test_checkbox_type_juggling($form, $form_state, $default_value, $return_value) {
  1754. $form['checkbox'] = array(
  1755. '#type' => 'checkbox',
  1756. '#return_value' => $return_value,
  1757. '#default_value' => $default_value,
  1758. );
  1759. return $form;
  1760. }
  1761. function form_test_checkboxes_zero($form, &$form_state, $json = TRUE) {
  1762. $form['checkbox_off'] = array(
  1763. '#type' => 'checkboxes',
  1764. '#options' => array('foo', 'bar', 'baz'),
  1765. );
  1766. $form['checkbox_zero_default'] = array(
  1767. '#type' => 'checkboxes',
  1768. '#options' => array('foo', 'bar', 'baz'),
  1769. '#default_value' => array(0),
  1770. );
  1771. $form['checkbox_string_zero_default'] = array(
  1772. '#type' => 'checkboxes',
  1773. '#options' => array('foo', 'bar', 'baz'),
  1774. '#default_value' => array('0'),
  1775. );
  1776. $form['submit'] = array(
  1777. '#type' => 'submit',
  1778. '#value' => 'Save',
  1779. );
  1780. if ($json) {
  1781. $form['#submit'][] = '_form_test_checkbox_submit';
  1782. }
  1783. else {
  1784. $form['#submit'][] = '_form_test_checkboxes_zero_no_redirect';
  1785. }
  1786. return $form;
  1787. }
  1788. function _form_test_checkboxes_zero_no_redirect($form, &$form_state) {
  1789. $form_state['redirect'] = FALSE;
  1790. }
  1791. /**
  1792. * Menu callback returns two instances of the same form.
  1793. */
  1794. function form_test_double_form() {
  1795. return array(
  1796. 'form1' => drupal_get_form('form_test_html_id'),
  1797. 'form2' => drupal_get_form('form_test_html_id'),
  1798. );
  1799. }
  1800. /**
  1801. * Builds a simple form to test duplicate HTML IDs.
  1802. */
  1803. function form_test_html_id($form, &$form_state) {
  1804. $form['name'] = array(
  1805. '#type' => 'textfield',
  1806. '#title' => 'name',
  1807. '#required' => TRUE,
  1808. );
  1809. $form['submit'] = array(
  1810. '#type' => 'submit',
  1811. '#value' => 'Save',
  1812. );
  1813. return $form;
  1814. }