diff --git a/addressfield.module b/addressfield.module index b470c83..26b39d9 100644 --- a/addressfield.module +++ b/addressfield.module @@ -355,23 +355,18 @@ function addressfield_field_info() { /** * Returns an array of default values for the addressfield form elements. */ -function addressfield_default_values($available_countries = NULL, $optional_country = FALSE) { - if ($optional_country) { - $default_country = ''; +function addressfield_default_values($available_countries = NULL) { + if (!isset($available_countries)) { + $available_countries = _addressfield_country_options_list(); } - else { - if (!isset($available_countries)) { - $available_countries = _addressfield_country_options_list(); - } - // Use the default country of the site if possible. - $default_country = variable_get('site_default_country', NULL); + // Use the default country of the site if possible. + $default_country = variable_get('site_default_country', NULL); - // If the default country is undefined or not in the list of available countries, - // just fallback to the first country in the list. - if (!$default_country || !isset($available_countries[$default_country])) { - $default_country = key($available_countries); - } + // If the default country is undefined or not in the list of available countries, + // just fallback to the first country in the list. + if (!$default_country || !isset($available_countries[$default_country])) { + $default_country = key($available_countries); } return array( @@ -396,7 +391,29 @@ function addressfield_default_values($available_countries = NULL, $optional_coun * Implements hook_field_is_empty(). */ function addressfield_field_is_empty($item, $field) { + + // We need the field instance to check to see if country is optional and if + // default country is the same as the currently set country. In that case we + // consider the field to be empty. + // @TODO - it might be safer still to have a flag that says to count + // submissions with country-only as empty when there is a default country. + $default_country = $optional_country = FALSE; + if (isset($item['element_key'])) { + $instance_keys = explode('|', $item['element_key']); + list($entity_type, $bundle_name, $field_name) = $instance_keys; + $field_instance = field_info_instance($entity_type, $field_name, $bundle_name); + $optional_country = $field_instance['widget']['settings']['format_handlers']['optional-country']; + if (isset($field_instance['default_value'][0]['country'])) { + $default_country = $field_instance['default_value'][0]['country']; + } + } + + // @TODO - $optional_country should be a boolean to match similar settings $ignore_item_keys = array('element_key', '_weight'); + if ($optional_country && $default_country && $default_country == $item['country']) { + $ignore_item_keys[] = 'country'; + } + foreach ($item as $field_name => $field_value) { if (in_array($field_name, $ignore_item_keys)) { continue; @@ -405,7 +422,6 @@ function addressfield_field_is_empty($item, $field) { return FALSE; } } - return TRUE; } @@ -577,13 +593,6 @@ function addressfield_standard_country_validate($element, &$form_state) { // Store the present address values in the form state for retrieval by the // widget form regardless of where the widget sits in the $form array. $form_state['addressfield'][$address['element_key']] = array_diff_key($address, array('element_key' => '')); - - // Don't allow a default country if the optional country plugin is enabled. - if ($form_state['complete form']['#form_id'] == 'field_ui_field_edit_form') { - if (!empty($form_state['values']['instance']['widget']['settings']['format_handlers']['optional-country'])) { - $form_state['values'][key($form_state['field'])][LANGUAGE_NONE][0]['country'] = ''; - } - } } /**