form_test.module 52 KB

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