Newer
Older
Angie Byron
committed
<?php
// $Id$
/**
* @file
* Helper module for the form API tests.
*/
/**
Dries Buytaert
committed
* Implements hook_menu().
Angie Byron
committed
*/
function form_test_menu() {
$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,
);
$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,
);
Angie Byron
committed
$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,
);
Angie Byron
committed
$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,
);
$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,
);
Dries Buytaert
committed
$items['form_test/form-storage'] = array(
'title' => 'Form storage test',
'page callback' => 'drupal_get_form',
Dries Buytaert
committed
'page arguments' => array('form_test_storage_form'),
Dries Buytaert
committed
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$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,
);
$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,
);
$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,
);
Dries Buytaert
committed
$items['form-test/select'] = array(
'title' => t('Select'),
'page callback' => 'drupal_get_form',
'page arguments' => array('form_test_select'),
'access callback' => TRUE,
);
Dries Buytaert
committed
$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,
);
Angie Byron
committed
$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,
);
Dries Buytaert
committed
$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,
);
Dries Buytaert
committed
$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,
);
Dries Buytaert
committed
$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,
);
Dries Buytaert
committed
$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,
);
$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,
);
Dries Buytaert
committed
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,
);
}
Dries Buytaert
committed
$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,
);
Dries Buytaert
committed
$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,
);
Dries Buytaert
committed
Angie Byron
committed
return $items;
}
Dries Buytaert
committed
/**
* 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();
}
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* 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.');
}
/**
* 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',
);
Dries Buytaert
committed
// To simplify this test, enable form caching and use form storage to
// remember our alteration.
$form_state['cache'] = TRUE;
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
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
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('');
}
}
Angie Byron
committed
/**
* 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,
);
Angie Byron
committed
$form['test'] = array(
'#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'),
);
$form['test_substring'] = array(
'#tree' => TRUE,
);
$form['test_substring']['foo'] = array(
'#title' => 'Test (substring) foo',
Angie Byron
committed
'#type' => 'textfield',
'#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
);
$form['test_substring']['foobar'] = array(
'#title' => 'Test (substring) foobar',
'#type' => 'textfield',
'#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
);
Angie Byron
committed
$form['actions']['partial'] = array(
'#type' => 'submit',
'#limit_validation_errors' => array(array('test')),
'#submit' => array('form_test_limit_validation_errors_form_partial_submit'),
Angie Byron
committed
'#value' => t('Partial validate'),
);
$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)'),
);
Angie Byron
committed
$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') {
form_error($element, t('@label element is invalid', array('@label' => $element['#title'])));
Angie Byron
committed
}
}
/**
* 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.');
}
}
Angie Byron
committed
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
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
/**
* 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.
*/
function _form_test_tableselect_form_builder($form, $form_state, $element_properties) {
Angie Byron
committed
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.
*/
function _form_test_tableselect_multiple_true_form($form, $form_state) {
return _form_test_tableselect_form_builder($form, $form_state, array('#multiple' => TRUE));
Angie Byron
committed
}
/**
* 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.
*/
function _form_test_tableselect_multiple_false_form($form, $form_state) {
return _form_test_tableselect_form_builder($form, $form_state, array('#multiple' => FALSE));
Angie Byron
committed
}
/**
* 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.
*/
function _form_test_tableselect_empty_form($form, $form_state) {
return _form_test_tableselect_form_builder($form, $form_state, array('#options' => array()));
Angie Byron
committed
}
/**
* Test functionality of the tableselect #js_select property.
*/
function _form_test_tableselect_js_select_form($form, $form_state, $action) {
Angie Byron
committed
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;
}
return _form_test_tableselect_form_builder($form, $form_state, $options);
Angie Byron
committed
}
Angie Byron
committed
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/**
* 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;
}
Dries Buytaert
committed
/**
* 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
Angie Byron
committed
* it would be the case, if the form would contain some #ajax callbacks.
Dries Buytaert
committed
*
Dries Buytaert
committed
* @see form_test_storage_form_submit()
Dries Buytaert
committed
*/
Dries Buytaert
committed
function form_test_storage_form($form, &$form_state) {
if ($form_state['rebuild']) {
$form_state['input'] = array();
}
Dries Buytaert
committed
// Initialize
if (empty($form_state['storage'])) {
Dries Buytaert
committed
if (empty($form_state['input'])) {
$_SESSION['constructions'] = 0;
}
// Put the initial thing into the storage
$form_state['storage'] = array(
'thing' => array(
'title' => 'none',
'value' => '',
),
);
}
// Count how often the form is constructed.
Dries Buytaert
committed
$_SESSION['constructions']++;
Dries Buytaert
committed
drupal_set_message("Form constructions: " . $_SESSION['constructions']);
Dries Buytaert
committed
Dries Buytaert
committed
$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'),
);
$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',
);
Dries Buytaert
committed
if (isset($_REQUEST['cache'])) {
// Manually activate caching, so we can test that the storage keeps working
// when it's enabled.
$form_state['cache'] = TRUE;
Dries Buytaert
committed
}
return $form;
}
/**
Dries Buytaert
committed
* Form element validation handler for 'value' element in form_test_storage_form().
*
* Tests updating of cached form storage during validation.
Dries Buytaert
committed
*/
Dries Buytaert
committed
function form_test_storage_element_validate_value_cached($element, &$form_state) {
// 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.
Dries Buytaert
committed
if (isset($_REQUEST['cache']) && $form_state['values']['value'] == 'change_title') {
$form_state['storage']['thing']['changed'] = TRUE;
Dries Buytaert
committed
}
}
/**
* Form submit handler to continue multi-step form.
Dries Buytaert
committed
*/
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) {
Dries Buytaert
committed
drupal_set_message("Title: " . check_plain($form_state['values']['title']));
drupal_set_message("Form constructions: " . $_SESSION['constructions']);
if (isset($form_state['storage']['thing']['changed'])) {
drupal_set_message("The thing has been changed.");
}
$form_state['redirect'] = 'node';
Dries Buytaert
committed
}
Dries Buytaert
committed
/**
Dries Buytaert
committed
* A form for testing form labels and required marks.
*/
Dries Buytaert
committed
function form_label_test_form() {
Dries Buytaert
committed
$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'),
),
);
$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'),
),
Dries Buytaert
committed
// 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>',
Dries Buytaert
committed
);
$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',
// We use an empty title, since not setting #title supresses the label
// and required marker.
'#title' => '',
'#required' => TRUE,
);
$form['form_textfield_test_title'] = array(
'#type' => 'textfield',
'#title' => t('Textfield test for title only'),
// Not required.
Dries Buytaert
committed
// 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>',
Dries Buytaert
committed
);
$form['form_textfield_test_title_after'] = array(
'#type' => 'textfield',
'#title' => t('Textfield test for title after element'),
'#title_display' => 'after',
);
$form['form_textfield_test_title_invisible'] = array(
'#type' => 'textfield',
'#title' => t('Textfield test for invisible title'),
'#title_display' => 'invisible',
);
Dries Buytaert
committed
// Textfield test for title set not to display
$form['form_textfield_test_title_no_show'] = array(
'#type' => 'textfield',
);
return $form;
}
/**
* Menu callback; Invokes a form builder function with a wrapper callback.
*/
function form_test_wrapper_callback($form_id) {
$form_state = array(
Angie Byron
committed
'build_info' => array('args' => array()),
'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;
}
/**
* 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;
}
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
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
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
/**
* 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();
}
Dries Buytaert
committed
Dries Buytaert
committed
/**
* 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',
);
Dries Buytaert
committed
$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',
);
Dries Buytaert
committed
$form['empty_value'] = $base + array(
Dries Buytaert
committed
'#title' => '#default_value one, #required, #empty_value 0',
'#required' => TRUE,
Dries Buytaert
committed
'#default_value' => 'one',
'#empty_value' => 0,
);
$form['empty_value_one'] = $base + array(
Dries Buytaert
committed
'#title' => '#default_value = #empty_value, #required',
'#required' => TRUE,
Dries Buytaert
committed
'#default_value' => 'one',
'#empty_value' => 'one',
);
$form['no_default'] = $base + array(
Dries Buytaert
committed
'#title' => 'No #default_value, #required',
'#required' => TRUE,
Dries Buytaert
committed
);
$form['no_default_optional'] = $base + array(
'#title' => 'No #default_value, not #required',
'#required' => FALSE,
Dries Buytaert
committed
'#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.',
);
$form['no_default_optional_empty_value'] = $base + array(
'#title' => 'No #default_value, not #required, #empty_value empty string',
'#empty_value' => '',
'#required' => FALSE,
'#description' => 'Should result in an empty string (due to #empty_value), because it is optional.',
Dries Buytaert
committed
);
$form['no_default_empty_option'] = $base + array(
Dries Buytaert
committed
'#title' => 'No #default_value, #required, #empty_option',
'#required' => TRUE,
Dries Buytaert
committed
'#empty_option' => '- Choose -',
);
$form['no_default_empty_option_optional'] = $base + array(
'#title' => 'No #default_value, not #required, #empty_option',
'#empty_option' => '- Dismiss -',
'#description' => 'Should result in an empty string (default of #empty_value), because it is optional.',
);
$form['no_default_empty_value'] = $base + array(
Dries Buytaert
committed
'#title' => 'No #default_value, #required, #empty_value 0',
'#required' => TRUE,
Dries Buytaert
committed
'#empty_value' => 0,
'#description' => 'Should never result in 0.',
);
$form['no_default_empty_value_one'] = $base + array(
Dries Buytaert
committed
'#title' => 'No #default_value, #required, #empty_value one',
'#required' => TRUE,
Dries Buytaert
committed
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
'#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,
'#description' => 'Should result in 0, because it is optional.',
);
$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();
}
Dries Buytaert
committed
/**
* 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',
),