ajax_example_graceful_degradation.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. <?php
  2. /**
  3. * @file
  4. * Demonstrations of AJAX with graceful degradation.
  5. */
  6. /**
  7. * @defgroup ajax_degradation_example Example: AJAX Graceful Degradation
  8. * @ingroup examples
  9. * @{
  10. * These examples show AJAX with graceful degradation when Javascript is not
  11. * available.
  12. *
  13. * In each of these the key idea is that the form is rebuilt different ways
  14. * depending on form input. In order to accomplish that, the formbuilder
  15. * function is in charge of almost all logic.
  16. */
  17. /**
  18. * Dropdown form based on previous choices.
  19. *
  20. * A form with a dropdown whose options are dependent on a choice made in a
  21. * previous dropdown.
  22. *
  23. * On changing the first dropdown, the options in the second
  24. * are updated. Gracefully degrades if no javascript.
  25. *
  26. * A bit of CSS and javascript is required. The CSS hides the "add more" button
  27. * if javascript is not enabled. The Javascript snippet is really only used
  28. * to enable us to present the form in degraded mode without forcing the user
  29. * to turn off Javascript. Both of these are loaded by using the
  30. * #attached FAPI property, so it is a good example of how to use that.
  31. *
  32. * The extra argument $no_js_use is here only to allow presentation of this
  33. * form as if Javascript were not enabled. ajax_example_menu() provides two
  34. * ways to call this form, one normal ($no_js_use = FALSE) and one simulating
  35. * Javascript disabled ($no_js_use = TRUE).
  36. */
  37. function ajax_example_dependent_dropdown_degrades($form, &$form_state, $no_js_use = FALSE) {
  38. // Get the list of options to populate the first dropdown.
  39. $options_first = _ajax_example_get_first_dropdown_options();
  40. // If we have a value for the first dropdown from $form_state['values'] we use
  41. // this both as the default value for the first dropdown and also as a
  42. // parameter to pass to the function that retrieves the options for the
  43. // second dropdown.
  44. $selected = isset($form_state['values']['dropdown_first']) ? $form_state['values']['dropdown_first'] : key($options_first);
  45. // Attach the CSS and JS we need to show this with and without javascript.
  46. // Without javascript we need an extra "Choose" button, and this is
  47. // hidden when we have javascript enabled.
  48. $form['#attached']['css'] = array(
  49. drupal_get_path('module', 'ajax_example') . '/ajax_example.css',
  50. );
  51. $form['#attached']['js'] = array(
  52. drupal_get_path('module', 'ajax_example') . '/ajax_example.js',
  53. );
  54. $form['dropdown_first_fieldset'] = array(
  55. '#type' => 'fieldset',
  56. );
  57. $form['dropdown_first_fieldset']['dropdown_first'] = array(
  58. '#type' => 'select',
  59. '#title' => 'Instrument Type',
  60. '#options' => $options_first,
  61. '#attributes' => array('class' => array('enabled-for-ajax')),
  62. // The '#ajax' property allows us to bind a callback to the server whenever
  63. // this form element changes. See ajax_example_autocheckboxes and
  64. // ajax_example_dependent_dropdown in ajax_example.module for more details.
  65. '#ajax' => array(
  66. 'callback' => 'ajax_example_dependent_dropdown_degrades_first_callback',
  67. 'wrapper' => 'dropdown-second-replace',
  68. ),
  69. );
  70. // This simply allows us to demonstrate no-javascript use without
  71. // actually turning off javascript in the browser. Removing the #ajax
  72. // element turns off AJAX behaviors on that element and as a result
  73. // ajax.js doesn't get loaded. This is for demonstration purposes only.
  74. if ($no_js_use) {
  75. unset($form['dropdown_first_fieldset']['dropdown_first']['#ajax']);
  76. }
  77. // Since we don't know if the user has js or not, we always need to output
  78. // this element, then hide it with with css if javascript is enabled.
  79. $form['dropdown_first_fieldset']['continue_to_second'] = array(
  80. '#type' => 'submit',
  81. '#value' => t('Choose'),
  82. '#attributes' => array('class' => array('next-button')),
  83. );
  84. $form['dropdown_second_fieldset'] = array(
  85. '#type' => 'fieldset',
  86. );
  87. $form['dropdown_second_fieldset']['dropdown_second'] = array(
  88. '#type' => 'select',
  89. '#title' => $options_first[$selected] . ' ' . t('Instruments'),
  90. '#prefix' => '<div id="dropdown-second-replace">',
  91. '#suffix' => '</div>',
  92. '#attributes' => array('class' => array('enabled-for-ajax')),
  93. // When the form is rebuilt during processing (either AJAX or multistep),
  94. // the $selected variable will now have the new value and so the options
  95. // will change.
  96. '#options' => _ajax_example_get_second_dropdown_options($selected),
  97. );
  98. $form['dropdown_second_fieldset']['submit'] = array(
  99. '#type' => 'submit',
  100. '#value' => t('OK'),
  101. // This class allows attached js file to override the disabled attribute,
  102. // since it's not necessary in ajax-enabled form.
  103. '#attributes' => array('class' => array('enabled-for-ajax')),
  104. );
  105. // Disable dropdown_second if a selection has not been made on dropdown_first.
  106. if (empty($form_state['values']['dropdown_first'])) {
  107. $form['dropdown_second_fieldset']['dropdown_second']['#disabled'] = TRUE;
  108. $form['dropdown_second_fieldset']['dropdown_second']['#description'] = t('You must make your choice on the first dropdown before changing this second one.');
  109. $form['dropdown_second_fieldset']['submit']['#disabled'] = TRUE;
  110. }
  111. return $form;
  112. }
  113. /**
  114. * Submit function for ajax_example_dependent_dropdown_degrades().
  115. */
  116. function ajax_example_dependent_dropdown_degrades_submit($form, &$form_state) {
  117. // Now handle the case of the next, previous, and submit buttons.
  118. // only submit will result in actual submission, all others rebuild.
  119. switch ($form_state['triggering_element']['#value']) {
  120. case t('OK'):
  121. // Submit: We're done.
  122. drupal_set_message(t('Your values have been submitted. dropdown_first=@first, dropdown_second=@second', array('@first' => $form_state['values']['dropdown_first'], '@second' => $form_state['values']['dropdown_second'])));
  123. return;
  124. }
  125. // 'Choose' or anything else will cause rebuild of the form and present
  126. // it again.
  127. $form_state['rebuild'] = TRUE;
  128. }
  129. /**
  130. * Selects just the second dropdown to be returned for re-rendering.
  131. *
  132. * @return array
  133. * Renderable array (the second dropdown).
  134. */
  135. function ajax_example_dependent_dropdown_degrades_first_callback($form, $form_state) {
  136. return $form['dropdown_second_fieldset']['dropdown_second'];
  137. }
  138. /**
  139. * Dynamically-enabled form with graceful no-JS degradation.
  140. *
  141. * Example of a form with portions dynamically enabled or disabled, but
  142. * with graceful degradation in the case of no javascript.
  143. *
  144. * The idea here is that certain parts of the form don't need to be displayed
  145. * unless a given option is selected, but then they should be displayed and
  146. * configured.
  147. *
  148. * The third $no_js_use argument is strictly for demonstrating operation
  149. * without javascript, without making the user/developer turn off javascript.
  150. */
  151. function ajax_example_dynamic_sections($form, &$form_state, $no_js_use = FALSE) {
  152. // Attach the CSS and JS we need to show this with and without javascript.
  153. // Without javascript we need an extra "Choose" button, and this is
  154. // hidden when we have javascript enabled.
  155. $form['#attached']['css'] = array(
  156. drupal_get_path('module', 'ajax_example') . '/ajax_example.css',
  157. );
  158. $form['#attached']['js'] = array(
  159. drupal_get_path('module', 'ajax_example') . '/ajax_example.js',
  160. );
  161. $form['description'] = array(
  162. '#type' => 'markup',
  163. '#markup' => '<div>' . t('This example demonstrates a form which dynamically creates various sections based on the configuration in the form.
  164. It deliberately allows graceful degradation to a non-javascript environment.
  165. In a non-javascript environment, the "Choose" button next to the select control
  166. is displayed; in a javascript environment it is hidden by the module CSS.
  167. <br/><br/>The basic idea here is that the form is built up based on
  168. the selection in the question_type_select field, and it is built the same
  169. whether we are in a javascript/AJAX environment or not.
  170. <br/><br/>
  171. Try the <a href="!ajax_link">AJAX version</a> and the <a href="!non_ajax_link">simulated-non-AJAX version</a>.
  172. ', array('!ajax_link' => url('examples/ajax_example/dynamic_sections'), '!non_ajax_link' => url('examples/ajax_example/dynamic_sections_no_js'))) . '</div>',
  173. );
  174. $form['question_type_select'] = array(
  175. '#type' => 'select',
  176. '#title' => t('Question style'),
  177. '#options' => drupal_map_assoc(
  178. array(
  179. t('Choose question style'),
  180. t('Multiple Choice'),
  181. t('True/False'),
  182. t('Fill-in-the-blanks'),
  183. )
  184. ),
  185. '#ajax' => array(
  186. 'wrapper' => 'questions-fieldset-wrapper',
  187. 'callback' => 'ajax_example_dynamic_sections_select_callback',
  188. ),
  189. );
  190. // The CSS for this module hides this next button if JS is enabled.
  191. $form['question_type_submit'] = array(
  192. '#type' => 'submit',
  193. '#value' => t('Choose'),
  194. '#attributes' => array('class' => array('next-button')),
  195. // No need to validate when submitting this.
  196. '#limit_validation_errors' => array(),
  197. '#validate' => array(),
  198. );
  199. // This simply allows us to demonstrate no-javascript use without
  200. // actually turning off javascript in the browser. Removing the #ajax
  201. // element turns off AJAX behaviors on that element and as a result
  202. // ajax.js doesn't get loaded.
  203. if ($no_js_use) {
  204. // Remove the #ajax from the above, so ajax.js won't be loaded.
  205. unset($form['question_type_select']['#ajax']);
  206. }
  207. // This fieldset just serves as a container for the part of the form
  208. // that gets rebuilt.
  209. $form['questions_fieldset'] = array(
  210. '#type' => 'fieldset',
  211. // These provide the wrapper referred to in #ajax['wrapper'] above.
  212. '#prefix' => '<div id="questions-fieldset-wrapper">',
  213. '#suffix' => '</div>',
  214. );
  215. if (!empty($form_state['values']['question_type_select'])) {
  216. $form['questions_fieldset']['question'] = array(
  217. '#markup' => t('Who was the first president of the U.S.?'),
  218. );
  219. $question_type = $form_state['values']['question_type_select'];
  220. switch ($question_type) {
  221. case t('Multiple Choice'):
  222. $form['questions_fieldset']['question'] = array(
  223. '#type' => 'radios',
  224. '#title' => t('Who was the first president of the United States'),
  225. '#options' => drupal_map_assoc(
  226. array(
  227. t('George Bush'),
  228. t('Adam McGuire'),
  229. t('Abraham Lincoln'),
  230. t('George Washington'),
  231. )
  232. ),
  233. );
  234. break;
  235. case t('True/False'):
  236. $form['questions_fieldset']['question'] = array(
  237. '#type' => 'radios',
  238. '#title' => t('Was George Washington the first president of the United States?'),
  239. '#options' => array(t('George Washington') => t("True"), 0 => t("False")),
  240. '#description' => t('Click "True" if you think George Washington was the first president of the United States.'),
  241. );
  242. break;
  243. case t('Fill-in-the-blanks'):
  244. $form['questions_fieldset']['question'] = array(
  245. '#type' => 'textfield',
  246. '#title' => t('Who was the first president of the United States'),
  247. '#description' => t('Please type the correct answer to the question.'),
  248. );
  249. break;
  250. }
  251. $form['questions_fieldset']['submit'] = array(
  252. '#type' => 'submit',
  253. '#value' => t('Submit your answer'),
  254. );
  255. }
  256. return $form;
  257. }
  258. /**
  259. * Validation function for ajax_example_dynamic_sections().
  260. */
  261. function ajax_example_dynamic_sections_validate($form, &$form_state) {
  262. $answer = $form_state['values']['question'];
  263. if ($answer !== t('George Washington')) {
  264. form_set_error('question', t('Wrong answer. Try again. (Hint: The right answer is "George Washington".)'));
  265. }
  266. }
  267. /**
  268. * Submit function for ajax_example_dynamic_sections().
  269. */
  270. function ajax_example_dynamic_sections_submit($form, &$form_state) {
  271. // This is only executed when a button is pressed, not when the AJAXified
  272. // select is changed.
  273. // Now handle the case of the next, previous, and submit buttons.
  274. // Only submit will result in actual submission, all others rebuild.
  275. switch ($form_state['triggering_element']['#value']) {
  276. case t('Submit your answer'):
  277. // Submit: We're done.
  278. $form_state['rebuild'] = FALSE;
  279. $answer = $form_state['values']['question'];
  280. // Special handling for the checkbox.
  281. if ($answer == 1 && $form['questions_fieldset']['question']['#type'] == 'checkbox') {
  282. $answer = $form['questions_fieldset']['question']['#title'];
  283. }
  284. if ($answer === t('George Washington')) {
  285. drupal_set_message(t('You got the right answer: @answer', array('@answer' => $answer)));
  286. }
  287. else {
  288. drupal_set_message(t('Sorry, your answer (@answer) is wrong', array('@answer' => $answer)));
  289. }
  290. return;
  291. // Any other form element will cause rebuild of the form and present
  292. // it again.
  293. case t('Choose'):
  294. $form_state['values']['question_type_select'] = $form_state['input']['question_type_select'];
  295. // Fall through.
  296. default:
  297. $form_state['rebuild'] = TRUE;
  298. }
  299. }
  300. /**
  301. * Callback for the select element.
  302. *
  303. * This just selects and returns the questions_fieldset.
  304. */
  305. function ajax_example_dynamic_sections_select_callback($form, $form_state) {
  306. return $form['questions_fieldset'];
  307. }
  308. /**
  309. * Wizard form.
  310. *
  311. * This example is a classic wizard, where a different and sequential form
  312. * is presented on each step of the form.
  313. *
  314. * In the AJAX version, the form is replaced for each wizard section. In the
  315. * multistep version, it causes a new page load.
  316. *
  317. * @param array $form
  318. * Form API form.
  319. * @param array $form_state
  320. * Form API form.
  321. * @param bool $no_js_use
  322. * Used for this demonstration only. If true means that the form should be
  323. * built using a simulated no-javascript approach (ajax.js will not be
  324. * loaded.)
  325. *
  326. * @return array
  327. * Form array.
  328. */
  329. function ajax_example_wizard($form, &$form_state, $no_js_use = FALSE) {
  330. // Provide a wrapper around the entire form, since we'll replace the whole
  331. // thing with each submit.
  332. $form['#prefix'] = '<div id="wizard-form-wrapper">';
  333. $form['#suffix'] = '</div>';
  334. // We want to deal with hierarchical form values.
  335. $form['#tree'] = TRUE;
  336. $form['description'] = array(
  337. '#markup' => '<div>' . t('This example is a step-by-step wizard. The <a href="!ajax">AJAX version</a> does it without page reloads; the <a href="!multistep">multistep version</a> is the same code but simulates a non-javascript environment, showing it with page reloads.',
  338. array('!ajax' => url('examples/ajax_example/wizard'), '!multistep' => url('examples/ajax_example/wizard_no_js')))
  339. . '</div>',
  340. );
  341. // $form_state['storage'] has no specific drupal meaning, but it is
  342. // traditional to keep variables for multistep forms there.
  343. $step = empty($form_state['storage']['step']) ? 1 : $form_state['storage']['step'];
  344. $form_state['storage']['step'] = $step;
  345. switch ($step) {
  346. case 1:
  347. $form['step1'] = array(
  348. '#type' => 'fieldset',
  349. '#title' => t('Step 1: Personal details'),
  350. );
  351. $form['step1']['name'] = array(
  352. '#type' => 'textfield',
  353. '#title' => t('Your name'),
  354. '#default_value' => empty($form_state['values']['step1']['name']) ? '' : $form_state['values']['step1']['name'],
  355. '#required' => TRUE,
  356. );
  357. break;
  358. case 2:
  359. $form['step2'] = array(
  360. '#type' => 'fieldset',
  361. '#title' => t('Step 2: Street address info'),
  362. );
  363. $form['step2']['address'] = array(
  364. '#type' => 'textfield',
  365. '#title' => t('Your street address'),
  366. '#default_value' => empty($form_state['values']['step2']['address']) ? '' : $form_state['values']['step2']['address'],
  367. '#required' => TRUE,
  368. );
  369. break;
  370. case 3:
  371. $form['step3'] = array(
  372. '#type' => 'fieldset',
  373. '#title' => t('Step 3: City info'),
  374. );
  375. $form['step3']['city'] = array(
  376. '#type' => 'textfield',
  377. '#title' => t('Your city'),
  378. '#default_value' => empty($form_state['values']['step3']['city']) ? '' : $form_state['values']['step3']['city'],
  379. '#required' => TRUE,
  380. );
  381. break;
  382. }
  383. if ($step == 3) {
  384. $form['submit'] = array(
  385. '#type' => 'submit',
  386. '#value' => t("Submit your information"),
  387. );
  388. }
  389. if ($step < 3) {
  390. $form['next'] = array(
  391. '#type' => 'submit',
  392. '#value' => t('Next step'),
  393. '#ajax' => array(
  394. 'wrapper' => 'wizard-form-wrapper',
  395. 'callback' => 'ajax_example_wizard_callback',
  396. ),
  397. );
  398. }
  399. if ($step > 1) {
  400. $form['prev'] = array(
  401. '#type' => 'submit',
  402. '#value' => t("Previous step"),
  403. // Since all info will be discarded, don't validate on 'prev'.
  404. '#limit_validation_errors' => array(),
  405. // #submit is required to use #limit_validation_errors
  406. '#submit' => array('ajax_example_wizard_submit'),
  407. '#ajax' => array(
  408. 'wrapper' => 'wizard-form-wrapper',
  409. 'callback' => 'ajax_example_wizard_callback',
  410. ),
  411. );
  412. }
  413. // This simply allows us to demonstrate no-javascript use without
  414. // actually turning off javascript in the browser. Removing the #ajax
  415. // element turns off AJAX behaviors on that element and as a result
  416. // ajax.js doesn't get loaded.
  417. // For demonstration only! You don't need this.
  418. if ($no_js_use) {
  419. // Remove the #ajax from the above, so ajax.js won't be loaded.
  420. // For demonstration only.
  421. unset($form['next']['#ajax']);
  422. unset($form['prev']['#ajax']);
  423. }
  424. return $form;
  425. }
  426. /**
  427. * Wizard callback function.
  428. *
  429. * @param array $form
  430. * Form API form.
  431. * @param array $form_state
  432. * Form API form.
  433. *
  434. * @return array
  435. * Form array.
  436. */
  437. function ajax_example_wizard_callback($form, $form_state) {
  438. return $form;
  439. }
  440. /**
  441. * Submit function for ajax_example_wizard.
  442. *
  443. * In AJAX this is only submitted when the final submit button is clicked,
  444. * but in the non-javascript situation, it is submitted with every
  445. * button click.
  446. */
  447. function ajax_example_wizard_submit($form, &$form_state) {
  448. // Save away the current information.
  449. $current_step = 'step' . $form_state['storage']['step'];
  450. if (!empty($form_state['values'][$current_step])) {
  451. $form_state['storage']['values'][$current_step] = $form_state['values'][$current_step];
  452. }
  453. // Increment or decrement the step as needed. Recover values if they exist.
  454. if ($form_state['triggering_element']['#value'] == t('Next step')) {
  455. $form_state['storage']['step']++;
  456. // If values have already been entered for this step, recover them from
  457. // $form_state['storage'] to pre-populate them.
  458. $step_name = 'step' . $form_state['storage']['step'];
  459. if (!empty($form_state['storage']['values'][$step_name])) {
  460. $form_state['values'][$step_name] = $form_state['storage']['values'][$step_name];
  461. }
  462. }
  463. if ($form_state['triggering_element']['#value'] == t('Previous step')) {
  464. $form_state['storage']['step']--;
  465. // Recover our values from $form_state['storage'] to pre-populate them.
  466. $step_name = 'step' . $form_state['storage']['step'];
  467. $form_state['values'][$step_name] = $form_state['storage']['values'][$step_name];
  468. }
  469. // If they're done, submit.
  470. if ($form_state['triggering_element']['#value'] == t('Submit your information')) {
  471. $value_message = t('Your information has been submitted:') . ' ';
  472. foreach ($form_state['storage']['values'] as $step => $values) {
  473. $value_message .= "$step: ";
  474. foreach ($values as $key => $value) {
  475. $value_message .= "$key=$value, ";
  476. }
  477. }
  478. drupal_set_message($value_message);
  479. $form_state['rebuild'] = FALSE;
  480. return;
  481. }
  482. // Otherwise, we still have work to do.
  483. $form_state['rebuild'] = TRUE;
  484. }
  485. /**
  486. * Form with 'add more' and 'remove' buttons.
  487. *
  488. * This example shows a button to "add more" - add another textfield, and
  489. * the corresponding "remove" button.
  490. *
  491. * It works equivalently with javascript or not, and does the same basic steps
  492. * either way.
  493. *
  494. * The basic idea is that we build the form based on the setting of
  495. * $form_state['num_names']. The custom submit functions for the "add-one"
  496. * and "remove-one" buttons increment and decrement $form_state['num_names']
  497. * and then force a rebuild of the form.
  498. *
  499. * The $no_js_use argument is simply for demonstration: When set, it prevents
  500. * '#ajax' from being set, thus making the example behave as if javascript
  501. * were disabled in the browser.
  502. */
  503. function ajax_example_add_more($form, &$form_state, $no_js_use = FALSE) {
  504. $form['description'] = array(
  505. '#markup' => '<div>' . t('This example shows an add-more and a remove-last button. The <a href="!ajax">AJAX version</a> does it without page reloads; the <a href="!multistep">non-js version</a> is the same code but simulates a non-javascript environment, showing it with page reloads.',
  506. array('!ajax' => url('examples/ajax_example/add_more'), '!multistep' => url('examples/ajax_example/add_more_no_js')))
  507. . '</div>',
  508. );
  509. // Because we have many fields with the same values, we have to set
  510. // #tree to be able to access them.
  511. $form['#tree'] = TRUE;
  512. $form['names_fieldset'] = array(
  513. '#type' => 'fieldset',
  514. '#title' => t('People coming to the picnic'),
  515. // Set up the wrapper so that AJAX will be able to replace the fieldset.
  516. '#prefix' => '<div id="names-fieldset-wrapper">',
  517. '#suffix' => '</div>',
  518. );
  519. // Build the fieldset with the proper number of names. We'll use
  520. // $form_state['num_names'] to determine the number of textfields to build.
  521. if (empty($form_state['num_names'])) {
  522. $form_state['num_names'] = 1;
  523. }
  524. for ($i = 0; $i < $form_state['num_names']; $i++) {
  525. $form['names_fieldset']['name'][$i] = array(
  526. '#type' => 'textfield',
  527. '#title' => t('Name'),
  528. );
  529. }
  530. $form['names_fieldset']['add_name'] = array(
  531. '#type' => 'submit',
  532. '#value' => t('Add one more'),
  533. '#submit' => array('ajax_example_add_more_add_one'),
  534. // See the examples in ajax_example.module for more details on the
  535. // properties of #ajax.
  536. '#ajax' => array(
  537. 'callback' => 'ajax_example_add_more_callback',
  538. 'wrapper' => 'names-fieldset-wrapper',
  539. ),
  540. );
  541. if ($form_state['num_names'] > 1) {
  542. $form['names_fieldset']['remove_name'] = array(
  543. '#type' => 'submit',
  544. '#value' => t('Remove one'),
  545. '#submit' => array('ajax_example_add_more_remove_one'),
  546. '#ajax' => array(
  547. 'callback' => 'ajax_example_add_more_callback',
  548. 'wrapper' => 'names-fieldset-wrapper',
  549. ),
  550. );
  551. }
  552. $form['submit'] = array(
  553. '#type' => 'submit',
  554. '#value' => t('Submit'),
  555. );
  556. // This simply allows us to demonstrate no-javascript use without
  557. // actually turning off javascript in the browser. Removing the #ajax
  558. // element turns off AJAX behaviors on that element and as a result
  559. // ajax.js doesn't get loaded.
  560. // For demonstration only! You don't need this.
  561. if ($no_js_use) {
  562. // Remove the #ajax from the above, so ajax.js won't be loaded.
  563. if (!empty($form['names_fieldset']['remove_name']['#ajax'])) {
  564. unset($form['names_fieldset']['remove_name']['#ajax']);
  565. }
  566. unset($form['names_fieldset']['add_name']['#ajax']);
  567. }
  568. return $form;
  569. }
  570. /**
  571. * Callback for both ajax-enabled buttons.
  572. *
  573. * Selects and returns the fieldset with the names in it.
  574. */
  575. function ajax_example_add_more_callback($form, $form_state) {
  576. return $form['names_fieldset'];
  577. }
  578. /**
  579. * Submit handler for the "add-one-more" button.
  580. *
  581. * Increments the max counter and causes a rebuild.
  582. */
  583. function ajax_example_add_more_add_one($form, &$form_state) {
  584. $form_state['num_names']++;
  585. $form_state['rebuild'] = TRUE;
  586. }
  587. /**
  588. * Submit handler for the "remove one" button.
  589. *
  590. * Decrements the max counter and causes a form rebuild.
  591. */
  592. function ajax_example_add_more_remove_one($form, &$form_state) {
  593. if ($form_state['num_names'] > 1) {
  594. $form_state['num_names']--;
  595. }
  596. $form_state['rebuild'] = TRUE;
  597. }
  598. /**
  599. * Final submit handler.
  600. *
  601. * Reports what values were finally set.
  602. */
  603. function ajax_example_add_more_submit($form, &$form_state) {
  604. $output = t('These people are coming to the picnic: @names',
  605. array(
  606. '@names' => implode(', ', $form_state['values']['names_fieldset']['name']),
  607. )
  608. );
  609. drupal_set_message($output);
  610. }
  611. /**
  612. * @} End of "defgroup ajax_degradation_example".
  613. */