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