diff --git a/addressfield.module b/addressfield.module index 177d92b..b384423 100644 --- a/addressfield.module +++ b/addressfield.module @@ -314,7 +314,7 @@ function addressfield_field_info() { /** * Returns an array of default values for the addressfield form elements. */ -function addressfield_default_values($available_countries = NULL) { +function addressfield_default_values($available_countries = NULL, $optional_country = FALSE) { if (!isset($available_countries)) { $available_countries = _addressfield_country_options_list(); } @@ -327,7 +327,7 @@ function addressfield_default_values($available_countries = NULL) { } return array( - 'country' => $default_country, + 'country' => ($optional_country ? '' : $default_country), 'name_line' => '', 'first_name' => '', 'last_name' => '', @@ -348,9 +348,14 @@ function addressfield_default_values($available_countries = NULL) { * Implements hook_field_is_empty(). */ function addressfield_field_is_empty($item, $field) { - // Every address field must have at least a country value or it is considered - // empty, even if it has name information. - return empty($item['country']); + $empty_field = TRUE; + foreach ($item as $field_name => $field_value) { + if ($field_name == 'element_key') { + continue; + } + $empty_field &= empty($item[$field_name]); + } + return $empty_field; } /** @@ -427,7 +432,8 @@ function addressfield_field_widget_form(&$form, &$form_state, $field, $instance, // Use the value from the form_state if available. $address = $form_state['addressfield'][$element_key]; } - elseif (!empty($items[$delta]['country'])) { + // we have to change the condition but on the field edit form $items is an empty array + elseif (count($items) && isset($items[$delta]) && !addressfield_field_is_empty($items[$delta], NULL)) { // Else use the saved value for the field. $address = $items[$delta]; } @@ -438,7 +444,7 @@ function addressfield_field_widget_form(&$form, &$form_state, $field, $instance, // Merge in default values to provide a value for every expected array key. $countries = _addressfield_country_options_list($field, $instance); - $address += addressfield_default_values($countries); + $address += addressfield_default_values($countries, isset($settings['format_handlers']['address-optional-country'])); // Add the form elements for the standard widget, which includes a country // select list at the top that reloads the available address elements when the