1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* The default format for adresses.
|
6
|
*/
|
7
|
|
8
|
$plugin = array(
|
9
|
'title' => t('Address form (country-specific)'),
|
10
|
'format callback' => 'addressfield_format_address_generate',
|
11
|
'type' => 'address',
|
12
|
'weight' => -100,
|
13
|
);
|
14
|
|
15
|
/**
|
16
|
* Format callback.
|
17
|
*
|
18
|
* @see CALLBACK_addressfield_format_callback()
|
19
|
*/
|
20
|
function addressfield_format_address_generate(&$format, $address, $context = array()) {
|
21
|
// Load the predefined address format for the selected country.
|
22
|
module_load_include('inc', 'addressfield', 'addressfield.address_formats');
|
23
|
$address_format = addressfield_get_address_format($address['country']);
|
24
|
|
25
|
// Used to move certain fields to their own row.
|
26
|
$clearfix = '<div class="clearfix"></div>';
|
27
|
|
28
|
// The street block.
|
29
|
$format['street_block'] = array(
|
30
|
'#type' => 'addressfield_container',
|
31
|
'#attributes' => array(
|
32
|
'class' => array('street-block'),
|
33
|
),
|
34
|
'#weight' => 0,
|
35
|
);
|
36
|
$format['street_block']['thoroughfare'] = array(
|
37
|
'#title' => t('Address 1'),
|
38
|
'#tag' => 'div',
|
39
|
'#attributes' => array(
|
40
|
'class' => array('thoroughfare'),
|
41
|
'autocomplete' => 'address-line1',
|
42
|
),
|
43
|
'#size' => 30,
|
44
|
'#maxlength' => 255,
|
45
|
'#required' => TRUE,
|
46
|
);
|
47
|
$format['street_block']['premise'] = array(
|
48
|
'#title' => t('Address 2'),
|
49
|
'#tag' => 'div',
|
50
|
'#attributes' => array(
|
51
|
'class' => array('premise'),
|
52
|
'autocomplete' => 'address-line2',
|
53
|
),
|
54
|
'#size' => 30,
|
55
|
'#maxlength' => 255,
|
56
|
);
|
57
|
$format['locality_block'] = array(
|
58
|
'#type' => 'addressfield_container',
|
59
|
'#attributes' => array(
|
60
|
'class' => array('addressfield-container-inline', 'locality-block', 'country-' . $address['country']),
|
61
|
),
|
62
|
'#weight' => 50,
|
63
|
'#maxlength' => 255,
|
64
|
);
|
65
|
$format['locality_block']['#attached']['css'][] = drupal_get_path('module', 'addressfield') . '/addressfield.css';
|
66
|
$format['locality_block']['postal_code'] = array(
|
67
|
'#title' => $address_format['postal_code_label'],
|
68
|
'#required' => in_array('postal_code', $address_format['required_fields']),
|
69
|
'#access' => in_array('postal_code', $address_format['used_fields']),
|
70
|
'#size' => 10,
|
71
|
'#maxlength' => 255,
|
72
|
'#attributes' => array(
|
73
|
'class' => array('postal-code'),
|
74
|
'autocomplete' => 'postal-code',
|
75
|
),
|
76
|
);
|
77
|
$format['locality_block']['dependent_locality'] = array(
|
78
|
'#title' => $address_format['dependent_locality_label'],
|
79
|
'#required' => in_array('dependent_locality', $address_format['required_fields']),
|
80
|
'#access' => in_array('dependent_locality', $address_format['used_fields']),
|
81
|
'#size' => 25,
|
82
|
'#maxlength' => 255,
|
83
|
'#tag' => 'div',
|
84
|
'#attributes' => array(
|
85
|
'class' => array('dependent-locality'),
|
86
|
'autocomplete' => '"address-level3',
|
87
|
),
|
88
|
// Most formats place this field in its own row.
|
89
|
'#suffix' => $clearfix,
|
90
|
);
|
91
|
$format['locality_block']['locality'] = array(
|
92
|
'#title' => $address_format['locality_label'],
|
93
|
'#required' => in_array('locality', $address_format['required_fields']),
|
94
|
'#access' => in_array('locality', $address_format['used_fields']),
|
95
|
'#size' => 30,
|
96
|
'#maxlength' => 255,
|
97
|
'#prefix' => ' ',
|
98
|
'#attributes' => array(
|
99
|
'class' => array('locality'),
|
100
|
'autocomplete' => '"address-level2',
|
101
|
),
|
102
|
);
|
103
|
$format['locality_block']['administrative_area'] = array(
|
104
|
'#title' => $address_format['administrative_area_label'],
|
105
|
'#required' => in_array('administrative_area', $address_format['required_fields']),
|
106
|
'#access' => in_array('administrative_area', $address_format['used_fields']),
|
107
|
'#empty_value' => '',
|
108
|
'#size' => 10,
|
109
|
'#maxlength' => 255,
|
110
|
'#prefix' => ' ',
|
111
|
'#render_option_value' => $address_format['render_administrative_area_value'],
|
112
|
'#attributes' => array(
|
113
|
'class' => array('state'),
|
114
|
'autocomplete' => 'address-level1',
|
115
|
),
|
116
|
);
|
117
|
$format['country'] = array(
|
118
|
'#title' => t('Country'),
|
119
|
'#options' => _addressfield_country_options_list(),
|
120
|
'#render_option_value' => TRUE,
|
121
|
'#required' => TRUE,
|
122
|
'#attributes' => array(
|
123
|
'class' => array('country'),
|
124
|
'autocomplete' => 'country',
|
125
|
),
|
126
|
'#weight' => 100,
|
127
|
);
|
128
|
|
129
|
if (empty($format['locality_block']['postal_code']['#access'])) {
|
130
|
// Remove the prefix from the first widget of the block.
|
131
|
$element_children = element_children($format['locality_block']);
|
132
|
$first_child = reset($element_children);
|
133
|
unset($format['locality_block'][$first_child]['#prefix']);
|
134
|
}
|
135
|
|
136
|
if (!empty($format['locality_block']['administrative_area']['#access'])) {
|
137
|
// Set the predefined administrative areas, if found.
|
138
|
module_load_include('inc', 'addressfield', 'addressfield.administrative_areas');
|
139
|
$administrative_areas = addressfield_get_administrative_areas($address['country']);
|
140
|
$format['locality_block']['administrative_area']['#options'] = $administrative_areas;
|
141
|
}
|
142
|
|
143
|
// Country-specific customizations.
|
144
|
if (in_array($address['country'], array('AU', 'EE', 'LT', 'LV', 'NZ', 'RU'))) {
|
145
|
// These countries don't use the "Address 2" line.
|
146
|
// Leave it as a precaution, but hide the label to avoid confusing users.
|
147
|
$format['street_block']['thoroughfare']['#title'] = t('Address');
|
148
|
$format['street_block']['premise']['#title_display'] = 'invisible';
|
149
|
}
|
150
|
elseif ($address['country'] == 'US') {
|
151
|
if ($context['mode'] == 'render') {
|
152
|
$format['locality_block']['locality']['#suffix'] = ',';
|
153
|
}
|
154
|
}
|
155
|
elseif ($address['country'] == 'BR') {
|
156
|
$format['locality_block']['dependent_locality']['#suffix'] = $clearfix;
|
157
|
$format['locality_block']['dependent_locality']['#tag'] = 'div';
|
158
|
$format['locality_block']['administrative_area']['#suffix'] = $clearfix;
|
159
|
$format['locality_block']['postal_code']['#tag'] = 'div';
|
160
|
// Change some titles to make translation easier.
|
161
|
$format['street_block']['#attributes'] = array(
|
162
|
'class' => array('addressfield-container-inline'),
|
163
|
);
|
164
|
$format['street_block']['thoroughfare']['#title'] = t('Thoroughfare');
|
165
|
$format['street_block']['thoroughfare']['#tag'] = NULL;
|
166
|
$format['street_block']['premise'] = array(
|
167
|
'#title' => t('Complement'),
|
168
|
'#tag' => NULL,
|
169
|
'#attributes' => array('class' => array('premise')),
|
170
|
'#size' => 20,
|
171
|
'#maxlength' => 255,
|
172
|
'#prefix' => ', ',
|
173
|
);
|
174
|
$format['locality_block']['locality']['#suffix'] = ' - ';
|
175
|
// Hide suffixes and prefixes while in form.
|
176
|
if ($context['mode'] == 'form') {
|
177
|
$format['street_block']['premise']['#prefix'] = NULL;
|
178
|
$format['street_block']['premise']['#suffix'] = NULL;
|
179
|
$format['locality_block']['locality']['#suffix'] = NULL;
|
180
|
}
|
181
|
}
|
182
|
elseif ($address['country'] == 'CN') {
|
183
|
$format['locality_block']['locality']['#suffix'] = $clearfix;
|
184
|
}
|
185
|
elseif ($address['country'] == 'CA') {
|
186
|
if ($context['mode'] == 'render') {
|
187
|
$format['locality_block']['locality']['#suffix'] = ',';
|
188
|
}
|
189
|
}
|
190
|
elseif ($address['country'] == 'GB') {
|
191
|
$format['locality_block']['administrative_area']['#size'] = '30';
|
192
|
}
|
193
|
elseif ($address['country'] == 'ID') {
|
194
|
$format['locality_block']['administrative_area']['#weight'] = 1;
|
195
|
}
|
196
|
elseif ($address['country'] == 'JP') {
|
197
|
$format['locality_block']['#weight'] = 10;
|
198
|
$format['locality_block']['postal_code']['#weight'] = 10;
|
199
|
$format['locality_block']['postal_code']['#tag'] = 'div';
|
200
|
$format['locality_block']['postal_code']['#size'] = 30;
|
201
|
$format['locality_block']['administrative_area']['#weight'] = 20;
|
202
|
$format['locality_block']['administrative_area']['#size'] = 30;
|
203
|
$format['locality_block']['locality']['#weight'] = 30;
|
204
|
$format['street_block']['#weight'] = 20;
|
205
|
}
|
206
|
elseif ($address['country'] == 'PE') {
|
207
|
$format['locality_block']['administrative_area']['#weight'] = 1;
|
208
|
$format['locality_block']['locality']['#weight'] = 2;
|
209
|
}
|
210
|
elseif ($address['country'] == 'PH' || $address['country'] == 'TH') {
|
211
|
$format['locality_block']['dependent_locality']['#suffix'] = '';
|
212
|
$format['locality_block']['locality']['#suffix'] = $clearfix;
|
213
|
}
|
214
|
|
215
|
// These countries show every field in its own row.
|
216
|
$countries_field_per_row = array(
|
217
|
'AM', 'EG', 'FK', 'GB', 'GG', 'GS', 'HK', 'HU', 'IE', 'IM', 'IO', 'JE', 'JM',
|
218
|
'JP', 'KE', 'KR', 'KZ', 'LK', 'PA', 'PN', 'RU', 'SC', 'SH', 'SZ', 'TC', 'UA',
|
219
|
'VG', 'ZA',
|
220
|
);
|
221
|
if (in_array($address['country'], $countries_field_per_row)) {
|
222
|
$format['locality_block']['#attributes']['class'][0] = 'addressfield-container';
|
223
|
$format['locality_block']['postal_code']['#prefix'] = '';
|
224
|
$format['locality_block']['postal_code']['#tag'] = 'div';
|
225
|
$format['locality_block']['locality']['#prefix'] = '';
|
226
|
$format['locality_block']['locality']['#tag'] = 'div';
|
227
|
$format['locality_block']['administrative_area']['#prefix'] = '';
|
228
|
$format['locality_block']['administrative_area']['#tag'] = 'div';
|
229
|
}
|
230
|
|
231
|
// These countries tend to put the postal code after the locality.
|
232
|
$countries_postal_code_after_locality = array(
|
233
|
'AS', 'AU', 'BD', 'BF', 'BH', 'BM', 'BN', 'BR', 'BT', 'CA', 'CC', 'CN', 'CX',
|
234
|
'EG', 'FK', 'FM', 'GB', 'GG', 'GS', 'GU', 'HN', 'HU', 'ID', 'IL', 'IM', 'IN',
|
235
|
'IO', 'IQ', 'IR', 'JE', 'JO', 'JP', 'KE', 'KH', 'KR', 'LB', 'LK', 'LS', 'LV',
|
236
|
'MH', 'MM', 'MN', 'MP', 'MT', 'MV', 'MX', 'MY', 'NF', 'NG', 'NP', 'NZ', 'PG',
|
237
|
'PH', 'PK', 'PN', 'PR', 'PW', 'RU', 'SA', 'SH', 'SO', 'SZ', 'TC', 'TH', 'TW',
|
238
|
'UA', 'UM', 'US', 'VE', 'VI', 'VG', 'VN', 'ZA',
|
239
|
);
|
240
|
if (in_array($address['country'], $countries_postal_code_after_locality)) {
|
241
|
// Take the widget out of the array.
|
242
|
$postal_code_widget = $format['locality_block']['postal_code'];
|
243
|
$postal_code_widget['#prefix'] = ' ';
|
244
|
unset($format['locality_block']['postal_code']);
|
245
|
|
246
|
// Add it back.
|
247
|
$format['locality_block']['postal_code'] = $postal_code_widget;
|
248
|
|
249
|
// Remove the prefix from the first widget of the block.
|
250
|
$element_children = element_children($format['locality_block']);
|
251
|
$first_child = reset($element_children);
|
252
|
unset($format['locality_block'][$first_child]['#prefix']);
|
253
|
}
|
254
|
|
255
|
if ($context['mode'] == 'form') {
|
256
|
// Provide a wrapper ID for AJAX replacement based on country selection.
|
257
|
if (!isset($format['#wrapper_id'])) {
|
258
|
$format['#wrapper_id'] = drupal_html_id('addressfield-wrapper');
|
259
|
$format['#prefix'] = '<div id="' . $format['#wrapper_id'] . '">';
|
260
|
$format['#suffix'] = '</div>';
|
261
|
}
|
262
|
// AJAX enable it.
|
263
|
$format['country']['#ajax'] = array(
|
264
|
'callback' => 'addressfield_standard_widget_refresh',
|
265
|
'wrapper' => $format['#wrapper_id'],
|
266
|
);
|
267
|
$format['country']['#element_validate'] = array('addressfield_standard_country_validate');
|
268
|
// Don't validate any element when the country is changed.
|
269
|
$format['country']['#limit_validation_errors'] = array();
|
270
|
|
271
|
// Move the country selector to the top of the form.
|
272
|
$format['country']['#weight'] = -500;
|
273
|
// Limit it to the countries supported by the widget.
|
274
|
if (isset($context['field'])) {
|
275
|
$format['country']['#options'] = _addressfield_country_options_list($context['field'], $context['instance']);
|
276
|
}
|
277
|
|
278
|
// The whole field is considered empty if the country column is empty.
|
279
|
// Therefore, if the field is optional, allow the country to be optional.
|
280
|
// The same logic applies if the field allows multiple values, in which case
|
281
|
// only the first delta needs to have a value.
|
282
|
if (empty($context['instance']['required']) || (isset($context['delta']) && $context['delta'] > 0)) {
|
283
|
$format['country']['#required'] = FALSE;
|
284
|
$format['country']['#empty_value'] = '';
|
285
|
// Hide all other fields until the country is selected.
|
286
|
if (empty($address['country'])) {
|
287
|
$format['street_block']['#access'] = FALSE;
|
288
|
$format['locality_block']['#access'] = FALSE;
|
289
|
}
|
290
|
}
|
291
|
}
|
292
|
}
|