form_test.module 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008
  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 simple form for testing form caching.
  826. */
  827. function form_test_cache_form($form, &$form_state) {
  828. $form['title'] = array(
  829. '#type' => 'textfield',
  830. '#title' => 'Title',
  831. '#required' => TRUE,
  832. );
  833. $form['submit'] = array(
  834. '#type' => 'submit',
  835. '#value' => 'Save',
  836. );
  837. return $form;
  838. }
  839. /**
  840. * A form for testing form labels and required marks.
  841. */
  842. function form_label_test_form() {
  843. $form['form_checkboxes_test'] = array(
  844. '#type' => 'checkboxes',
  845. '#title' => t('Checkboxes test'),
  846. '#options' => array(
  847. 'first-checkbox' => t('First checkbox'),
  848. 'second-checkbox' => t('Second checkbox'),
  849. 'third-checkbox' => t('Third checkbox'),
  850. '0' => t('0'),
  851. ),
  852. );
  853. $form['form_radios_test'] = array(
  854. '#type' => 'radios',
  855. '#title' => t('Radios test'),
  856. '#options' => array(
  857. 'first-radio' => t('First radio'),
  858. 'second-radio' => t('Second radio'),
  859. 'third-radio' => t('Third radio'),
  860. '0' => t('0'),
  861. ),
  862. // Test #field_prefix and #field_suffix placement.
  863. '#field_prefix' => '<span id="form-test-radios-field-prefix">' . t('Radios #field_prefix element') . '</span>',
  864. '#field_suffix' => '<span id="form-test-radios-field-suffix">' . t('Radios #field_suffix element') . '</span>',
  865. );
  866. $form['form_checkbox_test'] = array(
  867. '#type' => 'checkbox',
  868. '#title' => t('Checkbox test'),
  869. );
  870. $form['form_textfield_test_title_and_required'] = array(
  871. '#type' => 'textfield',
  872. '#title' => t('Textfield test for required with title'),
  873. '#required' => TRUE,
  874. );
  875. $form['form_textfield_test_no_title_required'] = array(
  876. '#type' => 'textfield',
  877. // We use an empty title, since not setting #title suppresses the label
  878. // and required marker.
  879. '#title' => '',
  880. '#required' => TRUE,
  881. );
  882. $form['form_textfield_test_title'] = array(
  883. '#type' => 'textfield',
  884. '#title' => t('Textfield test for title only'),
  885. // Not required.
  886. // Test #prefix and #suffix placement.
  887. '#prefix' => '<div id="form-test-textfield-title-prefix">' . t('Textfield #prefix element') . '</div>',
  888. '#suffix' => '<div id="form-test-textfield-title-suffix">' . t('Textfield #suffix element') . '</div>',
  889. );
  890. $form['form_textfield_test_title_after'] = array(
  891. '#type' => 'textfield',
  892. '#title' => t('Textfield test for title after element'),
  893. '#title_display' => 'after',
  894. );
  895. $form['form_textfield_test_title_invisible'] = array(
  896. '#type' => 'textfield',
  897. '#title' => t('Textfield test for invisible title'),
  898. '#title_display' => 'invisible',
  899. );
  900. // Textfield test for title set not to display.
  901. $form['form_textfield_test_title_no_show'] = array(
  902. '#type' => 'textfield',
  903. );
  904. // Checkboxes & radios with title as attribute.
  905. $form['form_checkboxes_title_attribute'] = array(
  906. '#type' => 'checkboxes',
  907. '#title' => 'Checkboxes test',
  908. '#options' => array(
  909. 'first-checkbox' => 'First checkbox',
  910. 'second-checkbox' => 'Second checkbox',
  911. ),
  912. '#title_display' => 'attribute',
  913. '#required' => TRUE,
  914. );
  915. $form['form_radios_title_attribute'] = array(
  916. '#type' => 'radios',
  917. '#title' => 'Radios test',
  918. '#options' => array(
  919. 'first-radio' => 'First radio',
  920. 'second-radio' => 'Second radio',
  921. ),
  922. '#title_display' => 'attribute',
  923. '#required' => TRUE,
  924. );
  925. return $form;
  926. }
  927. /**
  928. * Menu callback; Invokes a form builder function with a wrapper callback.
  929. */
  930. function form_test_wrapper_callback($form_id) {
  931. $form_state = array(
  932. 'build_info' => array('args' => array()),
  933. 'wrapper_callback' => 'form_test_wrapper_callback_wrapper',
  934. );
  935. return drupal_build_form($form_id, $form_state);
  936. }
  937. /**
  938. * Form wrapper for form_test_wrapper_callback_form().
  939. */
  940. function form_test_wrapper_callback_wrapper($form, &$form_state) {
  941. $form['wrapper'] = array('#markup' => 'Form wrapper callback element output.');
  942. return $form;
  943. }
  944. /**
  945. * Form builder for form wrapper callback test.
  946. */
  947. function form_test_wrapper_callback_form($form, &$form_state) {
  948. $form['builder'] = array('#markup' => 'Form builder element output.');
  949. return $form;
  950. }
  951. /**
  952. * Form builder for form_state_values_clean() test.
  953. */
  954. function form_test_form_state_values_clean_form($form, &$form_state) {
  955. // Build an example form containing multiple submit and button elements; not
  956. // only on the top-level.
  957. $form = array('#tree' => TRUE);
  958. $form['foo'] = array('#type' => 'submit', '#value' => t('Submit'));
  959. $form['bar'] = array('#type' => 'submit', '#value' => t('Submit'));
  960. $form['beer'] = array('#type' => 'value', '#value' => 1000);
  961. $form['baz']['foo'] = array('#type' => 'button', '#value' => t('Submit'));
  962. $form['baz']['baz'] = array('#type' => 'submit', '#value' => t('Submit'));
  963. $form['baz']['beer'] = array('#type' => 'value', '#value' => 2000);
  964. return $form;
  965. }
  966. /**
  967. * Form submit handler for form_state_values_clean() test form.
  968. */
  969. function form_test_form_state_values_clean_form_submit($form, &$form_state) {
  970. form_state_values_clean($form_state);
  971. drupal_json_output($form_state['values']);
  972. exit;
  973. }
  974. /**
  975. * Form constructor for the form_state_values_clean() test.
  976. */
  977. function form_test_form_state_values_clean_advanced_form($form, &$form_state) {
  978. // Build an example form containing a managed file and a submit form element.
  979. $form['image'] = array(
  980. '#type' => 'managed_file',
  981. '#title' => t('Image'),
  982. '#upload_location' => 'public://',
  983. '#default_value' => 0,
  984. );
  985. $form['submit'] = array(
  986. '#type' => 'submit',
  987. '#value' => t('Submit'),
  988. );
  989. return $form;
  990. }
  991. /**
  992. * Form submission handler for form_test_form_state_values_clean_advanced_form().
  993. */
  994. function form_test_form_state_values_clean_advanced_form_submit($form, &$form_state) {
  995. form_state_values_clean($form_state);
  996. print t('You WIN!');
  997. exit;
  998. }
  999. /**
  1000. * Build a form to test a checkbox.
  1001. */
  1002. function _form_test_checkbox($form, &$form_state) {
  1003. // A required checkbox.
  1004. $form['required_checkbox'] = array(
  1005. '#type' => 'checkbox',
  1006. '#required' => TRUE,
  1007. '#title' => 'required_checkbox',
  1008. );
  1009. // A disabled checkbox should get its default value back.
  1010. $form['disabled_checkbox_on'] = array(
  1011. '#type' => 'checkbox',
  1012. '#disabled' => TRUE,
  1013. '#return_value' => 'disabled_checkbox_on',
  1014. '#default_value' => 'disabled_checkbox_on',
  1015. '#title' => 'disabled_checkbox_on',
  1016. );
  1017. $form['disabled_checkbox_off'] = array(
  1018. '#type' => 'checkbox',
  1019. '#disabled' => TRUE,
  1020. '#return_value' => 'disabled_checkbox_off',
  1021. '#default_value' => NULL,
  1022. '#title' => 'disabled_checkbox_off',
  1023. );
  1024. // A checkbox is active when #default_value == #return_value.
  1025. $form['checkbox_on'] = array(
  1026. '#type' => 'checkbox',
  1027. '#return_value' => 'checkbox_on',
  1028. '#default_value' => 'checkbox_on',
  1029. '#title' => 'checkbox_on',
  1030. );
  1031. // But inactive in any other case.
  1032. $form['checkbox_off'] = array(
  1033. '#type' => 'checkbox',
  1034. '#return_value' => 'checkbox_off',
  1035. '#default_value' => 'checkbox_on',
  1036. '#title' => 'checkbox_off',
  1037. );
  1038. // Checkboxes with a #return_value of '0' are supported.
  1039. $form['zero_checkbox_on'] = array(
  1040. '#type' => 'checkbox',
  1041. '#return_value' => '0',
  1042. '#default_value' => '0',
  1043. '#title' => 'zero_checkbox_on',
  1044. );
  1045. // In that case, passing a #default_value != '0' means that the checkbox is off.
  1046. $form['zero_checkbox_off'] = array(
  1047. '#type' => 'checkbox',
  1048. '#return_value' => '0',
  1049. '#default_value' => '1',
  1050. '#title' => 'zero_checkbox_off',
  1051. );
  1052. $form['submit'] = array(
  1053. '#type' => 'submit',
  1054. '#value' => t('Submit')
  1055. );
  1056. return $form;
  1057. }
  1058. /**
  1059. * Return the form values via JSON.
  1060. */
  1061. function _form_test_checkbox_submit($form, &$form_state) {
  1062. drupal_json_output($form_state['values']);
  1063. exit();
  1064. }
  1065. /**
  1066. * Builds a form to test #type 'select' validation.
  1067. */
  1068. function form_test_select($form, &$form_state) {
  1069. $base = array(
  1070. '#type' => 'select',
  1071. '#options' => drupal_map_assoc(array('one', 'two', 'three')),
  1072. );
  1073. $form['select'] = $base + array(
  1074. '#title' => '#default_value one',
  1075. '#default_value' => 'one',
  1076. );
  1077. $form['select_required'] = $base + array(
  1078. '#title' => '#default_value one, #required',
  1079. '#required' => TRUE,
  1080. '#default_value' => 'one',
  1081. );
  1082. $form['select_optional'] = $base + array(
  1083. '#title' => '#default_value one, not #required',
  1084. '#required' => FALSE,
  1085. '#default_value' => 'one',
  1086. );
  1087. $form['empty_value'] = $base + array(
  1088. '#title' => '#default_value one, #required, #empty_value 0',
  1089. '#required' => TRUE,
  1090. '#default_value' => 'one',
  1091. '#empty_value' => 0,
  1092. );
  1093. $form['empty_value_one'] = $base + array(
  1094. '#title' => '#default_value = #empty_value, #required',
  1095. '#required' => TRUE,
  1096. '#default_value' => 'one',
  1097. '#empty_value' => 'one',
  1098. );
  1099. $form['no_default'] = $base + array(
  1100. '#title' => 'No #default_value, #required',
  1101. '#required' => TRUE,
  1102. );
  1103. $form['no_default_optional'] = $base + array(
  1104. '#title' => 'No #default_value, not #required',
  1105. '#required' => FALSE,
  1106. '#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.',
  1107. );
  1108. $form['no_default_optional_empty_value'] = $base + array(
  1109. '#title' => 'No #default_value, not #required, #empty_value empty string',
  1110. '#empty_value' => '',
  1111. '#required' => FALSE,
  1112. '#description' => 'Should result in an empty string (due to #empty_value), because it is optional.',
  1113. );
  1114. $form['no_default_empty_option'] = $base + array(
  1115. '#title' => 'No #default_value, #required, #empty_option',
  1116. '#required' => TRUE,
  1117. '#empty_option' => '- Choose -',
  1118. );
  1119. $form['no_default_empty_option_optional'] = $base + array(
  1120. '#title' => 'No #default_value, not #required, #empty_option',
  1121. '#empty_option' => '- Dismiss -',
  1122. '#description' => 'Should result in an empty string (default of #empty_value), because it is optional.',
  1123. );
  1124. $form['no_default_empty_value'] = $base + array(
  1125. '#title' => 'No #default_value, #required, #empty_value 0',
  1126. '#required' => TRUE,
  1127. '#empty_value' => 0,
  1128. '#description' => 'Should never result in 0.',
  1129. );
  1130. $form['no_default_empty_value_one'] = $base + array(
  1131. '#title' => 'No #default_value, #required, #empty_value one',
  1132. '#required' => TRUE,
  1133. '#empty_value' => 'one',
  1134. '#description' => 'A mistakenly assigned #empty_value contained in #options should not be valid.',
  1135. );
  1136. $form['no_default_empty_value_optional'] = $base + array(
  1137. '#title' => 'No #default_value, not #required, #empty_value 0',
  1138. '#required' => FALSE,
  1139. '#empty_value' => 0,
  1140. '#description' => 'Should result in 0, because it is optional.',
  1141. );
  1142. $form['multiple'] = $base + array(
  1143. '#title' => '#multiple, #default_value two',
  1144. '#default_value' => array('two'),
  1145. '#multiple' => TRUE,
  1146. );
  1147. $form['multiple_no_default'] = $base + array(
  1148. '#title' => '#multiple, no #default_value',
  1149. '#multiple' => TRUE,
  1150. );
  1151. $form['multiple_no_default_required'] = $base + array(
  1152. '#title' => '#multiple, #required, no #default_value',
  1153. '#required' => TRUE,
  1154. '#multiple' => TRUE,
  1155. );
  1156. $form['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  1157. return $form;
  1158. }
  1159. /**
  1160. * Form submit handler for form_test_select().
  1161. */
  1162. function form_test_select_submit($form, &$form_state) {
  1163. drupal_json_output($form_state['values']);
  1164. exit();
  1165. }
  1166. /**
  1167. * Form constructor to test expansion of #type checkboxes and radios.
  1168. */
  1169. function form_test_checkboxes_radios($form, &$form_state, $customize = FALSE) {
  1170. $form['#submit'] = array('_form_test_submit_values_json');
  1171. // Expand #type checkboxes, setting custom element properties for some but not
  1172. // all options.
  1173. $form['checkboxes'] = array(
  1174. '#type' => 'checkboxes',
  1175. '#title' => 'Checkboxes',
  1176. '#options' => array(
  1177. 0 => 'Zero',
  1178. 'foo' => 'Foo',
  1179. 1 => 'One',
  1180. 'bar' => 'Bar',
  1181. '>' => 'Special Char',
  1182. ),
  1183. );
  1184. if ($customize) {
  1185. $form['checkboxes'] += array(
  1186. 'foo' => array(
  1187. '#description' => 'Enable to foo.',
  1188. ),
  1189. 1 => array(
  1190. '#weight' => 10,
  1191. ),
  1192. );
  1193. }
  1194. // Expand #type radios, setting custom element properties for some but not
  1195. // all options.
  1196. $form['radios'] = array(
  1197. '#type' => 'radios',
  1198. '#title' => 'Radios',
  1199. '#options' => array(
  1200. 0 => 'Zero',
  1201. 'foo' => 'Foo',
  1202. 1 => 'One',
  1203. 'bar' => 'Bar',
  1204. '>' => 'Special Char',
  1205. ),
  1206. );
  1207. if ($customize) {
  1208. $form['radios'] += array(
  1209. 'foo' => array(
  1210. '#description' => 'Enable to foo.',
  1211. ),
  1212. 1 => array(
  1213. '#weight' => 10,
  1214. ),
  1215. );
  1216. }
  1217. $form['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  1218. return $form;
  1219. }
  1220. /**
  1221. * Build a form to test disabled elements.
  1222. */
  1223. function _form_test_disabled_elements($form, &$form_state) {
  1224. // Elements that take a simple default value.
  1225. foreach (array('textfield', 'textarea', 'hidden') as $type) {
  1226. $form[$type] = array(
  1227. '#type' => $type,
  1228. '#title' => $type,
  1229. '#default_value' => $type,
  1230. '#test_hijack_value' => 'HIJACK',
  1231. '#disabled' => TRUE,
  1232. );
  1233. }
  1234. // Multiple values option elements.
  1235. foreach (array('checkboxes', 'select') as $type) {
  1236. $form[$type . '_multiple'] = array(
  1237. '#type' => $type,
  1238. '#title' => $type . ' (multiple)',
  1239. '#options' => array(
  1240. 'test_1' => 'Test 1',
  1241. 'test_2' => 'Test 2',
  1242. ),
  1243. '#multiple' => TRUE,
  1244. '#default_value' => array('test_2' => 'test_2'),
  1245. // The keys of #test_hijack_value need to match the #name of the control.
  1246. // @see FormsTestCase::testDisabledElements()
  1247. '#test_hijack_value' => $type == 'select' ? array('' => 'test_1') : array('test_1' => 'test_1'),
  1248. '#disabled' => TRUE,
  1249. );
  1250. }
  1251. // Single values option elements.
  1252. foreach (array('radios', 'select') as $type) {
  1253. $form[$type . '_single'] = array(
  1254. '#type' => $type,
  1255. '#title' => $type . ' (single)',
  1256. '#options' => array(
  1257. 'test_1' => 'Test 1',
  1258. 'test_2' => 'Test 2',
  1259. ),
  1260. '#multiple' => FALSE,
  1261. '#default_value' => 'test_2',
  1262. '#test_hijack_value' => 'test_1',
  1263. '#disabled' => TRUE,
  1264. );
  1265. }
  1266. // Checkbox and radio.
  1267. foreach (array('checkbox', 'radio') as $type) {
  1268. $form[$type . '_unchecked'] = array(
  1269. '#type' => $type,
  1270. '#title' => $type . ' (unchecked)',
  1271. '#return_value' => 1,
  1272. '#default_value' => 0,
  1273. '#test_hijack_value' => 1,
  1274. '#disabled' => TRUE,
  1275. );
  1276. $form[$type . '_checked'] = array(
  1277. '#type' => $type,
  1278. '#title' => $type . ' (checked)',
  1279. '#return_value' => 1,
  1280. '#default_value' => 1,
  1281. '#test_hijack_value' => NULL,
  1282. '#disabled' => TRUE,
  1283. );
  1284. }
  1285. // Weight.
  1286. $form['weight'] = array(
  1287. '#type' => 'weight',
  1288. '#title' => 'weight',
  1289. '#default_value' => 10,
  1290. '#test_hijack_value' => 5,
  1291. '#disabled' => TRUE,
  1292. );
  1293. // Date.
  1294. $form['date'] = array(
  1295. '#type' => 'date',
  1296. '#title' => 'date',
  1297. '#disabled' => TRUE,
  1298. '#default_value' => array(
  1299. 'day' => 19,
  1300. 'month' => 11,
  1301. 'year' => 1978,
  1302. ),
  1303. '#test_hijack_value' => array(
  1304. 'day' => 20,
  1305. 'month' => 12,
  1306. 'year' => 1979,
  1307. ),
  1308. );
  1309. // The #disabled state should propagate to children.
  1310. $form['disabled_container'] = array(
  1311. '#disabled' => TRUE,
  1312. );
  1313. foreach (array('textfield', 'textarea', 'hidden') as $type) {
  1314. $form['disabled_container']['disabled_container_' . $type] = array(
  1315. '#type' => $type,
  1316. '#title' => $type,
  1317. '#default_value' => $type,
  1318. '#test_hijack_value' => 'HIJACK',
  1319. );
  1320. }
  1321. // Text format.
  1322. $form['text_format'] = array(
  1323. '#type' => 'text_format',
  1324. '#title' => 'Text format',
  1325. '#disabled' => TRUE,
  1326. '#default_value' => 'Text value',
  1327. '#format' => 'plain_text',
  1328. '#expected_value' => array(
  1329. 'value' => 'Text value',
  1330. 'format' => 'plain_text',
  1331. ),
  1332. '#test_hijack_value' => array(
  1333. 'value' => 'HIJACK',
  1334. 'format' => 'filtered_html',
  1335. ),
  1336. );
  1337. // Password fields.
  1338. $form['password'] = array(
  1339. '#type' => 'password',
  1340. '#title' => 'Password',
  1341. '#disabled' => TRUE,
  1342. );
  1343. $form['password_confirm'] = array(
  1344. '#type' => 'password_confirm',
  1345. '#title' => 'Password confirm',
  1346. '#disabled' => TRUE,
  1347. );
  1348. // Files.
  1349. $form['file'] = array(
  1350. '#type' => 'file',
  1351. '#title' => 'File',
  1352. '#disabled' => TRUE,
  1353. );
  1354. $form['managed_file'] = array(
  1355. '#type' => 'managed_file',
  1356. '#title' => 'Managed file',
  1357. '#disabled' => TRUE,
  1358. );
  1359. // Buttons.
  1360. $form['image_button'] = array(
  1361. '#type' => 'image_button',
  1362. '#value' => 'Image button',
  1363. '#disabled' => TRUE,
  1364. );
  1365. $form['button'] = array(
  1366. '#type' => 'button',
  1367. '#value' => 'Button',
  1368. '#disabled' => TRUE,
  1369. );
  1370. $form['submit_disabled'] = array(
  1371. '#type' => 'submit',
  1372. '#value' => 'Submit',
  1373. '#disabled' => TRUE,
  1374. );
  1375. $form['submit'] = array(
  1376. '#type' => 'submit',
  1377. '#value' => t('Submit'),
  1378. );
  1379. return $form;
  1380. }
  1381. /**
  1382. * Return the form values via JSON.
  1383. */
  1384. function _form_test_disabled_elements_submit($form, &$form_state) {
  1385. drupal_json_output($form_state['values']);
  1386. exit();
  1387. }
  1388. /**
  1389. * Build a form to test input forgery of enabled elements.
  1390. */
  1391. function _form_test_input_forgery($form, &$form_state) {
  1392. // For testing that a user can't submit a value not matching one of the
  1393. // allowed options.
  1394. $form['checkboxes'] = array(
  1395. '#type' => 'checkboxes',
  1396. '#options' => array(
  1397. 'one' => 'One',
  1398. 'two' => 'Two',
  1399. ),
  1400. );
  1401. $form['submit'] = array(
  1402. '#type' => 'submit',
  1403. '#value' => t('Submit'),
  1404. );
  1405. return $form;
  1406. }
  1407. /**
  1408. * Return the form values via JSON.
  1409. */
  1410. function _form_test_input_forgery_submit($form, &$form_state) {
  1411. drupal_json_output($form_state['values']);
  1412. exit();
  1413. }
  1414. /**
  1415. * Form builder for testing preservation of values during a rebuild.
  1416. */
  1417. function form_test_form_rebuild_preserve_values_form($form, &$form_state) {
  1418. // Start the form with two checkboxes, to test different defaults, and a
  1419. // textfield, to test more than one element type.
  1420. $form = array(
  1421. 'checkbox_1_default_off' => array(
  1422. '#type' => 'checkbox',
  1423. '#title' => t('This checkbox defaults to unchecked.'),
  1424. '#default_value' => FALSE,
  1425. ),
  1426. 'checkbox_1_default_on' => array(
  1427. '#type' => 'checkbox',
  1428. '#title' => t('This checkbox defaults to checked.'),
  1429. '#default_value' => TRUE,
  1430. ),
  1431. 'text_1' => array(
  1432. '#type' => 'textfield',
  1433. '#title' => t('This textfield has a non-empty default value.'),
  1434. '#default_value' => 'DEFAULT 1',
  1435. ),
  1436. );
  1437. // Provide an 'add more' button that rebuilds the form with an additional two
  1438. // checkboxes and a textfield. The test is to make sure that the rebuild
  1439. // triggered by this button preserves the user input values for the initial
  1440. // elements and initializes the new elements with the correct default values.
  1441. if (empty($form_state['storage']['add_more'])) {
  1442. $form['add_more'] = array(
  1443. '#type' => 'submit',
  1444. '#value' => 'Add more',
  1445. '#submit' => array('form_test_form_rebuild_preserve_values_form_add_more'),
  1446. );
  1447. }
  1448. else {
  1449. $form += array(
  1450. 'checkbox_2_default_off' => array(
  1451. '#type' => 'checkbox',
  1452. '#title' => t('This checkbox defaults to unchecked.'),
  1453. '#default_value' => FALSE,
  1454. ),
  1455. 'checkbox_2_default_on' => array(
  1456. '#type' => 'checkbox',
  1457. '#title' => t('This checkbox defaults to checked.'),
  1458. '#default_value' => TRUE,
  1459. ),
  1460. 'text_2' => array(
  1461. '#type' => 'textfield',
  1462. '#title' => t('This textfield has a non-empty default value.'),
  1463. '#default_value' => 'DEFAULT 2',
  1464. ),
  1465. );
  1466. }
  1467. // A submit button that finishes the form workflow (does not rebuild).
  1468. $form['submit'] = array(
  1469. '#type' => 'submit',
  1470. '#value' => 'Submit',
  1471. );
  1472. return $form;
  1473. }
  1474. /**
  1475. * Button submit handler for form_test_form_rebuild_preserve_values_form().
  1476. */
  1477. function form_test_form_rebuild_preserve_values_form_add_more($form, &$form_state) {
  1478. // Rebuild, to test preservation of input values.
  1479. $form_state['storage']['add_more'] = TRUE;
  1480. $form_state['rebuild'] = TRUE;
  1481. }
  1482. /**
  1483. * Form submit handler for form_test_form_rebuild_preserve_values_form().
  1484. */
  1485. function form_test_form_rebuild_preserve_values_form_submit($form, &$form_state) {
  1486. // Finish the workflow. Do not rebuild.
  1487. drupal_set_message(t('Form values: %values', array('%values' => var_export($form_state['values'], TRUE))));
  1488. }
  1489. /**
  1490. * Form constructor for testing form state persistence.
  1491. */
  1492. function form_test_state_persist($form, &$form_state) {
  1493. $form['title'] = array(
  1494. '#type' => 'textfield',
  1495. '#title' => 'title',
  1496. '#default_value' => 'DEFAULT',
  1497. '#required' => TRUE,
  1498. );
  1499. $form_state['value'] = 'State persisted.';
  1500. $form['submit'] = array(
  1501. '#type' => 'submit',
  1502. '#value' => t('Submit'),
  1503. );
  1504. return $form;
  1505. }
  1506. /**
  1507. * Submit handler.
  1508. *
  1509. * @see form_test_state_persist()
  1510. */
  1511. function form_test_state_persist_submit($form, &$form_state) {
  1512. drupal_set_message($form_state['value']);
  1513. $form_state['rebuild'] = TRUE;
  1514. }
  1515. /**
  1516. * Implements hook_form_FORM_ID_alter().
  1517. *
  1518. * @see form_test_state_persist()
  1519. */
  1520. function form_test_form_form_test_state_persist_alter(&$form, &$form_state) {
  1521. // Simulate a form alter implementation inserting form elements that enable
  1522. // caching of the form, e.g. elements having #ajax.
  1523. if (!empty($_REQUEST['cache'])) {
  1524. $form_state['cache'] = TRUE;
  1525. }
  1526. }
  1527. /**
  1528. * Form builder to test programmatic form submissions.
  1529. */
  1530. function form_test_programmatic_form($form, &$form_state) {
  1531. $form['textfield'] = array(
  1532. '#title' => 'Textfield',
  1533. '#type' => 'textfield',
  1534. );
  1535. $form['checkboxes'] = array(
  1536. '#type' => 'checkboxes',
  1537. '#options' => array(
  1538. 1 => 'First checkbox',
  1539. 2 => 'Second checkbox',
  1540. ),
  1541. // Both checkboxes are selected by default so that we can test the ability
  1542. // of programmatic form submissions to uncheck them.
  1543. '#default_value' => array(1, 2),
  1544. );
  1545. // This is used to test that programmatic form submissions can bypass #access
  1546. // restrictions.
  1547. $form['textfield_no_access'] = array(
  1548. '#type' => 'textfield',
  1549. '#title' => 'Textfield no access',
  1550. '#default_value' => 'default value',
  1551. '#access' => FALSE,
  1552. );
  1553. $form['field_to_validate'] = array(
  1554. '#type' => 'radios',
  1555. '#title' => 'Field to validate (in the case of limited validation)',
  1556. '#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.',
  1557. '#options' => array(
  1558. 'all' => 'Validate all fields',
  1559. 'textfield' => 'Validate the "Textfield" field',
  1560. 'field_to_validate' => 'Validate the "Field to validate" field',
  1561. ),
  1562. '#default_value' => 'all',
  1563. );
  1564. // The main submit button for the form.
  1565. $form['submit'] = array(
  1566. '#type' => 'submit',
  1567. '#value' => 'Submit',
  1568. );
  1569. // A secondary submit button that allows validation to be limited based on
  1570. // the value of the above radio selector.
  1571. $form['submit_limit_validation'] = array(
  1572. '#type' => 'submit',
  1573. '#value' => 'Submit with limited validation',
  1574. // Use the same submit handler for this button as for the form itself.
  1575. // (This must be set explicitly or otherwise the form API will ignore the
  1576. // #limit_validation_errors property.)
  1577. '#submit' => array('form_test_programmatic_form_submit'),
  1578. );
  1579. if (!empty($form_state['input']['field_to_validate']) && $form_state['input']['field_to_validate'] != 'all') {
  1580. $form['submit_limit_validation']['#limit_validation_errors'] = array(
  1581. array($form_state['input']['field_to_validate']),
  1582. );
  1583. }
  1584. return $form;
  1585. }
  1586. /**
  1587. * Form validation handler for programmatic form submissions.
  1588. *
  1589. * To test that the validation handler is correctly executed, the field value is
  1590. * explicitly required here.
  1591. */
  1592. function form_test_programmatic_form_validate($form, &$form_state) {
  1593. if (empty($form_state['values']['textfield'])) {
  1594. form_set_error('textfield', t('Textfield is required.'));
  1595. }
  1596. }
  1597. /**
  1598. * Form submit handler for programmatic form submissions.
  1599. *
  1600. * To test that the submission handler is correctly executed, we store the
  1601. * submitted values in a place we can access from the caller context.
  1602. */
  1603. function form_test_programmatic_form_submit($form, &$form_state) {
  1604. $form_state['storage']['programmatic_form_submit'] = $form_state['values'];
  1605. }
  1606. /**
  1607. * Form builder to test button click detection.
  1608. */
  1609. function form_test_clicked_button($form, &$form_state) {
  1610. // A single text field. In IE, when a form has only one non-button input field
  1611. // and the ENTER key is pressed while that field has focus, the form is
  1612. // submitted without any information identifying the button responsible for
  1613. // the submission. In other browsers, the form is submitted as though the
  1614. // first button were clicked.
  1615. $form['text'] = array(
  1616. '#title' => 'Text',
  1617. '#type' => 'textfield',
  1618. );
  1619. // Loop through each path argument, addding buttons based on the information
  1620. // in the argument. For example, if the path is
  1621. // form-test/clicked-button/s/i/rb, then 3 buttons are added: a 'submit', an
  1622. // 'image_button', and a 'button' with #access=FALSE. This enables form.test
  1623. // to test a variety of combinations.
  1624. $i=0;
  1625. $args = array_slice(arg(), 2);
  1626. foreach ($args as $arg) {
  1627. $name = 'button' . ++$i;
  1628. // 's', 'b', or 'i' in the argument define the button type wanted.
  1629. if (strpos($arg, 's') !== FALSE) {
  1630. $type = 'submit';
  1631. }
  1632. elseif (strpos($arg, 'b') !== FALSE) {
  1633. $type = 'button';
  1634. }
  1635. elseif (strpos($arg, 'i') !== FALSE) {
  1636. $type = 'image_button';
  1637. }
  1638. else {
  1639. $type = NULL;
  1640. }
  1641. if (isset($type)) {
  1642. $form[$name] = array(
  1643. '#type' => $type,
  1644. '#name' => $name,
  1645. );
  1646. // Image buttons need a #src; the others need a #value.
  1647. if ($type == 'image_button') {
  1648. $form[$name]['#src'] = 'misc/druplicon.png';
  1649. }
  1650. else {
  1651. $form[$name]['#value'] = $name;
  1652. }
  1653. // 'r' for restricted, so we can test that button click detection code
  1654. // correctly takes #access security into account.
  1655. if (strpos($arg, 'r') !== FALSE) {
  1656. $form[$name]['#access'] = FALSE;
  1657. }
  1658. }
  1659. }
  1660. return $form;
  1661. }
  1662. /**
  1663. * Form validation handler for the form_test_clicked_button() form.
  1664. */
  1665. function form_test_clicked_button_validate($form, &$form_state) {
  1666. if (isset($form_state['triggering_element'])) {
  1667. drupal_set_message(t('The clicked button is %name.', array('%name' => $form_state['triggering_element']['#name'])));
  1668. }
  1669. else {
  1670. drupal_set_message('There is no clicked button.');
  1671. }
  1672. }
  1673. /**
  1674. * Form submit handler for the form_test_clicked_button() form.
  1675. */
  1676. function form_test_clicked_button_submit($form, &$form_state) {
  1677. drupal_set_message('Submit handler for form_test_clicked_button executed.');
  1678. }
  1679. /**
  1680. * Form builder to detect form redirect.
  1681. */
  1682. function form_test_redirect($form, &$form_state) {
  1683. $form['redirection'] = array(
  1684. '#type' => 'checkbox',
  1685. '#title' => t('Use redirection'),
  1686. );
  1687. $form['destination'] = array(
  1688. '#type' => 'textfield',
  1689. '#title' => t('Redirect destination'),
  1690. '#states' => array(
  1691. 'visible' => array(
  1692. ':input[name="redirection"]' => array('checked' => TRUE),
  1693. ),
  1694. ),
  1695. );
  1696. $form['submit'] = array(
  1697. '#type' => 'submit',
  1698. '#value' => t('Submit'),
  1699. );
  1700. return $form;
  1701. }
  1702. /**
  1703. * Form submit handler to test different redirect behaviours.
  1704. */
  1705. function form_test_redirect_submit(&$form, &$form_state) {
  1706. if (!empty($form_state['values']['redirection'])) {
  1707. $form_state['redirect'] = !empty($form_state['values']['destination']) ? $form_state['values']['destination'] : NULL;
  1708. }
  1709. else {
  1710. $form_state['redirect'] = FALSE;
  1711. }
  1712. }
  1713. /**
  1714. * Implements hook_form_FORM_ID_alter() for the registration form.
  1715. */
  1716. function form_test_form_user_register_form_alter(&$form, &$form_state) {
  1717. $form['test_rebuild'] = array(
  1718. '#type' => 'submit',
  1719. '#value' => t('Rebuild'),
  1720. '#submit' => array('form_test_user_register_form_rebuild'),
  1721. );
  1722. // If requested, add the test field by attaching the node page form.
  1723. if (!empty($_REQUEST['field'])) {
  1724. $node = (object)array('type' => 'page');
  1725. field_attach_form('node', $node, $form, $form_state);
  1726. }
  1727. }
  1728. /**
  1729. * Submit callback that just lets the form rebuild.
  1730. */
  1731. function form_test_user_register_form_rebuild($form, &$form_state) {
  1732. drupal_set_message('Form rebuilt.');
  1733. $form_state['rebuild'] = TRUE;
  1734. }
  1735. /**
  1736. * Menu callback that returns two instances of the node form.
  1737. */
  1738. function form_test_two_instances() {
  1739. global $user;
  1740. $node1 = (object) array(
  1741. 'uid' => $user->uid,
  1742. 'name' => (isset($user->name) ? $user->name : ''),
  1743. 'type' => 'page',
  1744. 'language' => LANGUAGE_NONE,
  1745. );
  1746. $node2 = clone($node1);
  1747. $return['node_form_1'] = drupal_get_form('page_node_form', $node1);
  1748. $return['node_form_2'] = drupal_get_form('page_node_form', $node2);
  1749. return $return;
  1750. }
  1751. /**
  1752. * Menu callback for testing custom form includes.
  1753. */
  1754. function form_test_load_include_custom($form, &$form_state) {
  1755. $form['button'] = array(
  1756. '#type' => 'submit',
  1757. '#value' => t('Save'),
  1758. '#submit' => array('form_test_load_include_submit'),
  1759. );
  1760. // Specify the include file and enable form caching. That way the form is
  1761. // cached when it is submitted, but needs to find the specified submit handler
  1762. // in the include.
  1763. // Filename is a bit weird here: modules/simpletest/tests/form_test.file.inc
  1764. form_load_include($form_state, 'inc', 'form_test', 'form_test.file');
  1765. $form_state['cache'] = TRUE;
  1766. return $form;
  1767. }
  1768. function form_test_checkbox_type_juggling($form, $form_state, $default_value, $return_value) {
  1769. $form['checkbox'] = array(
  1770. '#type' => 'checkbox',
  1771. '#return_value' => $return_value,
  1772. '#default_value' => $default_value,
  1773. );
  1774. return $form;
  1775. }
  1776. function form_test_checkboxes_zero($form, &$form_state, $json = TRUE) {
  1777. $form['checkbox_off'] = array(
  1778. '#type' => 'checkboxes',
  1779. '#options' => array('foo', 'bar', 'baz'),
  1780. );
  1781. $form['checkbox_zero_default'] = array(
  1782. '#type' => 'checkboxes',
  1783. '#options' => array('foo', 'bar', 'baz'),
  1784. '#default_value' => array(0),
  1785. );
  1786. $form['checkbox_string_zero_default'] = array(
  1787. '#type' => 'checkboxes',
  1788. '#options' => array('foo', 'bar', 'baz'),
  1789. '#default_value' => array('0'),
  1790. );
  1791. $form['submit'] = array(
  1792. '#type' => 'submit',
  1793. '#value' => 'Save',
  1794. );
  1795. if ($json) {
  1796. $form['#submit'][] = '_form_test_checkbox_submit';
  1797. }
  1798. else {
  1799. $form['#submit'][] = '_form_test_checkboxes_zero_no_redirect';
  1800. }
  1801. return $form;
  1802. }
  1803. function _form_test_checkboxes_zero_no_redirect($form, &$form_state) {
  1804. $form_state['redirect'] = FALSE;
  1805. }
  1806. /**
  1807. * Menu callback returns two instances of the same form.
  1808. */
  1809. function form_test_double_form() {
  1810. return array(
  1811. 'form1' => drupal_get_form('form_test_html_id'),
  1812. 'form2' => drupal_get_form('form_test_html_id'),
  1813. );
  1814. }
  1815. /**
  1816. * Builds a simple form to test duplicate HTML IDs.
  1817. */
  1818. function form_test_html_id($form, &$form_state) {
  1819. $form['name'] = array(
  1820. '#type' => 'textfield',
  1821. '#title' => 'name',
  1822. '#required' => TRUE,
  1823. );
  1824. $form['submit'] = array(
  1825. '#type' => 'submit',
  1826. '#value' => 'Save',
  1827. );
  1828. return $form;
  1829. }