form_test.module 58 KB

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