| 1 |
<?php |
<?php |
| 2 |
// $Id: ahah_helper_demo.module,v 1.2 2008/11/19 16:25:29 wimleers Exp $ |
// $Id: ahah_helper_demo.module,v 1.3 2008/11/20 01:20:34 wimleers Exp $ |
| 3 |
|
|
| 4 |
|
|
| 5 |
|
/** |
| 6 |
|
* Hi, |
| 7 |
|
* |
| 8 |
|
* This is a very brief tutorial of what you need to do to use the ahah_helper |
| 9 |
|
* module. This piece of text is all you need to know, besides knowing the |
| 10 |
|
* Forms API basics already. |
| 11 |
|
* |
| 12 |
|
* This is not a perfect approach to AHAH forms. But then again, the very |
| 13 |
|
* reason this module exists is because Drupal 6's Forms API does an even |
| 14 |
|
* worse job. |
| 15 |
|
* 90% of what you know about Forms API stil applies. A few things are |
| 16 |
|
* different, to make it work perfectly. |
| 17 |
|
* 1) Always call ahah_helper_register() at the beginning of your form. It |
| 18 |
|
* sets $form['#cache'], stores in what file the form definition function |
| 19 |
|
* exists and adds some JS. |
| 20 |
|
* Finally, it stores the last entered value for each form item; this |
| 21 |
|
* allows you to dynamically add and remove form items and still remember |
| 22 |
|
* their last entered value! The last known value for $form['foo']['bar'] |
| 23 |
|
* is stored in $form_state['storage']['foo']['bar]. |
| 24 |
|
* 2) That's it. Well, that and one change in how you write form items: you |
| 25 |
|
* will *always* have to set #default_value, and *always* assign it the |
| 26 |
|
* latest known value. If not, the value will be empty. |
| 27 |
|
* This is an annoyance indeed, but it cannot be worked around easily. See |
| 28 |
|
* the remark at the top of the ahah_helper.module file. |
| 29 |
|
* |
| 30 |
|
* Good luck! |
| 31 |
|
* Wim Leers ~ http://wimleers.com/ |
| 32 |
|
*/ |
| 33 |
|
|
| 34 |
|
|
| 35 |
//---------------------------------------------------------------------------- |
//---------------------------------------------------------------------------- |
| 45 |
'access callback' => TRUE, |
'access callback' => TRUE, |
| 46 |
'type' => MENU_CALLBACK, |
'type' => MENU_CALLBACK, |
| 47 |
); |
); |
|
|
|
| 48 |
return $items; |
return $items; |
| 49 |
} |
} |
| 50 |
|
|
| 53 |
// Forms API callbacks. |
// Forms API callbacks. |
| 54 |
|
|
| 55 |
/** |
/** |
| 56 |
* Form definition; create subscription. |
* Form definition; ahah_helper_demo form. |
| 57 |
*/ |
*/ |
| 58 |
function ahah_helper_demo_form($form_state) { |
function ahah_helper_demo_form($form_state) { |
| 59 |
$form = array(); |
$form = array(); |
| 60 |
|
|
| 61 |
|
// Register the form with ahah_helper so we can use it. Also updates |
| 62 |
|
// $form_state['storage'] to ensure it contains the latest values that have |
| 63 |
|
// been entered, even when the form item has temporarily been removed from |
| 64 |
|
// the form. So if a form item *once* had a value, you *always* can retrieve |
| 65 |
|
// it. |
| 66 |
ahah_helper_register($form, $form_state); |
ahah_helper_register($form, $form_state); |
| 67 |
|
|
| 68 |
// We remember the last company used, to restore it when a user switched to |
// Determine the default value of the 'usage' select. When nothing is stored |
| 69 |
// Personal usage and back to Company usage. |
// in $form_state['storage'] yet, it's the form hasn't been submitted yet, |
| 70 |
if (isset($form_state['values']['billing_info']['company_name'])) { |
// thus it's the first time the form is being displayed. Then, we set the |
| 71 |
$form_state['storage']['billing_info']['company_name'] = $form_state['values']['billing_info']['company_name']; |
// default to 'company'. |
| 72 |
|
if (!isset($form_state['storage']['billing_info']['usage'])) { |
| 73 |
|
$usage_default_value = 'company'; |
| 74 |
|
} |
| 75 |
|
else { |
| 76 |
|
$usage_default_value = $form_state['storage']['billing_info']['usage']; |
| 77 |
} |
} |
| 78 |
|
|
| 79 |
$form['billing_info'] = array( |
$form['billing_info'] = array( |
| 80 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 81 |
'#title' => t('Billing information'), |
'#title' => t('Billing information'), |
| 82 |
'#prefix' => '<div id="billing-info-wrapper">', // This is our wrapper div. |
'#prefix' => '<div id="billing-info-wrapper">', // This is our wrapper div. |
| 83 |
'#suffix' => '</div>', |
'#suffix' => '</div>', |
| 84 |
'#tree' => TRUE, |
'#tree' => TRUE, // Don't forget to set #tree! |
| 85 |
); |
); |
| 86 |
$form['billing_info']['usage'] = array( |
$form['billing_info']['usage'] = array( |
| 87 |
'#type' => 'select', |
'#type' => 'select', |
| 88 |
'#title' => t('Usage'), |
'#title' => t('Usage'), |
| 89 |
'#options' => array( |
'#options' => array( |
| 90 |
'private' => t('Private'), |
'private' => t('Private'), |
| 91 |
'company' => t('Company'), |
'company' => t('Company'), |
| 92 |
), |
), |
| 93 |
'#default_value' => 'company', |
'#default_value' => $usage_default_value, |
| 94 |
'#ahah' => array( |
'#ahah' => array( |
| 95 |
'event' => 'change', |
'event' => 'change', |
| 96 |
// This is the "magical path". Note that the parameter is an array of |
// This is the "magical path". Note that the parameter is an array of |
| 97 |
// the parents of the form item of the wrapper div! |
// the parents of the form item of the wrapper div! |
| 98 |
'path' => ahah_helper_path(array('billing_info')), |
'path' => ahah_helper_path(array('billing_info')), |
| 99 |
'wrapper' => 'billing-info-wrapper', |
'wrapper' => 'billing-info-wrapper', |
| 100 |
), |
), |
| 101 |
); |
); |
| 102 |
$form['billing_info']['update_usage'] = array( |
$form['billing_info']['update_usage'] = array( |
| 103 |
'#type' => 'submit', |
'#type' => 'submit', |
| 104 |
'#value' => t('Update usage'), |
'#value' => t('Update usage'), |
| 105 |
// Note that we can simply use the generic submit callback provided by the |
// Note that we can simply use the generic submit callback provided by the |
| 106 |
// ahah_helper module here! |
// ahah_helper module here! |
| 113 |
|
|
| 114 |
|
|
| 115 |
// If 'company' is selected, then these two form items will be displayed. |
// If 'company' is selected, then these two form items will be displayed. |
| 116 |
if (!isset($form_state['values']) || $form_state['values']['billing_info']['usage'] == 'company') { |
if ($usage_default_value == 'company') { |
| 117 |
$form['billing_info']['company_name'] = array( |
$form['billing_info']['company_name'] = array( |
| 118 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 119 |
'#title' => t('Company name'), |
'#title' => t('Company name'), |
| 120 |
'#required' => TRUE, |
'#required' => TRUE, |
| 121 |
'#size' => 20, |
'#size' => 20, |
| 122 |
'#maxlength' => 255, |
'#maxlength' => 255, |
| 123 |
// If the user switched to Private usage and back to Company usage, we |
// If the user switched to Private usage and back to Company usage, we |
| 124 |
// remembered his company's name! |
// remembered his company's name! |
| 125 |
'#default_value' => (isset($form_state['storage']['billing_info']['company_name'])) ? $form_state['storage']['billing_info']['company_name'] : '', |
'#default_value' => $form_state['storage']['billing_info']['company_name'], |
| 126 |
); |
); |
| 127 |
$form['billing_info']['vat'] = array( |
$form['billing_info']['vat'] = array( |
| 128 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 129 |
'#title' => t('VAT number'), |
'#title' => t('VAT number'), |
| 130 |
'#description' => t('Please enter a Belgian VAT number, the format is: <em>BE0999999999</em>.'), |
'#description' => t('Please enter a Belgian VAT number, the format is: <em>BE0999999999</em>.'), |
| 131 |
'#size' => 20, |
'#size' => 20, |
| 132 |
'#maxlength' => 255, |
'#maxlength' => 255, |
| 133 |
|
// If the user switched to Private usage and back to Company usage, we |
| 134 |
|
// remembered his VAT number! |
| 135 |
|
'#default_value' => $form_state['storage']['billing_info']['vat'], |
| 136 |
'#ahah' => array( |
'#ahah' => array( |
| 137 |
'event' => 'blur', |
'event' => 'blur', |
| 138 |
'path' => ahah_helper_path(array('billing_info', 'vat')), |
'path' => ahah_helper_path(array('billing_info', 'vat')), |
| 141 |
'method' => 'replace', |
'method' => 'replace', |
| 142 |
), |
), |
| 143 |
); |
); |
| 144 |
if (isset($form_state['values']['billing_info']['vat']) && strlen($form_state['values']['billing_info']['vat']) > 0) { |
// Provide instantaneous (#ahah-powered) feedback to the user about the |
| 145 |
$form['billing_info']['vat']['#field_suffix'] = theme('image', (preg_match('/^BE0\d{9}$/', $form_state['values']['billing_info']['vat'])) ? 'misc/watchdog-ok.png' : 'misc/watchdog-error.png'); |
// VAT number he entered. |
| 146 |
|
if (isset($form_state['storage']['billing_info']['vat']) && strlen($form_state['storage']['billing_info']['vat']) > 0) { |
| 147 |
|
$form['billing_info']['vat']['#field_suffix'] = theme('image', (preg_match('/^BE0\d{9}$/', $form_state['storage']['billing_info']['vat'])) ? 'misc/watchdog-ok.png' : 'misc/watchdog-error.png'); |
| 148 |
} |
} |
| 149 |
} |
} |
| 150 |
// And if 'private' is selected, then these two form items will be displayed. |
// And if 'private' is selected, then these two form items will be displayed. |
| 151 |
else { |
else { |
| 152 |
$form['billing_info']['first_name'] = array( |
$form['billing_info']['first_name'] = array( |
| 153 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 154 |
'#title' => t('First name'), |
'#title' => t('First name'), |
| 155 |
'#required' => TRUE, |
'#required' => TRUE, |
| 156 |
'#size' => 20, |
'#size' => 20, |
| 157 |
'#maxlength' => 255, |
'#maxlength' => 255, |
| 158 |
|
// If the user switched to Company usage and back to Private usage, we |
| 159 |
|
// remembered his first name! |
| 160 |
|
'#default_value' => $form_state['storage']['billing_info']['first_name'], |
| 161 |
); |
); |
| 162 |
$form['billing_info']['last_name'] = array( |
$form['billing_info']['last_name'] = array( |
| 163 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 164 |
'#title' => t('Last name'), |
'#title' => t('Last name'), |
| 165 |
'#required' => TRUE, |
'#required' => TRUE, |
| 166 |
'#size' => 20, |
'#size' => 20, |
| 167 |
'#maxlength' => 255, |
'#maxlength' => 255, |
| 168 |
|
// If the user switched to Company usage and back to Private usage, we |
| 169 |
|
// remembered his last name! |
| 170 |
|
'#default_value' => $form_state['storage']['billing_info']['last_name'], |
| 171 |
); |
); |
| 172 |
} |
} |
| 173 |
|
|
| 174 |
$form['billing_info']['address'] = array( |
$form['billing_info']['address'] = array( |
| 175 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 176 |
'#title' => t('Address'), |
'#title' => t('Address'), |
| 177 |
'#required' => TRUE, |
'#required' => TRUE, |
| 178 |
'#size' => 20, |
'#size' => 20, |
| 179 |
'#maxlength' => 255, |
'#maxlength' => 255, |
| 180 |
|
// Always set #default_value, even if it's not a dynamically added form item! |
| 181 |
|
'#default_value' => $form_state['storage']['billing_info']['address'], |
| 182 |
); |
); |
| 183 |
$form['billing_info']['country'] = array( |
$form['billing_info']['country'] = array( |
| 184 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 185 |
'#title' => t('Country'), |
'#title' => t('Country'), |
| 186 |
'#size' => 20, |
'#size' => 20, |
| 187 |
'#maxlength' => 255, |
'#maxlength' => 255, |
| 188 |
|
// Always set #default_value, even if it's not a dynamically added form item! |
| 189 |
|
'#default_value' => $form_state['storage']['billing_info']['country'], |
| 190 |
); |
); |
| 191 |
|
|
| 192 |
$form['save'] = array( |
$form['save'] = array( |
| 193 |
'#type' => 'submit', |
'#type' => 'submit', |
| 194 |
'#value' => t('Save'), |
'#value' => t('Save'), |
| 195 |
); |
); |
| 196 |
|
|
|
|
|
| 197 |
return $form; |
return $form; |
| 198 |
} |
} |
| 199 |
|
|
| 200 |
|
/** |
| 201 |
|
* Validate callback for the ahah_helper_demo form. |
| 202 |
|
*/ |
| 203 |
function ahah_helper_demo_form_validate($form, &$form_state) { |
function ahah_helper_demo_form_validate($form, &$form_state) { |
| 204 |
if (isset($form['billing_info']['vat']) |
// Check the VAT number if: |
| 205 |
&& strlen($form_state['values']['billing_info']['vat']) > 0 |
// - the form item is being displayed, and |
| 206 |
&& !isset($form['billing_info']['vat']['#first_time']) |
// - a VAT number has been entered (it's not a required form item) |
| 207 |
&& !preg_match('/^BE0\d{9}$/', $form_state['values']['billing_info']['vat'])) { |
if (isset($form['billing_info']['vat']) && strlen($form_state['values']['billing_info']['vat']) > 0) { |
| 208 |
form_error($form['billing_info']['vat'], t('Invalid VAT number.')); |
// Check if the entered VAT number is valid in Belgium. |
| 209 |
|
if (!preg_match('/^BE0\d{9}$/', $form_state['values']['billing_info']['vat'])) { |
| 210 |
|
form_error($form['billing_info']['vat'], t('Invalid VAT number.')); |
| 211 |
|
} |
| 212 |
} |
} |
| 213 |
} |
} |
| 214 |
|
|
| 215 |
|
/** |
| 216 |
|
* Submit callback for the ahah_helper_demo form. |
| 217 |
|
*/ |
| 218 |
function ahah_helper_demo_form_submit($form, &$form_state) { |
function ahah_helper_demo_form_submit($form, &$form_state) { |
| 219 |
drupal_set_message('Congratulations, you entered valid data. Unfortunately, nothing was saved because this is a demo.'); |
drupal_set_message('Congratulations, you entered valid data. Unfortunately, nothing was saved because this is a demo.'); |
| 220 |
} |
} |