Projet

Général

Profil

Paste
Télécharger (11,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / addressfield / plugins / format / address.inc @ 9d13637e

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
    '#render_option_value' => $address_format['render_administrative_area_value'],
108
    '#attributes' => array(
109
      'class' => array('state'),
110
      'x-autocompletetype' => 'region',
111
      'autocomplete' => 'region',
112
    ),
113
  );
114
  $format['country'] = array(
115
    '#title' => t('Country'),
116
    '#options' => _addressfield_country_options_list(),
117
    '#render_option_value' => TRUE,
118
    '#required' => TRUE,
119
    '#attributes' => array(
120
      'class' => array('country'),
121
      'x-autocompletetype' => 'country',
122
      'autocomplete' => 'country',
123
    ),
124
    '#weight' => 100,
125
  );
126

    
127
  if (empty($format['locality_block']['postal_code']['#access'])) {
128
    // Remove the prefix from the first widget of the block.
129
    $element_children = element_children($format['locality_block']);
130
    $first_child = reset($element_children);
131
    unset($format['locality_block'][$first_child]['#prefix']);
132
  }
133

    
134
  if (!empty($format['locality_block']['administrative_area']['#access'])) {
135
    // Set the predefined administrative areas, if found.
136
    module_load_include('inc', 'addressfield', 'addressfield.administrative_areas');
137
    $administrative_areas = addressfield_get_administrative_areas($address['country']);
138
    $format['locality_block']['administrative_area']['#options'] = $administrative_areas;
139
  }
140

    
141
  // Country-specific customizations.
142
  if (in_array($address['country'], array('AU', 'EE', 'LT', 'LV', 'NZ', 'RU'))) {
143
    // These countries don't use the "Address 2" line.
144
    // Leave it as a precaution, but hide the label to avoid confusing users.
145
    $format['street_block']['thoroughfare']['#title'] = t('Address');
146
    $format['street_block']['premise']['#title_display'] = 'invisible';
147
  }
148
  elseif ($address['country'] == 'US') {
149
    if ($context['mode'] == 'render') {
150
      $format['locality_block']['locality']['#suffix'] = ',';
151
    }
152
  }
153
  elseif ($address['country'] == 'BR') {
154
    $format['locality_block']['dependent_locality']['#suffix'] = $clearfix;
155
    $format['locality_block']['dependent_locality']['#tag'] = 'div';
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']['#weight'] = 1;
192
  }
193
  elseif ($address['country'] == 'JP') {
194
    $format['locality_block']['#weight'] = 10;
195
    $format['locality_block']['postal_code']['#weight'] = 10;
196
    $format['locality_block']['postal_code']['#tag'] = 'div';
197
    $format['locality_block']['postal_code']['#size'] = 30;
198
    $format['locality_block']['administrative_area']['#weight'] = 20;
199
    $format['locality_block']['administrative_area']['#size'] = 30;
200
    $format['locality_block']['locality']['#weight'] = 30;
201
    $format['street_block']['#weight'] = 20;
202
  }
203
  elseif ($address['country'] == 'PE') {
204
    $format['locality_block']['administrative_area']['#weight'] = 1;
205
    $format['locality_block']['locality']['#weight'] = 2;
206
  }
207
  elseif ($address['country'] == 'PH' || $address['country'] == 'TH') {
208
    $format['locality_block']['dependent_locality']['#suffix'] = '';
209
    $format['locality_block']['locality']['#suffix'] = $clearfix;
210
  }
211

    
212
  // These countries show every field in its own row.
213
  $countries_field_per_row = array(
214
    'AM', 'EG', 'FK', 'GB', 'GG', 'GS', 'HK', 'HU', 'IE', 'IM', 'IO', 'JE', 'JM',
215
    'JP', 'KE', 'KR', 'KZ', 'LK', 'PA', 'PN', 'RU', 'SC', 'SH', 'SZ', 'TC', 'UA',
216
    'VG', 'ZA',
217
  );
218
  if (in_array($address['country'], $countries_field_per_row)) {
219
    $format['locality_block']['#attributes']['class'][0] = 'addressfield-container';
220
    $format['locality_block']['postal_code']['#prefix'] = '';
221
    $format['locality_block']['postal_code']['#tag'] = 'div';
222
    $format['locality_block']['locality']['#prefix'] = '';
223
    $format['locality_block']['locality']['#tag'] = 'div';
224
    $format['locality_block']['administrative_area']['#prefix'] = '';
225
    $format['locality_block']['administrative_area']['#tag'] = 'div';
226
  }
227

    
228
  // These countries tend to put the postal code after the locality.
229
  $countries_postal_code_after_locality = array(
230
    'AS', 'AU', 'BD', 'BF', 'BH', 'BM', 'BN', 'BR', 'BT', 'CA', 'CC', 'CN', 'CX',
231
    'EG', 'FK', 'FM', 'GB', 'GG', 'GS', 'GU', 'HN', 'HU', 'ID', 'IL', 'IM', 'IN',
232
    'IO', 'IQ', 'IR', 'JE', 'JO', 'JP', 'KE', 'KH', 'KR', 'LB', 'LK', 'LS', 'LV',
233
    'MH', 'MM', 'MN', 'MP', 'MT', 'MV', 'MX', 'MY', 'NF', 'NG', 'NP', 'NZ', 'PG',
234
    'PH', 'PK', 'PN', 'PR', 'PW', 'RU', 'SA', 'SH', 'SO', 'SZ', 'TC', 'TH', 'TW',
235
    'UA', 'UM', 'US', 'VE', 'VI', 'VG', 'VN', 'ZA',
236
  );
237
  if (in_array($address['country'], $countries_postal_code_after_locality)) {
238
    // Take the widget out of the array.
239
    $postal_code_widget = $format['locality_block']['postal_code'];
240
    $postal_code_widget['#prefix'] = ' ';
241
    unset($format['locality_block']['postal_code']);
242

    
243
    // Add it back.
244
    $format['locality_block']['postal_code'] = $postal_code_widget;
245

    
246
    // Remove the prefix from the first widget of the block.
247
    $element_children = element_children($format['locality_block']);
248
    $first_child = reset($element_children);
249
    unset($format['locality_block'][$first_child]['#prefix']);
250
  }
251

    
252
  if ($context['mode'] == 'form') {
253
    // Provide a wrapper ID for AJAX replacement based on country selection.
254
    if (!isset($format['#wrapper_id'])) {
255
      $format['#wrapper_id'] = drupal_html_id('addressfield-wrapper');
256
      $format['#prefix'] = '<div id="' . $format['#wrapper_id'] . '">';
257
      $format['#suffix'] = '</div>';
258
    }
259
    // AJAX enable it.
260
    $format['country']['#ajax'] = array(
261
      'callback' => 'addressfield_standard_widget_refresh',
262
      'wrapper' => $format['#wrapper_id'],
263
    );
264
    $format['country']['#element_validate'] = array('addressfield_standard_country_validate');
265
    // Don't validate any element when the country is changed.
266
    $format['country']['#limit_validation_errors'] = array();
267

    
268
    // Move the country selector to the top of the form.
269
    $format['country']['#weight'] = -500;
270
    // Limit it to the countries supported by the widget.
271
    if (isset($context['field'])) {
272
      $format['country']['#options'] = _addressfield_country_options_list($context['field'], $context['instance']);
273
    }
274

    
275
    // The whole field is considered empty if the country column is empty.
276
    // Therefore, if the field is optional, allow the country to be optional.
277
    // The same logic applies if the field allows multiple values, in which case
278
    // only the first delta needs to have a value.
279
    if (empty($context['instance']['required']) || (isset($context['delta']) && $context['delta'] > 0)) {
280
      $format['country']['#required'] = FALSE;
281
      $format['country']['#empty_value'] = '';
282
      // Hide all other fields until the country is selected.
283
      if (empty($address['country'])) {
284
        $format['street_block']['#access'] = FALSE;
285
        $format['locality_block']['#access'] = FALSE;
286
      }
287
    }
288
  }
289
}