form_test.module 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
// $Id$

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

/**
10
 * Implement hook_menu().
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 */
function form_test_menu() {
  $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,
  );

42 43 44
  $items['form_test/drupal_form_submit_batch_api'] = array(
    'title' => 'BatchAPI Drupal_form_submit tests',
    'page callback' => 'form_test_drupal_form_submit_batch_api',
45 46 47 48
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

49 50 51 52 53 54 55 56
  $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,
  );

57 58 59 60 61 62 63 64
  $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,
  );

65 66 67 68 69 70 71 72
  $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,
  );

73 74 75 76 77 78 79 80
  $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,
  );

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  return $items;
}

/**
 * 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.
 */
130
function _form_test_tableselect_form_builder($form, $form_state, $element_properties) {
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  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.
 */
154 155
function _form_test_tableselect_multiple_true_form($form, $form_state) {
  return _form_test_tableselect_form_builder($form, $form_state, array('#multiple' => TRUE));
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
}

/**
 * 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.
 */
171 172
function _form_test_tableselect_multiple_false_form($form, $form_state) {
  return _form_test_tableselect_form_builder($form, $form_state, array('#multiple' => FALSE));
173 174 175 176 177 178 179 180 181 182 183 184
}

/**
 * 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.
 */
185 186
function _form_test_tableselect_empty_form($form, $form_state) {
  return _form_test_tableselect_form_builder($form, $form_state, array('#options' => array()));
187 188 189 190 191
}

/**
 * Test functionality of the tableselect #js_select property.
 */
192
function _form_test_tableselect_js_select_form($form, $form_state, $action) {
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  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;
  }

211
  return _form_test_tableselect_form_builder($form, $form_state, $options);
212
}
213 214

/**
215
 * Page callback for the batch/drupal_form_submit interaction test.
216 217 218
 *
 * When called without any arguments we set up a batch that calls
 * form_test_batch_callback. That function will submit a form using
219
 * drupal_form_submit using the values specified in this function.
220 221
 *
 * The form's field test_value begins at 'initial_value', and is changed
222
 * to 'form_submitted' when the form is submitted successfully. On
223 224
 * completion this function is passed 'done' to complete the process.
 */
225
function form_test_drupal_form_submit_batch_api($arg = '') {
226 227 228 229 230 231 232 233 234 235 236 237
  // 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);
238
  batch_process('form_test/drupal_form_submit_batch_api/done');
239 240 241
}

/**
242
 * Submits form_test_mock_form using drupal_form_submit using the given $value.
243 244 245
 */
function form_test_batch_callback($value) {
  $state['values']['test_value'] = $value;
246
  drupal_form_submit('form_test_mock_form', $state);
247 248 249 250 251
}

/**
 * A simple form with a textfield and submit button.
 */
252
function form_test_mock_form($form, $form_state) {
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  $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']);
}
274 275 276 277 278 279 280

/**
 * 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
281
 * it would be the case, if the form would contain some #ajax callbacks.
282 283 284
 *
 * @see form_storage_test_form_submit().
 */
285
function form_storage_test_form($form, &$form_state) {
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
  // Initialize
  if (!isset($form_state['storage'])) {
    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 {
322
    $form['body'] = array('#value' => 'This is the second step.');
323 324 325 326 327 328 329 330 331
    $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.
332
    $form_state['cache'] = TRUE;
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
  }

  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']));
  }
  $form_state['storage']['step']++;
  drupal_set_message("Form constructions: ". $_SESSION['constructions']);
}
352 353 354 355 356 357

/**
 * Menu callback; Invokes a form builder function with a wrapper callback.
 */
function form_test_wrapper_callback($form_id) {
  $form_state = array(
358
    'build_info' => array('args' => array()),
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
    '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;
}

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
/**
 * 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;
}
404 405 406 407 408 409 410 411 412 413 414 415 416 417 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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

/**
 * 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();
}