| 1 |
<?php
|
| 2 |
|
| 3 |
function multipart_menu($may_cache) {
|
| 4 |
$items = array();
|
| 5 |
if ($may_cache) {
|
| 6 |
$items[] = array('path' => 'multipart', 'access' => TRUE, 'title' => t('Multipart form'), 'callback' => 'multipart_page');
|
| 7 |
}
|
| 8 |
return $items;
|
| 9 |
}
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Just a form to demo the multipart.
|
| 13 |
*/
|
| 14 |
function multipart_page() {
|
| 15 |
$form['field1'] = array('#type' => 'textfield', '#title' => 'field1');
|
| 16 |
$form['field2'] = array('#type' => 'textfield', '#title' => 'field2');
|
| 17 |
$form['field3'] = array('#type' => 'textfield', '#title' => 'field3');
|
| 18 |
$form['part']['#tree'] = TRUE;
|
| 19 |
$form['part']['field1a'] = array('#type' => 'textfield', '#title' => 'field1a');
|
| 20 |
$form['part']['field2a'] = array('#type' => 'textfield', '#title' => 'field2a');
|
| 21 |
$form['submit'] = array('#type' => 'submit', '#value' => 'Submit');
|
| 22 |
return drupal_get_form('multipart', $form);
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Dump the values and restart.
|
| 27 |
*/
|
| 28 |
function multipart_submit($form_id, $form_values) {
|
| 29 |
drupal_set_message(var_export($form_values, TRUE));
|
| 30 |
drupal_goto($_GET['q']);
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Here you set which stage has which fields.
|
| 35 |
*/
|
| 36 |
function multipart_fields($stage) {
|
| 37 |
$fields = array(
|
| 38 |
0 => array('field1' => 1, 'part' => array('field1a' => 1)),
|
| 39 |
1 => array('field2' => 1, 'part' => array('field2a' => 1)),
|
| 40 |
2 => array('field3' => 1)
|
| 41 |
);
|
| 42 |
return isset($fields[$stage]) ? $fields[$stage] : array();
|
| 43 |
}
|
| 44 |
|
| 45 |
/**
|
| 46 |
* This function changes the form accordin to multipart_fields.
|
| 47 |
* If you are not calling your submit button 'submit', change it accordingly.
|
| 48 |
*/
|
| 49 |
function multipart_form_alter($form_id, &$form) {
|
| 50 |
if ($form_id == 'multipart') {
|
| 51 |
if (isset($_POST['edit']['stage']) && is_numeric($_POST['edit']['stage'])) {
|
| 52 |
$current_stage = $_POST['edit']['stage'] + 1;
|
| 53 |
}
|
| 54 |
else {
|
| 55 |
$current_stage = 0;
|
| 56 |
}
|
| 57 |
if ($current_fields = multipart_fields($current_stage)) {
|
| 58 |
$current_fields['submit'] = 1;
|
| 59 |
$form['submit']['#type'] = 'button';
|
| 60 |
_multipart_hide($form, $current_fields);
|
| 61 |
}
|
| 62 |
$form['stage'] = array('#type' => 'hidden', '#value' => $current_stage);
|
| 63 |
}
|
| 64 |
}
|
| 65 |
|
| 66 |
/**
|
| 67 |
* This functions walks the form tree and sets non-value elements to hidden.
|
| 68 |
*/
|
| 69 |
function _multipart_hide(&$form, $current_fields) {
|
| 70 |
$children = element_children($form);
|
| 71 |
if (empty($children)) {
|
| 72 |
if (!isset($current_fields) && $form['#type'] != 'value') {
|
| 73 |
$form['#type'] = 'hidden';
|
| 74 |
unset($form['#prefix'], $form['#suffix']);
|
| 75 |
}
|
| 76 |
}
|
| 77 |
else {
|
| 78 |
foreach (element_children($form) as $key) {
|
| 79 |
_multipart_hide($form[$key], isset($current_fields) && isset($current_fields[$key]) ? $current_fields[$key] : NULL);
|
| 80 |
}
|
| 81 |
}
|
| 82 |
}
|