form_test.module 51.6 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

/**
 * @file
 * Helper module for the form API tests.
 */

/**
9
 * Implements hook_menu().
10 11
 */
function form_test_menu() {
12 13 14 15 16 17 18
  $items['form-test/alter'] = array(
    'title' => 'Form altering test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_alter_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
19 20 21 22 23 24 25
  $items['form-test/validate'] = array(
    'title' => 'Form validation handlers test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_validate_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
26 27 28 29 30 31 32
  $items['form-test/limit-validation-errors'] = array(
    'title' => 'Form validation with some error suppression',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_limit_validation_errors_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  $items['form_test/tableselect/multiple-true'] = array(
    'title' => 'Tableselect checkboxes test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_form_test_tableselect_multiple_true_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['form_test/tableselect/multiple-false'] = array(
    'title' => 'Tableselect radio button test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_form_test_tableselect_multiple_false_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['form_test/tableselect/empty-text'] = array(
    'title' => 'Tableselect empty text test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_form_test_tableselect_empty_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['form_test/tableselect/advanced-select'] = array(
    'title' => 'Tableselect js_select tests',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_form_test_tableselect_js_select_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

63 64 65 66 67 68 69 70
  $items['form_test/vertical-tabs'] = array(
    'title' => 'Vertical tabs tests',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_form_test_vertical_tabs_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

71 72 73
  $items['form_test/form-storage'] = array(
    'title' => 'Form storage test',
    'page callback' => 'drupal_get_form',
74
    'page arguments' => array('form_test_storage_form'),
75 76 77 78
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

79 80 81 82 83 84 85 86
  $items['form_test/wrapper-callback'] = array(
    'title' => 'Form wrapper callback test',
    'page callback' => 'form_test_wrapper_callback',
    'page arguments' => array('form_test_wrapper_callback_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

87 88 89 90 91 92 93 94
  $items['form_test/form-state-values-clean'] = array(
    'title' => 'Form state values clearance test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_form_state_values_clean_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

95 96 97 98 99 100 101 102
  $items['form_test/form-state-values-clean-advanced'] = array(
    'title' => 'Form state values clearance advanced test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_form_state_values_clean_advanced_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

103 104 105 106 107 108 109
  $items['form-test/checkbox'] = array(
    'title' => t('Form test'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_form_test_checkbox'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
110 111 112 113 114 115
  $items['form-test/select'] = array(
    'title' => t('Select'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_select'),
    'access callback' => TRUE,
  );
116 117 118 119 120 121
  $items['form-test/placeholder-text'] = array(
    'title' => 'Placeholder',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_placeholder_test'),
    'access callback' => TRUE,
  );
122 123 124 125 126 127
  $items['form-test/checkboxes-radios'] = array(
    'title' => t('Checkboxes, Radios'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_checkboxes_radios'),
    'access callback' => TRUE,
  );
128 129 130 131 132 133
  $items['form-test/email'] = array(
    'title' => 'E-Mail fields',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_email'),
    'access callback' => TRUE,
  );
134

135 136 137 138 139 140 141 142
  $items['form-test/disabled-elements'] = array(
    'title' => t('Form test'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_form_test_disabled_elements'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

143 144 145 146 147 148 149 150
  $items['form-test/input-forgery'] = array(
    'title' => t('Form test'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_form_test_input_forgery'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

151 152 153 154 155 156 157 158
  $items['form-test/form-rebuild-preserve-values'] = array(
    'title' => 'Form values preservation during rebuild test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_form_rebuild_preserve_values_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

159 160 161 162 163 164 165 166
  $items['form_test/form-labels'] = array(
    'title' => 'Form label test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_label_test_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

167 168 169 170 171 172 173 174
  $items['form-test/state-persist'] = array(
    'title' => 'Form state persistence without storage',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_state_persist'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

175 176 177 178 179 180 181 182
  $items['form-test/clicked-button'] = array(
    'title' => 'Clicked button test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_clicked_button'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

183 184 185 186 187 188 189 190 191 192 193 194
  if (module_exists('node')) {
    $items['form-test/two-instances-of-same-form'] = array(
      'title' => 'AJAX test with two form instances',
      'page callback' => 'form_test_two_instances',
      'access callback' => 'node_access',
      'access arguments' => array('create', 'page'),
      'file path' => drupal_get_path('module', 'node'),
      'file' => 'node.pages.inc',
      'type' => MENU_CALLBACK,
    );
  }

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  $items['form-test/load-include-menu'] = array(
    'title' => 'FAPI test loading includes',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_load_include_menu'),
    'access callback' => TRUE,
    'file' => 'form_test.file.inc',
    'type' => MENU_CALLBACK,
  );

  $items['form-test/load-include-custom'] = array(
    'title' => 'FAPI test loading includes',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_load_include_custom'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
211 212 213 214 215 216 217
  $items['form-test/checkboxes-zero'] = array(
    'title' => 'FAPI test involving checkboxes and zero',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_checkboxes_zero'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
218

219 220 221
  return $items;
}

222 223 224 225 226 227 228 229
/**
 * Form submit handler to return form values as JSON.
 */
function _form_test_submit_values_json($form, &$form_state) {
  drupal_json_output($form_state['values']);
  drupal_exit();
}

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
/**
 * Form builder for testing hook_form_alter() and hook_form_FORM_ID_alter().
 */
function form_test_alter_form($form, &$form_state) {
  // Elements can be added as needed for future testing needs, but for now,
  // we're only testing alter hooks that do not require any elements added by
  // this function.
  return $form;
}

/**
 * Implements hook_form_FORM_ID_alter() on behalf of block.module.
 */
function block_form_form_test_alter_form_alter(&$form, &$form_state) {
  drupal_set_message('block_form_form_test_alter_form_alter() executed.');
}

/**
 * Implements hook_form_alter().
 */
function form_test_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'form_test_alter_form') {
    drupal_set_message('form_test_form_alter() executed.');
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function form_test_form_form_test_alter_form_alter(&$form, &$form_state) {
  drupal_set_message('form_test_form_form_test_alter_form_alter() executed.');
}

/**
 * Implements hook_form_FORM_ID_alter() on behalf of system.module.
 */
function system_form_form_test_alter_form_alter(&$form, &$form_state) {
  drupal_set_message('system_form_form_test_alter_form_alter() executed.');
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
/**
 * Form builder for testing drupal_validate_form().
 *
 * Serves for testing form processing and alterations by form validation
 * handlers, especially for the case of a validation error:
 * - form_set_value() should be able to alter submitted values in
 *   $form_state['values'] without affecting the form element.
 * - #element_validate handlers should be able to alter the $element in the form
 *   structure and the alterations should be contained in the rebuilt form.
 * - #validate handlers should be able to alter the $form and the alterations
 *   should be contained in the rebuilt form.
 */
function form_test_validate_form($form, &$form_state) {
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => 'Name',
    '#default_value' => '',
    '#element_validate' => array('form_test_element_validate_name'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Save',
  );
293 294 295 296 297

  // To simplify this test, enable form caching and use form storage to
  // remember our alteration.
  $form_state['cache'] = TRUE;

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  return $form;
}

/**
 * Form element validation handler for 'name' in form_test_validate_form().
 */
function form_test_element_validate_name(&$element, &$form_state) {
  $triggered = FALSE;
  if ($form_state['values']['name'] == 'element_validate') {
    // Alter the form element.
    $element['#value'] = '#value changed by #element_validate';
    // Alter the submitted value in $form_state.
    form_set_value($element, 'value changed by form_set_value() in #element_validate', $form_state);

    $triggered = TRUE;
  }
  if ($form_state['values']['name'] == 'element_validate_access') {
    $form_state['storage']['form_test_name'] = $form_state['values']['name'];
    // Alter the form element.
    $element['#access'] = FALSE;

    $triggered = TRUE;
  }
  elseif (!empty($form_state['storage']['form_test_name'])) {
    // To simplify this test, just take over the element's value into $form_state.
    form_set_value($element, $form_state['storage']['form_test_name'], $form_state);

    $triggered = TRUE;
  }

  if ($triggered) {
    // Output the element's value from $form_state.
    drupal_set_message(t('@label value: @value', array('@label' => $element['#title'], '@value' => $form_state['values']['name'])));

    // Trigger a form validation error to see our changes.
    form_set_error('');
  }
}

/**
 * Form validation handler for form_test_validate_form().
 */
function form_test_validate_form_validate(&$form, &$form_state) {
  if ($form_state['values']['name'] == 'validate') {
    // Alter the form element.
    $form['name']['#value'] = '#value changed by #validate';
    // Alter the submitted value in $form_state.
    form_set_value($form['name'], 'value changed by form_set_value() in #validate', $form_state);
    // Output the element's value from $form_state.
    drupal_set_message(t('@label value: @value', array('@label' => $form['name']['#title'], '@value' => $form_state['values']['name'])));

    // Trigger a form validation error to see our changes.
    form_set_error('');
  }
}

354 355 356 357 358 359 360 361 362
/**
 * Builds a simple form with a button triggering partial validation.
 */
function form_test_limit_validation_errors_form($form, &$form_state) {
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => 'Title',
    '#required' => TRUE,
  );
363

364
  $form['test'] = array(
365 366 367 368 369 370 371 372 373 374 375 376
    '#title' => 'Test',
    '#type' => 'textfield',
    '#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
  );
  $form['test_numeric_index'] = array(
    '#tree' => TRUE,
  );
  $form['test_numeric_index'][0] = array(
    '#title' => 'Test (numeric index)',
    '#type' => 'textfield',
    '#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
  );
377

378 379 380 381 382
  $form['test_substring'] = array(
    '#tree' => TRUE,
  );
  $form['test_substring']['foo'] = array(
    '#title' => 'Test (substring) foo',
383 384 385
    '#type' => 'textfield',
    '#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
  );
386 387 388 389 390
  $form['test_substring']['foobar'] = array(
    '#title' => 'Test (substring) foobar',
    '#type' => 'textfield',
    '#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
  );
391

392 393 394
  $form['actions']['partial'] = array(
    '#type' => 'submit',
    '#limit_validation_errors' => array(array('test')),
395
    '#submit' => array('form_test_limit_validation_errors_form_partial_submit'),
396 397
    '#value' => t('Partial validate'),
  );
398 399 400 401 402 403 404 405 406 407 408 409
  $form['actions']['partial_numeric_index'] = array(
    '#type' => 'submit',
    '#limit_validation_errors' => array(array('test_numeric_index', 0)),
    '#submit' => array('form_test_limit_validation_errors_form_partial_submit'),
    '#value' => t('Partial validate (numeric index)'),
  );
  $form['actions']['substring'] = array(
    '#type' => 'submit',
    '#limit_validation_errors' => array(array('test_substring', 'foo')),
    '#submit' => array('form_test_limit_validation_errors_form_partial_submit'),
    '#value' => t('Partial validate (substring)'),
  );
410 411 412 413 414 415 416 417 418 419 420 421
  $form['actions']['full'] = array(
    '#type' => 'submit',
    '#value' => t('Full validate'),
  );
  return $form;
}

/**
 * Form element validation handler for the 'test' element.
 */
function form_test_limit_validation_errors_element_validate_test(&$element, &$form_state) {
  if ($element['#value'] == 'invalid') {
422
    form_error($element, t('@label element is invalid', array('@label' => $element['#title'])));
423 424 425
  }
}

426 427 428 429 430 431 432 433 434 435 436
/**
 * Form submit handler for the partial validation submit button.
 */
function form_test_limit_validation_errors_form_partial_submit($form, $form_state) {
  // The title has not been validated, thus its value - in case of the test case
  // an empty string - may not be set.
  if (!isset($form_state['values']['title']) && isset($form_state['values']['test'])) {
    drupal_set_message('Only validated values appear in the form values.');
  }
}

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
/**
 * Create a header and options array. Helper function for callbacks.
 */
function _form_test_tableselect_get_data() {
  $header = array(
    'one' => t('One'),
    'two' => t('Two'),
    'three' => t('Three'),
    'four' => t('Four'),
  );

  $options['row1'] = array(
    'one' => 'row1col1',
    'two' => t('row1col2'),
    'three' => t('row1col3'),
    'four' => t('row1col4'),
  );

  $options['row2'] = array(
    'one' => 'row2col1',
    'two' => t('row2col2'),
    'three' => t('row2col3'),
    'four' => t('row2col4'),
  );

  $options['row3'] = array(
    'one' => 'row3col1',
    'two' => t('row3col2'),
    'three' => t('row3col3'),
    'four' => t('row3col4'),
  );

  return array($header, $options);
}

/**
 * Build a form to test the tableselect element.
 *
 * @param $form_state
 *   The form_state
 * @param $element_properties
 *   An array of element properties for the tableselect element.
 *
 * @return
 *   A form with a tableselect element and a submit button.
 */
483
function _form_test_tableselect_form_builder($form, $form_state, $element_properties) {
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  list($header, $options) = _form_test_tableselect_get_data();

  $form['tableselect'] = $element_properties;

  $form['tableselect'] += array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#multiple' => FALSE,
    '#empty' => t('Empty text.'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;
}

/**
 * Test the tableselect #multiple = TRUE functionality.
 */
507 508
function _form_test_tableselect_multiple_true_form($form, $form_state) {
  return _form_test_tableselect_form_builder($form, $form_state, array('#multiple' => TRUE));
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
}

/**
 * Process the tableselect #multiple = TRUE submitted values.
 */
function _form_test_tableselect_multiple_true_form_submit($form, &$form_state) {
  $selected = $form_state['values']['tableselect'];
  foreach ($selected as $key => $value) {
    drupal_set_message(t('Submitted: @key = @value', array('@key' => $key, '@value' => $value)));
  }
}

/**
 * Test the tableselect #multiple = FALSE functionality.
 */
524 525
function _form_test_tableselect_multiple_false_form($form, $form_state) {
  return _form_test_tableselect_form_builder($form, $form_state, array('#multiple' => FALSE));
526 527 528 529 530 531 532 533 534 535 536 537
}

/**
 * Process the tableselect #multiple = FALSE submitted values.
 */
function _form_test_tableselect_multiple_false_form_submit($form, &$form_state) {
  drupal_set_message(t('Submitted: @value', array('@value' => $form_state['values']['tableselect'])));
}

/**
 * Test functionality of the tableselect #empty property.
 */
538 539
function _form_test_tableselect_empty_form($form, $form_state) {
  return _form_test_tableselect_form_builder($form, $form_state, array('#options' => array()));
540 541 542 543 544
}

/**
 * Test functionality of the tableselect #js_select property.
 */
545
function _form_test_tableselect_js_select_form($form, $form_state, $action) {
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
  switch ($action) {
    case 'multiple-true-default':
      $options = array('#multiple' => TRUE);
      break;

    case 'multiple-false-default':
      $options = array('#multiple' => FALSE);
      break;

    case 'multiple-true-no-advanced-select':
      $options = array('#multiple' => TRUE, '#js_select' => FALSE);
      break;

    case 'multiple-false-advanced-select':
      $options = array('#multiple' => FALSE, '#js_select' => TRUE);
      break;
  }

564
  return _form_test_tableselect_form_builder($form, $form_state, $options);
565
}
566

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
/**
 * Tests functionality of vertical tabs.
 */
function _form_test_vertical_tabs_form($form, &$form_state) {
  $form['vertical_tabs'] = array(
    '#type' => 'vertical_tabs',
  );
  $form['tab1'] = array(
    '#type' => 'fieldset',
    '#title' => t('Tab 1'),
    '#collapsible' => TRUE,
    '#group' => 'vertical_tabs',
  );
  $form['tab1']['field1'] = array(
    '#title' => t('Field 1'),
    '#type' => 'textfield',
  );
  $form['tab2'] = array(
    '#type' => 'fieldset',
    '#title' => t('Tab 2'),
    '#collapsible' => TRUE,
    '#group' => 'vertical_tabs',
  );
  $form['tab2']['field2'] = array(
    '#title' => t('Field 2'),
    '#type' => 'textfield',
  );
  return $form;
}

597 598 599 600 601 602
/**
 * A multistep form for testing the form storage.
 *
 * It uses two steps for editing a virtual "thing". Any changes to it are saved
 * in the form storage and have to be present during any step. By setting the
 * request parameter "cache" the form can be tested with caching enabled, as
603
 * it would be the case, if the form would contain some #ajax callbacks.
604
 *
605
 * @see form_test_storage_form_submit()
606
 */
607 608 609 610
function form_test_storage_form($form, &$form_state) {
  if ($form_state['rebuild']) {
    $form_state['input'] = array();
  }
611
  // Initialize
612
  if (empty($form_state['storage'])) {
613 614 615 616 617 618 619 620 621 622 623
    if (empty($form_state['input'])) {
      $_SESSION['constructions'] = 0;
    }
    // Put the initial thing into the storage
    $form_state['storage'] = array(
      'thing' => array(
        'title' => 'none',
        'value' => '',
      ),
    );
  }
624
  // Count how often the form is constructed.
625
  $_SESSION['constructions']++;
626
  drupal_set_message("Form constructions: " . $_SESSION['constructions']);
627

628 629 630 631 632 633 634 635 636 637 638 639
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => 'Title',
    '#default_value' => $form_state['storage']['thing']['title'],
    '#required' => TRUE,
  );
  $form['value'] = array(
    '#type' => 'textfield',
    '#title' => 'Value',
    '#default_value' => $form_state['storage']['thing']['value'],
    '#element_validate' => array('form_test_storage_element_validate_value_cached'),
  );
640 641 642 643 644 645 646 647 648 649 650 651 652 653
  $form['continue_button'] = array(
    '#type' => 'button',
    '#value' => 'Reset',
    // Rebuilds the form without keeping the values.
  );
  $form['continue_submit'] = array(
    '#type' => 'submit',
    '#value' => 'Continue submit',
    '#submit' => array('form_storage_test_form_continue_submit'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Save',
  );
654 655 656 657

  if (isset($_REQUEST['cache'])) {
    // Manually activate caching, so we can test that the storage keeps working
    // when it's enabled.
658
    $form_state['cache'] = TRUE;
659 660 661 662 663 664
  }

  return $form;
}

/**
665 666 667
 * Form element validation handler for 'value' element in form_test_storage_form().
 *
 * Tests updating of cached form storage during validation.
668
 */
669
function form_test_storage_element_validate_value_cached($element, &$form_state) {
670 671 672 673
  // If caching is enabled and we receive a certain value, change the storage.
  // This presumes that another submitted form value triggers a validation error
  // elsewhere in the form. Form API should still update the cached form storage
  // though.
674
  if (isset($_REQUEST['cache']) && $form_state['values']['value'] == 'change_title') {
675
    $form_state['storage']['thing']['changed'] = TRUE;
676 677 678 679
  }
}

/**
680
 * Form submit handler to continue multi-step form.
681
 */
682 683 684 685 686 687 688 689 690 691
function form_storage_test_form_continue_submit($form, &$form_state) {
  $form_state['storage']['thing']['title'] = $form_state['values']['title'];
  $form_state['storage']['thing']['value'] = $form_state['values']['value'];
  $form_state['rebuild'] = TRUE;
}

/**
 * Form submit handler to finish multi-step form.
 */
function form_test_storage_form_submit($form, &$form_state) {
692 693
  drupal_set_message("Title: " . check_plain($form_state['values']['title']));
  drupal_set_message("Form constructions: " . $_SESSION['constructions']);
694 695 696 697
  if (isset($form_state['storage']['thing']['changed'])) {
    drupal_set_message("The thing has been changed.");
  }
  $form_state['redirect'] = 'node';
698
}
699

700
/**
701 702
 * A form for testing form labels and required marks.
 */
703
function form_label_test_form() {
704 705 706 707 708 709 710
  $form['form_checkboxes_test'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Checkboxes test'),
    '#options' => array(
      'first-checkbox' => t('First checkbox'),
      'second-checkbox' => t('Second checkbox'),
      'third-checkbox' => t('Third checkbox'),
711
      '0' => t('0'),
712 713 714 715 716 717 718 719 720
    ),
  );
  $form['form_radios_test'] = array(
    '#type' => 'radios',
    '#title' => t('Radios test'),
    '#options' => array(
      'first-radio' => t('First radio'),
      'second-radio' => t('Second radio'),
      'third-radio' => t('Third radio'),
721
      '0' => t('0'),
722
    ),
723 724 725
    // Test #field_prefix and #field_suffix placement.
    '#field_prefix' => '<span id="form-test-radios-field-prefix">' . t('Radios #field_prefix element') . '</span>',
    '#field_suffix' => '<span id="form-test-radios-field-suffix">' . t('Radios #field_suffix element') . '</span>',
726 727 728 729 730 731 732 733 734 735 736 737
  );
  $form['form_checkbox_test'] = array(
    '#type' => 'checkbox',
    '#title' => t('Checkbox test'),
  );
  $form['form_textfield_test_title_and_required'] = array(
    '#type' => 'textfield',
    '#title' => t('Textfield test for required with title'),
    '#required' => TRUE,
  );
  $form['form_textfield_test_no_title_required'] = array(
    '#type' => 'textfield',
738
    // We use an empty title, since not setting #title suppresses the label
739 740 741 742 743 744 745 746
    // and required marker.
    '#title' => '',
    '#required' => TRUE,
  );
  $form['form_textfield_test_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Textfield test for title only'),
    // Not required.
747 748 749
    // Test #prefix and #suffix placement.
    '#prefix' => '<div id="form-test-textfield-title-prefix">' . t('Textfield #prefix element') . '</div>',
    '#suffix' => '<div id="form-test-textfield-title-suffix">' . t('Textfield #suffix element') . '</div>',
750 751 752 753 754 755
  );
  $form['form_textfield_test_title_after'] = array(
    '#type' => 'textfield',
    '#title' => t('Textfield test for title after element'),
    '#title_display' => 'after',
  );
756 757 758 759 760
  $form['form_textfield_test_title_invisible'] = array(
    '#type' => 'textfield',
    '#title' => t('Textfield test for invisible title'),
    '#title_display' => 'invisible',
  );
761 762 763 764 765 766 767 768
  // Textfield test for title set not to display
  $form['form_textfield_test_title_no_show'] = array(
    '#type' => 'textfield',
  );

  return $form;
}

769 770 771 772 773
/**
 * Menu callback; Invokes a form builder function with a wrapper callback.
 */
function form_test_wrapper_callback($form_id) {
  $form_state = array(
774
    'build_info' => array('args' => array()),
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
    'wrapper_callback' => 'form_test_wrapper_callback_wrapper',
  );
  return drupal_build_form($form_id, $form_state);
}

/**
 * Form wrapper for form_test_wrapper_callback_form().
 */
function form_test_wrapper_callback_wrapper($form, &$form_state) {
  $form['wrapper'] = array('#markup' => 'Form wrapper callback element output.');
  return $form;
}

/**
 * Form builder for form wrapper callback test.
 */
function form_test_wrapper_callback_form($form, &$form_state) {
  $form['builder'] = array('#markup' => 'Form builder element output.');
  return $form;
}

796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
/**
 * Form builder for form_state_values_clean() test.
 */
function form_test_form_state_values_clean_form($form, &$form_state) {
  // Build an example form containing multiple submit and button elements; not
  // only on the top-level.
  $form = array('#tree' => TRUE);
  $form['foo'] = array('#type' => 'submit', '#value' => t('Submit'));
  $form['bar'] = array('#type' => 'submit', '#value' => t('Submit'));
  $form['beer'] = array('#type' => 'value', '#value' => 1000);
  $form['baz']['foo'] = array('#type' => 'button', '#value' => t('Submit'));
  $form['baz']['baz'] = array('#type' => 'submit', '#value' => t('Submit'));
  $form['baz']['beer'] = array('#type' => 'value', '#value' => 2000);
  return $form;
}

/**
 * Form submit handler for form_state_values_clean() test form.
 */
function form_test_form_state_values_clean_form_submit($form, &$form_state) {
  form_state_values_clean($form_state);
  drupal_json_output($form_state['values']);
  exit;
}
820

821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
/**
 * Form constructor for the form_state_values_clean() test.
 */
function form_test_form_state_values_clean_advanced_form($form, &$form_state) {
  // Build an example form containing a managed file and a submit form element.
  $form['image'] = array(
    '#type' => 'managed_file',
    '#title' => t('Image'),
    '#upload_location' => 'public://',
    '#default_value' => 0,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

/**
 * Form submission handler for form_test_form_state_values_clean_advanced_form().
 */
function form_test_form_state_values_clean_advanced_form_submit($form, &$form_state) {
  form_state_values_clean($form_state);
  print t('You WIN!');
  exit;
}

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
/**
 * Build a form to test a checkbox.
 */
function _form_test_checkbox($form, &$form_state) {
  // A required checkbox.
  $form['required_checkbox'] = array(
    '#type' => 'checkbox',
    '#required' => TRUE,
    '#title' => 'required_checkbox',
  );

  // A disabled checkbox should get its default value back.
  $form['disabled_checkbox_on'] = array(
    '#type' => 'checkbox',
    '#disabled' => TRUE,
    '#return_value' => 'disabled_checkbox_on',
    '#default_value' => 'disabled_checkbox_on',
    '#title' => 'disabled_checkbox_on',
  );
  $form['disabled_checkbox_off'] = array(
    '#type' => 'checkbox',
    '#disabled' => TRUE,
    '#return_value' => 'disabled_checkbox_off',
    '#default_value' => NULL,
    '#title' => 'disabled_checkbox_off',
  );

  // A checkbox is active when #default_value == #return_value.
  $form['checkbox_on'] = array(
    '#type' => 'checkbox',
    '#return_value' => 'checkbox_on',
    '#default_value' => 'checkbox_on',
    '#title' => 'checkbox_on',
  );

  // But inactive in any other case.
  $form['checkbox_off'] = array(
    '#type' => 'checkbox',
    '#return_value' => 'checkbox_off',
    '#default_value' => 'checkbox_on',
    '#title' => 'checkbox_off',
  );

  // Checkboxes with a #return_value of '0' are supported.
  $form['zero_checkbox_on'] = array(
    '#type' => 'checkbox',
    '#return_value' => '0',
    '#default_value' => '0',
    '#title' => 'zero_checkbox_on',
  );

  // In that case, passing a #default_value != '0' means that the checkbox is off.
  $form['zero_checkbox_off'] = array(
    '#type' => 'checkbox',
    '#return_value' => '0',
    '#default_value' => '1',
    '#title' => 'zero_checkbox_off',
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit')
  );

  return $form;
}

/**
 * Return the form values via JSON.
 */
function _form_test_checkbox_submit($form, &$form_state) {
  drupal_json_output($form_state['values']);
  exit();
}
922

923 924 925 926 927 928 929 930 931 932 933 934 935
/**
 * Builds a form to test #type 'select' validation.
 */
function form_test_select($form, &$form_state) {
  $base = array(
    '#type' => 'select',
    '#options' => drupal_map_assoc(array('one', 'two', 'three')),
  );

  $form['select'] = $base + array(
    '#title' => '#default_value one',
    '#default_value' => 'one',
  );
936 937 938 939 940 941 942 943 944 945
  $form['select_required'] = $base + array(
    '#title' => '#default_value one, #required',
    '#required' => TRUE,
    '#default_value' => 'one',
  );
  $form['select_optional'] = $base + array(
    '#title' => '#default_value one, not #required',
    '#required' => FALSE,
    '#default_value' => 'one',
  );
946
  $form['empty_value'] = $base + array(
947 948
    '#title' => '#default_value one, #required, #empty_value 0',
    '#required' => TRUE,
949 950 951 952
    '#default_value' => 'one',
    '#empty_value' => 0,
  );
  $form['empty_value_one'] = $base + array(
953 954
    '#title' => '#default_value = #empty_value, #required',
    '#required' => TRUE,
955 956 957 958 959
    '#default_value' => 'one',
    '#empty_value' => 'one',
  );

  $form['no_default'] = $base + array(
960 961
    '#title' => 'No #default_value, #required',
    '#required' => TRUE,
962 963 964 965
  );
  $form['no_default_optional'] = $base + array(
    '#title' => 'No #default_value, not #required',
    '#required' => FALSE,
966
    '#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.',
967 968 969 970 971
  );
  $form['no_default_optional_empty_value'] = $base + array(
    '#title' => 'No #default_value, not #required, #empty_value empty string',
    '#empty_value' => '',
    '#required' => FALSE,
972
    '#description' => 'Should result in an empty string (due to #empty_value) because it is optional.',
973 974 975
  );

  $form['no_default_empty_option'] = $base + array(
976 977
    '#title' => 'No #default_value, #required, #empty_option',
    '#required' => TRUE,
978 979 980 981 982
    '#empty_option' => '- Choose -',
  );
  $form['no_default_empty_option_optional'] = $base + array(
    '#title' => 'No #default_value, not #required, #empty_option',
    '#empty_option' => '- Dismiss -',
983
    '#description' => 'Should result in an empty string (default of #empty_value) because it is optional.',
984 985 986
  );

  $form['no_default_empty_value'] = $base + array(
987 988
    '#title' => 'No #default_value, #required, #empty_value 0',
    '#required' => TRUE,
989 990 991 992
    '#empty_value' => 0,
    '#description' => 'Should never result in 0.',
  );
  $form['no_default_empty_value_one'] = $base + array(
993 994
    '#title' => 'No #default_value, #required, #empty_value one',
    '#required' => TRUE,
995 996 997 998 999 1000 1001
    '#empty_value' => 'one',
    '#description' => 'A mistakenly assigned #empty_value contained in #options should not be valid.',
  );
  $form['no_default_empty_value_optional'] = $base + array(
    '#title' => 'No #default_value, not #required, #empty_value 0',
    '#required' => FALSE,
    '#empty_value' => 0,
1002
    '#description' => 'Should result in 0 because it is optional.',
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
  );

  $form['multiple'] = $base + array(
    '#title' => '#multiple, #default_value two',
    '#default_value' => array('two'),
    '#multiple' => TRUE,
  );
  $form['multiple_no_default'] = $base + array(
    '#title' => '#multiple, no #default_value',
    '#multiple' => TRUE,
  );
  $form['multiple_no_default_required'] = $base + array(
    '#title' => '#multiple, #required, no #default_value',
    '#required' => TRUE,
    '#multiple' => TRUE,
  );

  $form['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  return $form;
}

/**
 * Form submit handler for form_test_select().
 */
function form_test_select_submit($form, &$form_state) {
  drupal_json_output($form_state['values']);
  exit();
}

1032 1033 1034 1035
/**
 * Builds a form to test the placeholder attribute.
 */
function form_test_placeholder_test($form, &$form_state) {
1036
  foreach (array('textfield', 'textarea', 'password', 'tel', 'email') as $type) {
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
    $form[$type] = array(
      '#type' => $type,
      '#title' => $type,
      '#placeholder' => 'placeholder-text',
    );
  }

  return $form;
}

1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
/**
 * Form constructor to test expansion of #type checkboxes and radios.
 */
function form_test_checkboxes_radios($form, &$form_state, $customize = FALSE) {
  $form['#submit'] = array('_form_test_submit_values_json');

  // Expand #type checkboxes, setting custom element properties for some but not
  // all options.
  $form['checkboxes'] = array(
    '#type' => 'checkboxes',
    '#title' => 'Checkboxes',
    '#options' => array(
      0 => 'Zero',
      'foo' => 'Foo',
      1 => 'One',
      'bar' => 'Bar',
1063
      '>' => 'Special Char',
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
    ),
  );
  if ($customize) {
    $form['checkboxes'] += array(
      'foo' => array(
        '#description' => 'Enable to foo.',
      ),
      1 => array(
        '#weight' => 10,
      ),
    );
  }

  // Expand #type radios, setting custom element properties for some but not
  // all options.
  $form['radios'] = array(
    '#type' => 'radios',
    '#title' => 'Radios',
    '#options' => array(
      0 => 'Zero',
      'foo' => 'Foo',
      1 => 'One',
      'bar' => 'Bar',
1087
      '>' => 'Special Char',
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
    ),
  );
  if ($customize) {
    $form['radios'] += array(
      'foo' => array(
        '#description' => 'Enable to foo.',
      ),
      1 => array(
        '#weight' => 10,
      ),
    );
  }

  $form['submit'] = array('#type' => 'submit', '#value' => 'Submit');

  return $form;
}

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
/**
 * Form constructor for testing #type 'email' elements.
 *
 * @see form_test_email_submit()
 * @ingroup forms
 */
function form_test_email($form, &$form_state) {
  $form['email'] = array(
    '#type' => 'email',
    '#title' => 'E-Mail address',
    '#description' => 'An e-mail address.',
  );
  $form['email_required'] = array(
    '#type' => 'email',
    '#title' => 'Address',
    '#required' => TRUE,
    '#description' => 'A required e-mail address field.',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  return $form;
}

/**
 * Form submission handler for form_test_email().
 */
function form_test_email_submit($form, &$form_state) {
  drupal_json_output($form_state['values']);
  exit();
}

1139 1140 1141 1142 1143
/**
 * Build a form to test disabled elements.
 */
function _form_test_disabled_elements($form, &$form_state) {
  // Elements that take a simple default value.
1144
  foreach (array('textfield', 'textarea', 'tel', 'hidden') as $type) {
1145 1146 1147 1148
    $form[$type] = array(
      '#type' => $type,
      '#title' => $type,
      '#default_value' => $type,
1149
      '#test_hijack_value' => 'HIJACK',
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
      '#disabled' => TRUE,
    );
  }

  // Multiple values option elements.
  foreach (array('checkboxes', 'select') as $type) {
    $form[$type . '_multiple'] = array(
      '#type' => $type,
      '#title' => $type . ' (multiple)',
      '#options' => array(
        'test_1' => 'Test 1',
        'test_2' => 'Test 2',
      ),
      '#multiple' => TRUE,
      '#default_value' => array('test_2' => 'test_2'),
1165 1166 1167
      // The keys of #test_hijack_value need to match the #name of the control.
      // @see FormsTestCase::testDisabledElements()
      '#test_hijack_value' => $type == 'select' ? array('' => 'test_1') : array('test_1' => 'test_1'),
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
      '#disabled' => TRUE,
    );
  }

  // Single values option elements.
  foreach (array('radios', 'select') as $type) {
    $form[$type . '_single'] = array(
      '#type' => $type,
      '#title' => $type . ' (single)',
      '#options' => array(
        'test_1' => 'Test 1',
        'test_2' => 'Test 2',
      ),
      '#multiple' => FALSE,
      '#default_value' => 'test_2',
1183
      '#test_hijack_value' => 'test_1',
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
      '#disabled' => TRUE,
    );
  }

  // Checkbox and radio.
  foreach (array('checkbox', 'radio') as $type) {
    $form[$type . '_unchecked'] = array(
      '#type' => $type,
      '#title' => $type . ' (unchecked)',
      '#return_value' => 1,
      '#default_value' => 0,
1195
      '#test_hijack_value' => 1,
1196 1197 1198 1199 1200 1201 1202
      '#disabled' => TRUE,
    );
    $form[$type . '_checked'] = array(
      '#type' => $type,
      '#title' => $type . ' (checked)',
      '#return_value' => 1,
      '#default_value' => 1,
1203
      '#test_hijack_value' => NULL,
1204 1205 1206 1207 1208 1209 1210 1211 1212
      '#disabled' => TRUE,
    );
  }

  // Weight.
  $form['weight'] = array(
    '#type' => 'weight',
    '#title' => 'weight',
    '#default_value' => 10,
1213
    '#test_hijack_value' => 5,
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
    '#disabled' => TRUE,
  );

  // Date.
  $form['date'] = array(
    '#type' => 'date',
    '#title' => 'date',
    '#disabled' => TRUE,
    '#default_value' => array(
      'day' => 19,
      'month' => 11,
      'year' => 1978,
    ),
1227 1228 1229 1230 1231
    '#test_hijack_value' => array(
      'day' => 20,
      'month' => 12,
      'year' => 1979,
    ),
1232 1233
  );

1234 1235 1236 1237
  // The #disabled state should propagate to children.
  $form['disabled_container'] = array(
    '#disabled' => TRUE,
  );
1238
  foreach (array('textfield', 'textarea', 'hidden', 'tel') as $type) {
1239 1240 1241 1242 1243 1244 1245 1246
    $form['disabled_container']['disabled_container_' . $type] = array(
      '#type' => $type,
      '#title' => $type,
      '#default_value' => $type,
      '#test_hijack_value' => 'HIJACK',
    );
  }

1247 1248 1249 1250 1251 1252 1253 1254
  // Try to hijack the email field with a valid email.
  $form['disabled_container']['disabled_container_email'] = array(
    '#type' => 'email',
    '#title' => 'email',
    '#default_value' => 'foo@example.com',
    '#test_hijack_value' => 'bar@example.com',
  );

1255 1256 1257 1258 1259 1260
  // Text format.
  $form['text_format'] = array(
    '#type' => 'text_format',
    '#title' => 'Text format',
    '#disabled' => TRUE,
    '#default_value' => 'Text value',
1261
    '#format' => 'plain_text',
1262 1263
    '#expected_value' => array(
      'value' => 'Text value',
1264
      'format' => 'plain_text',
1265 1266 1267
    ),
    '#test_hijack_value' => array(
      'value' => 'HIJACK',
1268
      'format' => 'filtered_html',
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
    ),
  );

  // Password fields.
  $form['password'] = array(
    '#type' => 'password',
    '#title' => 'Password',
    '#disabled' => TRUE,
  );
  $form['password_confirm'] = array(
    '#type' => 'password_confirm',
    '#title' => 'Password confirm',
    '#disabled' => TRUE,
  );

  // Files.
  $form['file'] = array(
    '#type' => 'file',
    '#title' => 'File',
    '#disabled' => TRUE,
  );
  $form['managed_file'] = array(
    '#type' => 'managed_file',
    '#title' => 'Managed file',
    '#disabled' => TRUE,
  );

  // Buttons.
  $form['image_button'] = array(
    '#type' => 'image_button',
    '#value' => 'Image button',
    '#disabled' => TRUE,
  );
  $form['button'] = array(
    '#type' => 'button',
    '#value' => 'Button',
    '#disabled' => TRUE,
  );
  $form['submit_disabled'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    '#disabled' => TRUE,
  );

1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;
}

/**
 * Return the form values via JSON.
 */
function _form_test_disabled_elements_submit($form, &$form_state) {
  drupal_json_output($form_state['values']);
  exit();
}

1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
/**
 * Build a form to test input forgery of enabled elements.
 */
function _form_test_input_forgery($form, &$form_state) {
  // For testing that a user can't submit a value not matching one of the
  // allowed options.
  $form['checkboxes'] = array(
    '#type' => 'checkboxes',
    '#options' => array(
      'one' => 'One',
      'two' => 'Two',
    ),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

/**
 * Return the form values via JSON.
 */
function _form_test_input_forgery_submit($form, &$form_state) {
  drupal_json_output($form_state['values']);
  exit();
}

1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
/**
 * Form builder for testing preservation of values during a rebuild.
 */
function form_test_form_rebuild_preserve_values_form($form, &$form_state) {
  // Start the form with two checkboxes, to test different defaults, and a
  // textfield, to test more than one element type.
  $form = array(
    'checkbox_1_default_off' => array(
      '#type' => 'checkbox',
      '#title' => t('This checkbox defaults to unchecked.'),
      '#default_value' => FALSE,
    ),
    'checkbox_1_default_on' => array(
      '#type' => 'checkbox',
      '#title' => t('This checkbox defaults to checked.'),
      '#default_value' => TRUE,
    ),
    'text_1' => array(
      '#type' => 'textfield',
      '#title' => t('This textfield has a non-empty default value.'),
      '#default_value' => 'DEFAULT 1',
    ),
  );
  // Provide an 'add more' button that rebuilds the form with an additional two
  // checkboxes and a textfield. The test is to make sure that the rebuild
  // triggered by this button preserves the user input values for the initial
  // elements and initializes the new elements with the correct default values.
  if (empty($form_state['storage']['add_more'])) {
    $form['add_more'] = array(
      '#type' => 'submit',
      '#value' => 'Add more',
      '#submit' => array('form_test_form_rebuild_preserve_values_form_add_more'),
    );
  }
  else {
    $form += array(
      'checkbox_2_default_off' => array(
        '#type' => 'checkbox',
        '#title' => t('This checkbox defaults to unchecked.'),
        '#default_value' => FALSE,
      ),
      'checkbox_2_default_on' => array(
        '#type' => 'checkbox',
        '#title' => t('This checkbox defaults to checked.'),
        '#default_value' => TRUE,
      ),
      'text_2' => array(
        '#type' => 'textfield',
        '#title' => t('This textfield has a non-empty default value.'),
        '#default_value' => 'DEFAULT 2',
      ),
    );
  }
  // A submit button that finishes the form workflow (does not rebuild).
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  return $form;
}

/**
 * Button submit handler for form_test_form_rebuild_preserve_values_form().
 */
function form_test_form_rebuild_preserve_values_form_add_more($form, &$form_state) {
  // Rebuild, to test preservation of input values.
  $form_state['storage']['add_more'] = TRUE;
  $form_state['rebuild'] = TRUE;
}

/**
 * Form submit handler for form_test_form_rebuild_preserve_values_form().
 */
function form_test_form_rebuild_preserve_values_form_submit($form, &$form_state) {
  // Finish the workflow. Do not rebuild.
  drupal_set_message(t('Form values: %values', array('%values' => var_export($form_state['values'], TRUE))));
}
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456

/**
 * Form constructor for testing form state persistence.
 */
function form_test_state_persist($form, &$form_state) {
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => 'title',
    '#default_value' => 'DEFAULT',
    '#required' => TRUE,
  );
  $form_state['value'] = 'State persisted.';

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

/**
 * Submit handler.
1457
 *
1458 1459 1460 1461 1462 1463 1464 1465 1466
 * @see form_test_state_persist()
 */
function form_test_state_persist_submit($form, &$form_state) {
  drupal_set_message($form_state['value']);
  $form_state['rebuild'] = TRUE;
}

/**
 * Implements hook_form_FORM_ID_alter().
1467
 *
1468 1469 1470 1471 1472 1473 1474 1475 1476
 * @see form_test_state_persist()
 */
function form_test_form_form_test_state_persist_alter(&$form, &$form_state) {
  // Simulate a form alter implementation inserting form elements that enable
  // caching of the form, e.g. elements having #ajax.
  if (!empty($_REQUEST['cache'])) {
    $form_state['cache'] = TRUE;
  }
}
1477 1478 1479 1480 1481

/**
 * Form builder to test programmatic form submissions.
 */
function form_test_programmatic_form($form, &$form_state) {
1482 1483
  $form['textfield'] = array(
    '#title' => 'Textfield',
1484 1485
    '#type' => 'textfield',
  );
1486

1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
  $form['checkboxes'] = array(
    '#type' => 'checkboxes',
    '#options' => array(
      1 => 'First checkbox',
      2 => 'Second checkbox',
    ),
    // Both checkboxes are selected by default so that we can test the ability
    // of programmatic form submissions to uncheck them.
    '#default_value' => array(1, 2),
  );
1497