/[drupal]/contributions/modules/ahah_helper/ahah_helper_demo.module
ViewVC logotype

Contents of /contributions/modules/ahah_helper/ahah_helper_demo.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Sat Feb 14 15:48:36 2009 UTC (9 months, 1 week ago) by wimleers
Branch: MAIN
CVS Tags: DRUPAL-6--2-0, HEAD
Changes since 1.3: +112 -50 lines
File MIME type: text/x-php
Rewrite of ahah_helper module. Now uses core-style AHAH rendering, but with some edge cases handled that core doesn't take care of. The example has been completely revised and now features a small tutorial. Most of this work was again sponsored by Mollom.
1 <?php
2 // $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 //----------------------------------------------------------------------------
36 // Drupal core hooks.
37
38 /**
39 * Implementation of hook_menu().
40 */
41 function ahah_helper_demo_menu() {
42 $items['ahah_helper_demo'] = array(
43 'page callback' => 'drupal_get_form',
44 'page arguments' => array('ahah_helper_demo_form'),
45 'access callback' => TRUE,
46 'type' => MENU_CALLBACK,
47 );
48 return $items;
49 }
50
51
52 //----------------------------------------------------------------------------
53 // Forms API callbacks.
54
55 /**
56 * Form definition; ahah_helper_demo form.
57 */
58 function ahah_helper_demo_form($form_state) {
59 $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);
67
68 // Determine the default value of the 'usage' select. When nothing is stored
69 // in $form_state['storage'] yet, it's the form hasn't been submitted yet,
70 // thus it's the first time the form is being displayed. Then, we set the
71 // 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(
80 '#type' => 'fieldset',
81 '#title' => t('Billing information'),
82 '#prefix' => '<div id="billing-info-wrapper">', // This is our wrapper div.
83 '#suffix' => '</div>',
84 '#tree' => TRUE, // Don't forget to set #tree!
85 );
86 $form['billing_info']['usage'] = array(
87 '#type' => 'select',
88 '#title' => t('Usage'),
89 '#options' => array(
90 'private' => t('Private'),
91 'company' => t('Company'),
92 ),
93 '#default_value' => $usage_default_value,
94 '#ahah' => array(
95 'event' => 'change',
96 // This is the "magical path". Note that the parameter is an array of
97 // the parents of the form item of the wrapper div!
98 'path' => ahah_helper_path(array('billing_info')),
99 'wrapper' => 'billing-info-wrapper',
100 ),
101 );
102 $form['billing_info']['update_usage'] = array(
103 '#type' => 'submit',
104 '#value' => t('Update usage'),
105 // Note that we can simply use the generic submit callback provided by the
106 // ahah_helper module here!
107 // All it does, is set $form_state['rebuild'] = TRUE.
108 '#submit' => array('ahah_helper_generic_submit'),
109 // We set the 'no-js' class, which means this submit button will be hidden
110 // automatically by Drupal if JS is enabled.
111 '#attributes' => array('class' => 'no-js'),
112 );
113
114
115 // If 'company' is selected, then these two form items will be displayed.
116 if ($usage_default_value == 'company') {
117 $form['billing_info']['company_name'] = array(
118 '#type' => 'textfield',
119 '#title' => t('Company name'),
120 '#required' => TRUE,
121 '#size' => 20,
122 '#maxlength' => 255,
123 // If the user switched to Private usage and back to Company usage, we
124 // remembered his company's name!
125 '#default_value' => $form_state['storage']['billing_info']['company_name'],
126 );
127 $form['billing_info']['vat'] = array(
128 '#type' => 'textfield',
129 '#title' => t('VAT number'),
130 '#description' => t('Please enter a Belgian VAT number, the format is: <em>BE0999999999</em>.'),
131 '#size' => 20,
132 '#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(
137 'event' => 'blur',
138 'path' => ahah_helper_path(array('billing_info', 'vat')),
139 'wrapper' => 'edit-billing-info-vat-wrapper',
140 'effect' => 'none',
141 'method' => 'replace',
142 ),
143 );
144 // Provide instantaneous (#ahah-powered) feedback to the user about the
145 // 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.
151 else {
152 $form['billing_info']['first_name'] = array(
153 '#type' => 'textfield',
154 '#title' => t('First name'),
155 '#required' => TRUE,
156 '#size' => 20,
157 '#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(
163 '#type' => 'textfield',
164 '#title' => t('Last name'),
165 '#required' => TRUE,
166 '#size' => 20,
167 '#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(
175 '#type' => 'textfield',
176 '#title' => t('Address'),
177 '#required' => TRUE,
178 '#size' => 20,
179 '#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(
184 '#type' => 'textfield',
185 '#title' => t('Country'),
186 '#size' => 20,
187 '#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(
193 '#type' => 'submit',
194 '#value' => t('Save'),
195 );
196
197 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) {
204 // Check the VAT number if:
205 // - the form item is being displayed, and
206 // - a VAT number has been entered (it's not a required form item)
207 if (isset($form['billing_info']['vat']) && strlen($form_state['values']['billing_info']['vat']) > 0) {
208 // 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) {
219 drupal_set_message('Congratulations, you entered valid data. Unfortunately, nothing was saved because this is a demo.');
220 }

  ViewVC Help
Powered by ViewVC 1.1.2