Révision f066bdb5
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/addressfield/addressfield.module | ||
---|---|---|
42 | 42 |
*/ |
43 | 43 |
function addressfield_module_implements_alter(&$implementations, $hook) { |
44 | 44 |
if ($hook == 'token_info_alter') { |
45 |
$group = $implementations['addressfield']; |
|
46 |
unset($implementations['addressfield']); |
|
47 |
$implementations['addressfield'] = $group; |
|
45 |
// Make sure that the $implementations list is populated before altering it, |
|
46 |
// to work around a crash experienced by some people (#2181001). |
|
47 |
if (isset($implementations['addressfield'])) { |
|
48 |
$group = $implementations['addressfield']; |
|
49 |
unset($implementations['addressfield']); |
|
50 |
$implementations['addressfield'] = $group; |
|
51 |
} |
|
48 | 52 |
} |
49 | 53 |
} |
50 | 54 |
|
... | ... | |
142 | 146 |
|
143 | 147 |
ctools_include('plugins'); |
144 | 148 |
$format = array(); |
145 |
$format['#handlers'] = $handlers; |
|
149 |
// Add the handlers, ordered by weight. |
|
150 |
$plugins = addressfield_format_plugins(); |
|
151 |
$format['#handlers'] = array_intersect(array_keys($plugins), $handlers); |
|
152 |
|
|
146 | 153 |
foreach ($format['#handlers'] as $handler) { |
147 | 154 |
if ($callback = ctools_plugin_load_function('addressfield', 'format', $handler, 'format callback')) { |
148 | 155 |
$callback($format, $address, $context); |
... | ... | |
156 | 163 |
if ($context['mode'] == 'form') { |
157 | 164 |
$format['#addressfield'] = TRUE; |
158 | 165 |
$format['#process'][] = 'addressfield_process_format_form'; |
159 |
$format['#required'] = FALSE; |
|
160 | 166 |
} |
161 | 167 |
elseif ($context['mode'] == 'render') { |
162 | 168 |
$format['#pre_render'][] = 'addressfield_render_address'; |
... | ... | |
247 | 253 |
// If the element instructs us to render the option value instead of the |
248 | 254 |
// raw address element value and its #options array has a matching key, |
249 | 255 |
// swap it out for the option value now. |
250 |
$child['#children'] = ''; |
|
251 |
|
|
252 |
if (!empty($child['#render_option_value']) && isset($child['#options'][$address[$key]])) { |
|
256 |
if (!empty($child['#render_option_value']) && isset($address[$key]) && isset($child['#options'][$address[$key]])) { |
|
253 | 257 |
$child['#children'] = check_plain($child['#options'][$address[$key]]); |
254 | 258 |
} |
255 | 259 |
elseif (isset($address[$key])) { |
256 | 260 |
$child['#children'] = check_plain($address[$key]); |
257 | 261 |
} |
262 |
else { |
|
263 |
$child['#children'] = ''; |
|
264 |
} |
|
258 | 265 |
|
259 | 266 |
// Skip empty elements. |
260 | 267 |
if ((string) $child['#children'] === '') { |
... | ... | |
294 | 301 |
*/ |
295 | 302 |
function theme_addressfield_container($variables) { |
296 | 303 |
$element = $variables['element']; |
297 |
|
|
298 | 304 |
$element['#children'] = trim($element['#children']); |
305 |
// Remove the autocomplete attributes because the W3C validator complains. |
|
306 |
// They are only used on forms anyway. |
|
307 |
unset($element['#attributes']['autocomplete']); |
|
308 |
unset($element['#attributes']['x-autocompletetype']); |
|
309 |
|
|
299 | 310 |
if (strlen($element['#children']) > 0) { |
300 | 311 |
$output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>'; |
301 | 312 |
$output .= $element['#children']; |
... | ... | |
418 | 429 |
// Trim whitespace from all of the address components and convert any double |
419 | 430 |
// spaces to single spaces. |
420 | 431 |
foreach ($item as $key => &$value) { |
421 |
if (!in_array($key, array('element_key', 'data')) && is_string($value)) {
|
|
432 |
if (!in_array($key, array('data')) && is_string($value)) { |
|
422 | 433 |
$value = trim(str_replace(' ', ' ', $value)); |
423 | 434 |
} |
424 | 435 |
} |
... | ... | |
479 | 490 |
function addressfield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { |
480 | 491 |
$settings = $instance['widget']['settings']; |
481 | 492 |
|
482 |
// Generate a specific key used to identify this element to restore a default |
|
483 |
// value upon AJAX submission regardless of where this element is in the |
|
484 |
// $form array. |
|
485 |
$element_key = implode('|', array($element['#entity_type'], $element['#bundle'], $element['#field_name'], $element['#language'], $element['#delta'])); |
|
486 |
|
|
487 |
// Store the key in the element array as a value so it can be easily retrieved |
|
488 |
// in context in the $form_state['values'] array in the element validator. |
|
489 |
$element['element_key'] = array( |
|
490 |
'#type' => 'value', |
|
491 |
'#value' => $element_key, |
|
492 |
); |
|
493 |
|
|
494 |
// Get the default address used to build the widget form elements, looking |
|
495 |
// first in the form state, then in the stored value for the field, and then |
|
496 |
// in the default values of the instance. |
|
497 | 493 |
$address = array(); |
498 |
|
|
499 |
if (!empty($form_state['addressfield'][$element_key])) { |
|
500 |
// Use the value from the form_state if available. |
|
501 |
$address = $form_state['addressfield'][$element_key]; |
|
494 |
// If the form has been rebuilt via AJAX, use the form state values. |
|
495 |
// $form_state['values'] is empty because of #limit_validation_errors, so |
|
496 |
// $form_state['input'] needs to be used instead. |
|
497 |
$parents = array_merge($element['#field_parents'], array($element['#field_name'], $langcode, $delta)); |
|
498 |
$input_address = drupal_array_get_nested_value($form_state['input'], $parents); |
|
499 |
if (!empty($input_address)) { |
|
500 |
$address = $input_address; |
|
502 | 501 |
} |
503 | 502 |
elseif (!empty($items[$delta]['country'])) { |
504 | 503 |
// Else use the saved value for the field. |
505 | 504 |
$address = $items[$delta]; |
506 | 505 |
} |
507 |
else { |
|
508 |
// Otherwise use the instance default. |
|
509 |
$address = (array) $instance['default_value'][0]; |
|
510 |
} |
|
511 | 506 |
|
512 | 507 |
// Determine the list of available countries, and if the currently selected |
513 | 508 |
// country is not in it, unset it so it can be reset to the default country. |
... | ... | |
516 | 511 |
unset($address['country']); |
517 | 512 |
} |
518 | 513 |
|
519 |
// Merge in default values to provide a value for every expected array key. |
|
520 |
$address += addressfield_default_values($countries); |
|
514 |
// Merge in default values. |
|
515 |
// Skip the country default if the field is optional, in which case it might |
|
516 |
// not have a country selected, causing the whole field to be treated as empty. |
|
517 |
// The same logic applies if the field allows multiple values, in which case |
|
518 |
// only the first delta is required. |
|
519 |
$default_values = addressfield_default_values($countries); |
|
520 |
if (empty($instance['required']) || $delta > 0) { |
|
521 |
$default_values['country'] = ''; |
|
522 |
} |
|
523 |
$address += $default_values; |
|
521 | 524 |
|
522 | 525 |
// Add the form elements for the standard widget, which includes a country |
523 | 526 |
// select list at the top that reloads the available address elements when the |
... | ... | |
528 | 531 |
// in that context, and it is overridable if necessary. |
529 | 532 |
$element['#type'] = 'fieldset'; |
530 | 533 |
|
534 |
if (!empty($instance['description'])) { |
|
535 |
// Checkout panes convert the fieldset into a container, causing |
|
536 |
// #description to not be rendered. Hence, a real element is added. |
|
537 |
$element['element_description'] = array( |
|
538 |
'#markup' => $instance['description'], |
|
539 |
'#prefix' => '<div class="fieldset-description">', |
|
540 |
'#suffix' => '</div>', |
|
541 |
'#weight' => -999, |
|
542 |
); |
|
543 |
} |
|
544 |
|
|
531 | 545 |
// Generate the address form. |
532 | 546 |
$context = array( |
533 | 547 |
'mode' => 'form', |
... | ... | |
538 | 552 |
); |
539 | 553 |
$element += addressfield_generate($address, $settings['format_handlers'], $context); |
540 | 554 |
|
541 |
// Mark the form element as required if necessary. |
|
542 |
$element['#required'] = $delta == 0 && $instance['required']; |
|
555 |
// If #required is set to FALSE, _addressfield_process_format_form() strips |
|
556 |
// it from all child elements. This is only needed on the field edit form. |
|
557 |
$element['#required'] = ($form_state['build_info']['form_id'] != 'field_ui_field_edit_form'); |
|
543 | 558 |
} |
544 | 559 |
|
545 | 560 |
return $element; |
546 | 561 |
} |
547 | 562 |
|
548 | 563 |
/** |
549 |
* Element validate callback: rebuilds the form on country change and stores the |
|
550 |
* current address value in the $form_state for retrieval on rebuild. |
|
564 |
* Element validate callback: rebuilds the form on country change. |
|
551 | 565 |
*/ |
552 | 566 |
function addressfield_standard_country_validate($element, &$form_state) { |
553 |
// If the country was changed, rebuild the form. |
|
554 | 567 |
if ($element['#default_value'] != $element['#value']) { |
555 |
$form_state['rebuild'] = TRUE; |
|
556 |
} |
|
557 |
|
|
558 |
$parents = $element['#parents']; |
|
559 |
array_pop($parents); |
|
568 |
$parents = $element['#parents']; |
|
569 |
array_pop($parents); |
|
570 |
$address = drupal_array_get_nested_value($form_state['values'], $parents); |
|
571 |
|
|
572 |
// Clear the country-specific field values. |
|
573 |
$country_specific_data = array( |
|
574 |
'dependent_locality' => '', |
|
575 |
'locality' => '', |
|
576 |
'administrative_area' => '', |
|
577 |
'postal_code' => '', |
|
578 |
); |
|
579 |
$address = array_diff_key($address, $country_specific_data); |
|
560 | 580 |
|
561 |
// Search through the form values to find the current address.
|
|
562 |
$address = drupal_array_get_nested_value($form_state['values'], $parents);
|
|
581 |
drupal_array_set_nested_value($form_state['values'], $parents, $address);
|
|
582 |
drupal_array_set_nested_value($form_state['input'], $parents, $address);
|
|
563 | 583 |
|
564 |
// Store the present address values in the form state for retrieval by the |
|
565 |
// widget form regardless of where the widget sits in the $form array. |
|
566 |
$form_state['addressfield'][$address['element_key']] = array_diff_key($address, array('element_key' => '')); |
|
584 |
$form_state['rebuild'] = TRUE; |
|
585 |
} |
|
567 | 586 |
} |
568 | 587 |
|
569 | 588 |
/** |
... | ... | |
593 | 612 |
// new country select list. |
594 | 613 |
$commands[] = ajax_command_replace(NULL, render($element)); |
595 | 614 |
$commands[] = ajax_command_invoke('#' . $element['country']['#id'], 'focus'); |
615 |
// Add the status messages inside the new addressfield's wrapper element, |
|
616 |
// just like core does. |
|
617 |
$commands[] = ajax_command_prepend(NULL, theme('status_messages')); |
|
596 | 618 |
|
597 | 619 |
// Allow other modules to add arbitrary AJAX commands on the refresh. |
598 | 620 |
drupal_alter('addressfield_standard_widget_refresh', $commands, $form, $form_state); |
... | ... | |
792 | 814 |
'premise' => array( |
793 | 815 |
'label' => t('Premise (i.e. Apartment / Suite number)'), |
794 | 816 |
), |
817 |
'sub_premise' => array( |
|
818 |
'label' => t('Sub Premise (i.e. Suite, Apartment, Floor, Unknown.'), |
|
819 |
), |
|
795 | 820 |
); |
796 | 821 |
|
797 | 822 |
// Add the default values for each of the address field properties. |
Formats disponibles : Unified diff
Weekly update of contrib modules