| 1 |
<?php |
<?php |
| 2 |
|
|
| 3 |
// $Id$ |
// $Id: multipage_form_example.module,v 1.1 2006/03/22 19:16:41 jvandyk Exp $ |
| 4 |
|
|
| 5 |
/** |
/** |
| 6 |
* Implementation of hook_help(). |
* Implementation of hook_help(). |
| 50 |
* #required attributes here - we leave that up to multipage_form_example_pre_render(). |
* #required attributes here - we leave that up to multipage_form_example_pre_render(). |
| 51 |
*/ |
*/ |
| 52 |
function multipage_form_example_form(&$node) { |
function multipage_form_example_form(&$node) { |
|
|
|
| 53 |
$form = array(); |
$form = array(); |
| 54 |
|
|
| 55 |
// 'checkboxes' elements are just ridiculously hard--i recommend just |
// 'checkboxes' elements are just ridiculously hard--i recommend just |
| 74 |
// based on validated $form_values instead of $_POST, and so forth. |
// based on validated $form_values instead of $_POST, and so forth. |
| 75 |
$form['#pre_render'] = array('multipage_form_example_pre_render'); |
$form['#pre_render'] = array('multipage_form_example_pre_render'); |
| 76 |
|
|
| 77 |
// Title and body, page 1 |
// Title and body, page 1 |
| 78 |
$form['title'] = array( |
$form['title'] = array( |
| 79 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 80 |
'#title' => t('Title'), |
'#title' => t('Title'), |
| 82 |
'#description' => t("Enter a title for your favorites section."), |
'#description' => t("Enter a title for your favorites section."), |
| 83 |
'#size' => 60, |
'#size' => 60, |
| 84 |
'#maxlength' => 128, |
'#maxlength' => 128, |
| 85 |
|
'#required' => TRUE, |
| 86 |
); |
); |
| 87 |
$form['body'] = array( |
$form['body'] = array( |
| 88 |
'#type' => 'textarea', |
'#type' => 'textarea', |
| 94 |
|
|
| 95 |
// Person, page 2 |
// Person, page 2 |
| 96 |
$form['person'] = array( |
$form['person'] = array( |
| 97 |
// This is a fieldset, but has to be set as such in #pre_render, |
'#type' => 'fieldset', |
|
// otherwise we either have the choice of displaying the <fieldset> |
|
|
// all the time, or not passing through the values of its children. |
|
| 98 |
'#title' => t('Your favorite person'), |
'#title' => t('Your favorite person'), |
| 99 |
); |
); |
| 100 |
$form['person']['fav_person'] = array( |
$form['person']['fav_person'] = array( |
| 101 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 102 |
'#title' => t('Name'), |
'#title' => t('Name'), |
| 103 |
'#default_value' => isset($node->fav_person) ? $node->fav_person : NULL, |
'#default_value' => isset($node->fav_person) ? $node->fav_person : NULL, |
| 104 |
'#description' => t('Enter their real name, or their code name if they like to fly stealth.'), |
'#description' => t('Enter their real name, or their code name if they like to fly stealth.'), |
| 105 |
|
'#required' => TRUE, |
| 106 |
); |
); |
| 107 |
$form['person']['fav_person_desc'] = array( |
$form['person']['fav_person_desc'] = array( |
| 108 |
'#type' => 'textarea', |
'#type' => 'textarea', |
| 109 |
'#title' => t('Description'), |
'#title' => t('Description'), |
| 110 |
'#default_value' => isset($node->fav_person_desc) ? $node->fav_person_desc : NULL, |
'#default_value' => isset($node->fav_person_desc) ? $node->fav_person_desc : NULL, |
| 111 |
'#description' => t('Juicy details go here...'), |
'#description' => t('Juicy details go here...'), |
| 112 |
|
'#required' => TRUE, |
| 113 |
); |
); |
| 114 |
$form['person']['fav_gummi'] = array( |
$form['person']['fav_gummi'] = array( |
| 115 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 123 |
'#title' => t('Favorite color'), |
'#title' => t('Favorite color'), |
| 124 |
'#default_value' => isset($node->fav_color) ? $node->fav_color : 'red', |
'#default_value' => isset($node->fav_color) ? $node->fav_color : 'red', |
| 125 |
'#options' => array('red' => t('Red'), 'green' => t('Green'), 'blue' => t('Blue'), 'yellow' => t('Yellow')), |
'#options' => array('red' => t('Red'), 'green' => t('Green'), 'blue' => t('Blue'), 'yellow' => t('Yellow')), |
| 126 |
|
'#required' => TRUE, |
| 127 |
); |
); |
| 128 |
$form['fav_number'] = array( |
$form['fav_number'] = array( |
| 129 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 130 |
'#title' => t('Favorite number'), |
'#title' => t('Favorite number'), |
| 131 |
'#default_value' => isset($node->fav_number) ? $node->fav_number : NULL, |
'#default_value' => isset($node->fav_number) ? $node->fav_number : NULL, |
| 132 |
|
'#required' => TRUE, |
| 133 |
); |
); |
| 134 |
|
|
| 135 |
// Movie and tv show, page 4 |
// Movie and tv show, page 4 |
| 137 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 138 |
'#title' => t('Favorite movie'), |
'#title' => t('Favorite movie'), |
| 139 |
'#default_value' => isset($node->fav_movie) ? $node->fav_movie : NULL, |
'#default_value' => isset($node->fav_movie) ? $node->fav_movie : NULL, |
| 140 |
|
'#required' => TRUE, |
| 141 |
); |
); |
| 142 |
$form['fav_tv'] = array( |
$form['fav_tv'] = array( |
| 143 |
'#type' => 'radios', |
'#type' => 'radios', |
| 144 |
'#title' => t('How often do you watch your favorite tv show?'), |
'#title' => t('How often do you watch your favorite tv show?'), |
| 145 |
'#default_value' => isset($node->fav_tv) ? $node->fav_tv : 'daily', |
'#default_value' => isset($node->fav_tv) ? $node->fav_tv : 'daily', |
| 146 |
'#options' => array('daily' => t('Daily'), 'weekly' => t('Weekly'), 'monthly' => t('Monthly'),), |
'#options' => array('daily' => t('Daily'), 'weekly' => t('Weekly'), 'monthly' => t('Monthly'),), |
| 147 |
|
'#required' => TRUE, |
| 148 |
); |
); |
| 149 |
|
|
| 150 |
// Some markup elements to help the user see what they've already |
// Some markup elements to help the user see what they've already |
| 162 |
$form['back'] = array( |
$form['back'] = array( |
| 163 |
'#type' => 'button', |
'#type' => 'button', |
| 164 |
'#value' => t('Back'), |
'#value' => t('Back'), |
| 165 |
'#weight' => 35, |
'#weight' => 35, |
| 166 |
); |
); |
| 167 |
|
|
|
|
|
| 168 |
return $form; |
return $form; |
| 169 |
} |
} |
| 170 |
|
|
| 173 |
* which keeps track of what stage the form is in. |
* which keeps track of what stage the form is in. |
| 174 |
*/ |
*/ |
| 175 |
function multipage_form_example_form_alter($form_id, &$form) { |
function multipage_form_example_form_alter($form_id, &$form) { |
|
|
|
| 176 |
// Make sure it's our multipage form. |
// Make sure it's our multipage form. |
| 177 |
if ($form_id == 'multipage_form_example_node_form') { |
if ($form_id == 'multipage_form_example_node_form') { |
| 178 |
|
|
| 220 |
* complicated multipage example form. |
* complicated multipage example form. |
| 221 |
*/ |
*/ |
| 222 |
function multipage_form_example_pre_render($form_id, &$form, $next_page = TRUE) { |
function multipage_form_example_pre_render($form_id, &$form, $next_page = TRUE) { |
|
|
|
| 223 |
global $form_values; |
global $form_values; |
| 224 |
|
|
| 225 |
// Make sure it's our multipage form. |
// Make sure it's our multipage form. |
| 226 |
if ($form_id == 'multipage_form_example_node_form') { |
if ($form_id == 'multipage_form_example_node_form') { |
|
|
|
| 227 |
// Are we ready for the next page in our form? |
// Are we ready for the next page in our form? |
| 228 |
if ($next_page && isset($_POST['edit']['page']) && ($_POST['op'] != t('Back'))) { |
if ($next_page && isset($_POST['edit']['page']) && ($_POST['op'] != t('Back'))) { |
| 229 |
$form['page']['#value'] = $form['page']['#value'] + 1; |
$form['page']['#value'] = $form['page']['#value'] + 1; |
| 230 |
} |
} |
| 231 |
|
|
| 232 |
// Validation errors? Show previous page for correcting. |
// Validation errors? Show previous page for correcting. |
| 233 |
if (form_get_errors()) { $form['page']['#value']--; } |
if (form_get_errors()) { |
| 234 |
|
$form['page']['#value']--; |
| 235 |
|
} |
| 236 |
|
|
| 237 |
// Modify the #required/#type values depending on our current page. |
// Modify the #required/#type values depending on our current page. |
| 238 |
// The arrays tell us the pages the changes should take place in. |
// The arrays tell us the pages the changes should take place in. |
| 239 |
|
|
| 240 |
// Title/body, stage 1 |
// Title/body, stage 1 |
| 241 |
$form['title']['#type'] = in_array($form['page']['#value'], array(1)) ? 'textfield' : 'hidden'; |
multipage_form_set_element_visibility($form['title'], in_array($form['page']['#value'], array(1))); |
| 242 |
$form['title']['#required'] = in_array($form['page']['#value'], array(1)) ? 1 : 0; |
multipage_form_set_element_visibility($form['body'], in_array($form['page']['#value'], array(1))); |
|
$form['body']['#type'] = in_array($form['page']['#value'], array(1)) ? 'textarea' : 'hidden'; |
|
| 243 |
|
|
| 244 |
// Person, stage 2 |
// Person, stage 2 |
| 245 |
$form['person']['#type'] = in_array($form['page']['#value'], array(2)) ? 'fieldset' : NULL; |
multipage_form_set_element_visibility($form['person'], in_array($form['page']['#value'], array(2))); |
| 246 |
$form['person']['fav_person']['#type'] = in_array($form['page']['#value'], array(2)) ? 'textfield' : 'hidden'; |
multipage_form_set_element_visibility($form['person']['fav_person'], in_array($form['page']['#value'], array(2))); |
| 247 |
$form['person']['fav_person']['#required'] = in_array($form['page']['#value'], array(2)) ? 1 : 0; |
multipage_form_set_element_visibility($form['person']['fav_person_desc'], in_array($form['page']['#value'], array(2))); |
| 248 |
$form['person']['fav_person_desc']['#type'] = in_array($form['page']['#value'], array(2)) ? 'textarea' : 'hidden'; |
multipage_form_set_element_visibility($form['person']['fav_gummi'], in_array($form['page']['#value'], array(2))); |
|
$form['person']['fav_person_desc']['#required'] = in_array($form['page']['#value'], array(2)) ? 1 : 0; |
|
|
$form['person']['fav_gummi']['#type'] = in_array($form['page']['#value'], array(2)) ? 'checkbox' : 'hidden'; |
|
| 249 |
|
|
| 250 |
// Color and number, page 3 |
// Color and number, page 3 |
| 251 |
$form['fav_color']['#type'] = in_array($form['page']['#value'], array(3)) ? 'select' : 'hidden'; |
multipage_form_set_element_visibility($form['fav_color'], in_array($form['page']['#value'], array(3))); |
| 252 |
$form['fav_color']['#required'] = in_array($form['page']['#value'], array(3)) ? 1 : 0; |
multipage_form_set_element_visibility($form['fav_number'], in_array($form['page']['#value'], array(3))); |
|
$form['fav_number']['#type'] = in_array($form['page']['#value'], array(3)) ? 'textfield' : 'hidden'; |
|
|
$form['fav_number']['#required'] = in_array($form['page']['#value'], array(3)) ? 1 : 0; |
|
| 253 |
|
|
| 254 |
// Movie and tv show, page 4 |
// Movie and tv show, page 4 |
| 255 |
$form['fav_movie']['#type'] = in_array($form['page']['#value'], array(4)) ? 'textfield' : 'hidden'; |
multipage_form_set_element_visibility($form['fav_movie'], in_array($form['page']['#value'], array(4))); |
| 256 |
$form['fav_movie']['#required'] = in_array($form['page']['#value'], array(4)) ? 1 : 0; |
// This bit of trickery is because radios is a bit of a special case -- |
| 257 |
// this bit of trickery is because radios is a bit of a special case -- it's an 'expanding' form element, |
// it's an 'expanding' form element, which means that the multiple buttons |
| 258 |
// which means that the multiple buttons get built by form_builder. Because the building for display actually |
// get built by form_builder. Because the building for display actually |
| 259 |
// happens when the form is still set to stage 3, we have to build it as a radios type in stage 3, or nothing will |
// happens when the form is still set to stage 3, we have to build it as a |
| 260 |
// display on stage 4! form_hide_elements is a little helper function that recurses through and hides |
// radios type in stage 3, or nothing will display on stage 4! |
| 261 |
// all of the buttons we built if we don't actually happen to be displaying stage 4. whew! |
// form_hide_elements is a little helper function that recurses through and |
| 262 |
$form['fav_tv']['#type'] = in_array($form['page']['#value'], array(4)) ? 'radios' : ($next_page ? hide_form_elements($form['fav_tv']) : 'radios'); |
// hides all of the buttons we built if we don't actually happen to be |
| 263 |
$form['fav_tv']['#required'] = in_array($form['page']['#value'], array(4)) ? 1 : 0; |
// displaying stage 4. whew! |
| 264 |
|
if ($next_page) { |
| 265 |
|
multipage_form_set_element_visibility($form['fav_tv'], in_array($form['page']['#value'], array(4))); |
| 266 |
|
} |
| 267 |
|
|
| 268 |
// The progress display for stuff we've already entered |
// The progress display for stuff we've already entered |
| 269 |
$person_display = t('Your favorite person is %person, and they %like gummi bears.<br \>', array('%person' => check_plain($form_values['fav_person']), '%like' => ($form_values['fav_gummi'] ? t('like') : t('don\'t like')))); |
$person_display = t('Your favorite person is %person, and they %like gummi bears.<br \>', array('%person' => check_plain($form_values['fav_person']), '%like' => ($form_values['fav_gummi'] ? t('like') : t('don\'t like')))); |
| 276 |
// the last chance we have. Notice that the type gets set to 'value' and not 'hidden', b/c hidden will screw w/ |
// the last chance we have. Notice that the type gets set to 'value' and not 'hidden', b/c hidden will screw w/ |
| 277 |
// $_POST['op'] |
// $_POST['op'] |
| 278 |
if ($next_page) { |
if ($next_page) { |
| 279 |
$form['back']['#type'] = in_array($form['page']['#value'], array(2,3,4)) ? 'button' : 'value'; |
multipage_form_set_element_visibility($form['back'], in_array($form['page']['#value'], array(2, 3, 4))); |
| 280 |
$form['preview']['#type'] = in_array($form['page']['#value'], array(4)) ? 'button' : 'value'; |
multipage_form_set_element_visibility($form['preview'], in_array($form['page']['#value'], array(4))); |
| 281 |
$form['submit']['#type'] = in_array($form['page']['#value'], array(4)) ? 'submit' : 'button'; |
multipage_form_set_element_visibility($form['submit'], in_array($form['page']['#value'], array(4))); |
| 282 |
$submit_text = array(NULL, t('Next (person)'), t('Next (color/number)'), t('Next (tv/movie)'), t('Submit')); |
$submit_text = array(NULL, t('Next (person)'), t('Next (color/number)'), t('Next (tv/movie)'), t('Submit')); |
| 283 |
$form['submit']['#value'] = $submit_text[$form['page']['#value']]; |
$form['submit']['#value'] = $submit_text[$form['page']['#value']]; |
| 284 |
} |
} |
| 285 |
} |
} |
| 286 |
} |
} |
| 342 |
* way, style sheets can modify the output without requiring theme code. |
* way, style sheets can modify the output without requiring theme code. |
| 343 |
*/ |
*/ |
| 344 |
function theme_multipage_form_example($node) { |
function theme_multipage_form_example($node) { |
|
|
|
| 345 |
$person_display = t('Your favorite person is %person, and they %like gummi bears. <br \>', array('%person' => check_plain($node->fav_person), '%like' => ($node->fav_gummi ? t('like') : t('don\'t like')))); |
$person_display = t('Your favorite person is %person, and they %like gummi bears. <br \>', array('%person' => check_plain($node->fav_person), '%like' => ($node->fav_gummi ? t('like') : t('don\'t like')))); |
| 346 |
$color_number_display = t('Your favorite color is %color, and your favorite number is %number. <br \>', array('%color' => check_plain($node->fav_color), '%number' => check_plain($node->fav_number))); |
$color_number_display = t('Your favorite color is %color, and your favorite number is %number. <br \>', array('%color' => check_plain($node->fav_color), '%number' => check_plain($node->fav_number))); |
| 347 |
$tv_movie_display = t('Your favorite movie is %movie, and you watch your favorite tv show %watch.', array('%movie' => check_plain($node->fav_movie), '%watch' => check_plain($node->fav_tv))); |
$tv_movie_display = t('Your favorite movie is %movie, and you watch your favorite tv show %watch.', array('%movie' => check_plain($node->fav_movie), '%watch' => check_plain($node->fav_tv))); |
| 349 |
$output = '<div class="multipage_form_example">'; |
$output = '<div class="multipage_form_example">'; |
| 350 |
$output .= $person_display . $color_number_display . $tv_movie_display; |
$output .= $person_display . $color_number_display . $tv_movie_display; |
| 351 |
$output .= '</div>'; |
$output .= '</div>'; |
| 352 |
|
|
| 353 |
return $output; |
return $output; |
| 354 |
} |
} |
| 355 |
|
|
| 356 |
/** |
/** |
| 357 |
* A little helper function to hide the child elements of 'radios'. |
* A little helper function to hide the child elements of 'radios'. |
| 358 |
*/ |
*/ |
| 359 |
function hide_form_elements(&$element) { |
function multipage_form_set_element_visibility(&$element, $visible) { |
| 360 |
|
multipage_form_restore_attributes($element); |
| 361 |
|
if (!$visible) { |
| 362 |
|
switch ($element['#type']) { |
| 363 |
|
case 'textfield': |
| 364 |
|
case 'textarea': |
| 365 |
|
case 'radios': |
| 366 |
|
case 'select': |
| 367 |
|
multipage_form_set_attribute($element, '#type', 'hidden'); |
| 368 |
|
multipage_form_set_attribute($element, '#required', FALSE); |
| 369 |
|
break; |
| 370 |
|
|
| 371 |
|
case 'radio': |
| 372 |
|
case 'checkbox': |
| 373 |
|
multipage_form_set_attribute($element, '#type', 'hidden'); |
| 374 |
|
break; |
| 375 |
|
|
| 376 |
|
case 'fieldset': |
| 377 |
|
multipage_form_set_attribute($element, '#type', NULL); |
| 378 |
|
break; |
| 379 |
|
|
| 380 |
|
case 'button': |
| 381 |
|
multipage_form_set_attribute($element, '#type', 'value'); |
| 382 |
|
break; |
| 383 |
|
|
| 384 |
|
case 'submit': |
| 385 |
|
multipage_form_set_attribute($element, '#type', 'button'); |
| 386 |
|
break; |
| 387 |
|
} |
| 388 |
|
} |
| 389 |
|
|
| 390 |
foreach (element_children($element) as $key) { |
foreach (element_children($element) as $key) { |
| 391 |
$element[$key]['#type'] = 'hidden'; |
multipage_form_set_element_visibility($element[$key], $visible); |
| 392 |
|
} |
| 393 |
|
} |
| 394 |
|
|
| 395 |
|
/** |
| 396 |
|
* Set an attribute on an element array with storing the previous value or |
| 397 |
|
* restore to a previous value. |
| 398 |
|
* |
| 399 |
|
* @param $element |
| 400 |
|
* The form element to modify. |
| 401 |
|
* @param $attribute |
| 402 |
|
* An attribute of the form element. |
| 403 |
|
* @param $new_value |
| 404 |
|
* The new value for the attribute to be set with; NULL to restore a previous |
| 405 |
|
* value. |
| 406 |
|
*/ |
| 407 |
|
function multipage_form_set_attribute(&$element, $key, $new_value) { |
| 408 |
|
$element['#multipage_form_original_'. $key] = $element[$key]; |
| 409 |
|
$element[$key] = $new_value; |
| 410 |
|
} |
| 411 |
|
|
| 412 |
|
function multipage_form_restore_attributes(&$element) { |
| 413 |
|
foreach (array_filter(array_keys($element), create_function('$key', 'return (strpos($key, "#multipage_form_original_") === 0);')) as $key) { |
| 414 |
|
$element[str_replace('#multipage_form_original_', '', $key)] = $element[$key]; |
| 415 |
} |
} |
|
return 'hidden'; |
|
| 416 |
} |
} |
| 417 |
|
|
| 418 |
/** |
/** |
| 434 |
return $element; |
return $element; |
| 435 |
} |
} |
| 436 |
|
|
| 437 |
function theme_hidden_array ($element) { |
function theme_hidden_array($element) { |
| 438 |
return $element['#children']; |
return $element['#children']; |
| 439 |
} |
} |