2009-01-28 07:43:26 +00:00
|
|
|
<?php
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Helper module for the form API tests.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-05-27 18:34:03 +00:00
|
|
|
* Implement hook_menu().
|
2009-01-28 07:43:26 +00:00
|
|
|
*/
|
|
|
|
function form_test_menu() {
|
2009-11-28 14:39:31 +00:00
|
|
|
$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,
|
|
|
|
);
|
|
|
|
|
2009-01-28 07:43:26 +00:00
|
|
|
$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,
|
|
|
|
);
|
|
|
|
|
2009-04-29 07:18:04 +00:00
|
|
|
$items['form_test/drupal_form_submit_batch_api'] = array(
|
|
|
|
'title' => 'BatchAPI Drupal_form_submit tests',
|
|
|
|
'page callback' => 'form_test_drupal_form_submit_batch_api',
|
2009-03-28 18:09:11 +00:00
|
|
|
'access arguments' => array('access content'),
|
|
|
|
'type' => MENU_CALLBACK,
|
|
|
|
);
|
|
|
|
|
2009-04-22 09:12:44 +00:00
|
|
|
$items['form_test/form-storage'] = array(
|
|
|
|
'title' => 'Form storage test',
|
|
|
|
'page callback' => 'drupal_get_form',
|
|
|
|
'page arguments' => array('form_storage_test_form'),
|
|
|
|
'access arguments' => array('access content'),
|
|
|
|
'type' => MENU_CALLBACK,
|
|
|
|
);
|
|
|
|
|
2009-09-18 00:12:48 +00:00
|
|
|
$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,
|
|
|
|
);
|
|
|
|
|
2009-10-15 11:47:25 +00:00
|
|
|
$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,
|
|
|
|
);
|
|
|
|
|
2009-11-15 21:36:06 +00:00
|
|
|
$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,
|
|
|
|
);
|
|
|
|
|
2009-01-28 07:43:26 +00:00
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
2009-11-28 14:39:31 +00:00
|
|
|
/**
|
|
|
|
* 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',
|
|
|
|
);
|
|
|
|
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') {
|
|
|
|
// To simplify this test, enable form caching and use form storage to
|
|
|
|
// remember our alteration.
|
|
|
|
$form_state['cache'] = TRUE;
|
|
|
|
$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('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-28 07:43:26 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-09-18 00:12:48 +00:00
|
|
|
function _form_test_tableselect_form_builder($form, $form_state, $element_properties) {
|
2009-01-28 07:43:26 +00:00
|
|
|
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.
|
|
|
|
*/
|
2009-09-18 00:12:48 +00:00
|
|
|
function _form_test_tableselect_multiple_true_form($form, $form_state) {
|
|
|
|
return _form_test_tableselect_form_builder($form, $form_state, array('#multiple' => TRUE));
|
2009-01-28 07:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-09-18 00:12:48 +00:00
|
|
|
function _form_test_tableselect_multiple_false_form($form, $form_state) {
|
|
|
|
return _form_test_tableselect_form_builder($form, $form_state, array('#multiple' => FALSE));
|
2009-01-28 07:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-09-18 00:12:48 +00:00
|
|
|
function _form_test_tableselect_empty_form($form, $form_state) {
|
|
|
|
return _form_test_tableselect_form_builder($form, $form_state, array('#options' => array()));
|
2009-01-28 07:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test functionality of the tableselect #js_select property.
|
|
|
|
*/
|
2009-09-18 00:12:48 +00:00
|
|
|
function _form_test_tableselect_js_select_form($form, $form_state, $action) {
|
2009-01-28 07:43:26 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-09-18 00:12:48 +00:00
|
|
|
return _form_test_tableselect_form_builder($form, $form_state, $options);
|
2009-01-28 07:43:26 +00:00
|
|
|
}
|
2009-03-28 18:09:11 +00:00
|
|
|
|
|
|
|
/**
|
2009-04-29 07:18:04 +00:00
|
|
|
* Page callback for the batch/drupal_form_submit interaction test.
|
2009-03-28 18:09:11 +00:00
|
|
|
*
|
|
|
|
* When called without any arguments we set up a batch that calls
|
|
|
|
* form_test_batch_callback. That function will submit a form using
|
2009-04-29 07:18:04 +00:00
|
|
|
* drupal_form_submit using the values specified in this function.
|
2009-03-28 18:09:11 +00:00
|
|
|
*
|
|
|
|
* The form's field test_value begins at 'initial_value', and is changed
|
2009-11-10 17:27:54 +00:00
|
|
|
* to 'form_submitted' when the form is submitted successfully. On
|
2009-03-28 18:09:11 +00:00
|
|
|
* completion this function is passed 'done' to complete the process.
|
|
|
|
*/
|
2009-04-29 07:18:04 +00:00
|
|
|
function form_test_drupal_form_submit_batch_api($arg = '') {
|
2009-03-28 18:09:11 +00:00
|
|
|
// If we're at the end of the batch process, return.
|
|
|
|
if ($arg == 'done') {
|
|
|
|
return t('Done');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise set up the batch.
|
|
|
|
$batch['operations'] = array(
|
|
|
|
array('form_test_batch_callback', array('form_submitted')),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Set the batch and process it.
|
|
|
|
batch_set($batch);
|
2009-04-29 07:18:04 +00:00
|
|
|
batch_process('form_test/drupal_form_submit_batch_api/done');
|
2009-03-28 18:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-29 07:18:04 +00:00
|
|
|
* Submits form_test_mock_form using drupal_form_submit using the given $value.
|
2009-03-28 18:09:11 +00:00
|
|
|
*/
|
|
|
|
function form_test_batch_callback($value) {
|
|
|
|
$state['values']['test_value'] = $value;
|
2009-04-29 07:18:04 +00:00
|
|
|
drupal_form_submit('form_test_mock_form', $state);
|
2009-03-28 18:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple form with a textfield and submit button.
|
|
|
|
*/
|
2009-09-18 00:12:48 +00:00
|
|
|
function form_test_mock_form($form, $form_state) {
|
2009-03-28 18:09:11 +00:00
|
|
|
$form['test_value'] = array(
|
|
|
|
'#type' => 'textfield',
|
|
|
|
'#default_value' => 'initial_state',
|
|
|
|
);
|
|
|
|
|
|
|
|
$form['submit'] = array(
|
|
|
|
'#type' => 'submit',
|
|
|
|
'#value' => t('Submit'),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Form submission callback.
|
|
|
|
*
|
|
|
|
* Updates the variable 'form_test_mock_submit' to the submitted form value.
|
|
|
|
*/
|
|
|
|
function form_test_mock_form_submit($form, &$form_state) {
|
|
|
|
variable_set('form_test_mock_submit', $form_state['values']['test_value']);
|
|
|
|
}
|
2009-04-22 09:12:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2009-08-17 07:12:16 +00:00
|
|
|
* it would be the case, if the form would contain some #ajax callbacks.
|
2009-04-22 09:12:44 +00:00
|
|
|
*
|
|
|
|
* @see form_storage_test_form_submit().
|
|
|
|
*/
|
2009-09-18 00:12:48 +00:00
|
|
|
function form_storage_test_form($form, &$form_state) {
|
2009-04-22 09:12:44 +00:00
|
|
|
// Initialize
|
2009-11-21 17:01:31 +00:00
|
|
|
if (empty($form_state['storage'])) {
|
2009-04-22 09:12:44 +00:00
|
|
|
if (empty($form_state['input'])) {
|
|
|
|
$_SESSION['constructions'] = 0;
|
|
|
|
}
|
|
|
|
// Put the initial thing into the storage
|
|
|
|
$form_state['storage'] = array(
|
|
|
|
'thing' => array(
|
|
|
|
'title' => 'none',
|
|
|
|
'value' => '',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$form_state['storage'] += array('step' => 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count how often the form is constructed
|
|
|
|
$_SESSION['constructions']++;
|
|
|
|
|
|
|
|
if ($form_state['storage']['step'] == 1) {
|
|
|
|
$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'],
|
|
|
|
);
|
|
|
|
$form['submit'] = array(
|
|
|
|
'#type' => 'submit',
|
|
|
|
'#value' => 'Continue',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
2009-05-12 08:37:45 +00:00
|
|
|
$form['body'] = array('#value' => 'This is the second step.');
|
2009-04-22 09:12:44 +00:00
|
|
|
$form['submit'] = array(
|
|
|
|
'#type' => 'submit',
|
|
|
|
'#value' => 'Save',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_REQUEST['cache'])) {
|
|
|
|
// Manually activate caching, so we can test that the storage keeps working
|
|
|
|
// when it's enabled.
|
2009-11-18 18:51:11 +00:00
|
|
|
$form_state['cache'] = TRUE;
|
2009-04-22 09:12:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multistep form submit callback.
|
|
|
|
*/
|
|
|
|
function form_storage_test_form_submit($form, &$form_state) {
|
|
|
|
if ($form_state['storage']['step'] == 1) {
|
|
|
|
$form_state['storage']['thing']['title'] = $form_state['values']['title'];
|
|
|
|
$form_state['storage']['thing']['value'] = $form_state['values']['value'];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
drupal_set_message("Title: ". check_plain($form_state['storage']['thing']['title']));
|
|
|
|
}
|
2009-11-21 14:06:46 +00:00
|
|
|
$form_state['rebuild'] = TRUE;
|
2009-04-22 09:12:44 +00:00
|
|
|
$form_state['storage']['step']++;
|
|
|
|
drupal_set_message("Form constructions: ". $_SESSION['constructions']);
|
|
|
|
}
|
2009-09-18 00:12:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Menu callback; Invokes a form builder function with a wrapper callback.
|
|
|
|
*/
|
|
|
|
function form_test_wrapper_callback($form_id) {
|
|
|
|
$form_state = array(
|
2009-11-04 04:56:54 +00:00
|
|
|
'build_info' => array('args' => array()),
|
2009-09-18 00:12:48 +00:00
|
|
|
'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;
|
|
|
|
}
|
|
|
|
|
2009-10-15 11:47:25 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2009-11-15 21:36:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|