diff --git a/addressfield.module b/addressfield.module index c010514..630f369 100644 --- a/addressfield.module +++ b/addressfield.module @@ -355,7 +355,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(); } @@ -370,7 +370,7 @@ function addressfield_default_values($available_countries = NULL) { } return array( - 'country' => $default_country, + 'country' => ($optional_country ? '' : $default_country), 'name_line' => '', 'first_name' => '', 'last_name' => '', @@ -391,9 +391,15 @@ 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; + $ignore_item_keys = array('element_key', '_weight'); + foreach ($item as $field_name => $field_value) { + if (in_array($field_name, $ignore_item_keys)) { + continue; + } + $empty_field &= empty($field_value); + } + return $empty_field; } /** @@ -500,7 +506,7 @@ 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'])) { + elseif (count($items) && isset($items[$delta]) && !addressfield_field_is_empty($items[$delta], NULL)) { // Else use the saved value for the field. $address = $items[$delta]; } @@ -517,7 +523,7 @@ function addressfield_field_widget_form(&$form, &$form_state, $field, $instance, } // Merge in default values to provide a value for every expected array key. - $address += addressfield_default_values($countries); + $address += addressfield_default_values($countries, isset($settings['format_handlers']['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 @@ -564,6 +570,22 @@ 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' => '')); + + // On the field edit form, we check the state of "Make country optional". + // If checked, we always delete the default country on the field edit form. + // It finds the name, the langcode and the delta of the field, + // and clears the country. + if ($form_state['complete form']['#form_id'] == 'field_ui_field_edit_form') { + if (!empty($form_state['values']['instance']['widget']['settings']['format_handlers']['optional-country'])) { + foreach ($form_state['field'] as $field_name => $field) { + foreach ($form_state['values'][$field_name] as $langcode => $langvalue) { + foreach ($form_state['values'][$field_name][$langcode] as $delta => $value) { + $form_state['values'][$field_name][$langcode][$delta]['country'] = ''; + } + } + } + } + } } /** diff --git a/plugins/format/optional-country.inc b/plugins/format/optional-country.inc new file mode 100644 index 0000000..8087600 --- /dev/null +++ b/plugins/format/optional-country.inc @@ -0,0 +1,26 @@ + t('Make country optional'), + 'format callback' => 'addressfield_format_address_optional_country', + 'type' => 'address', + 'weight' => -80, +); + +/** + * Format callback. + * + * @see CALLBACK_addressfield_format_callback() + */ +function addressfield_format_address_optional_country(&$format, &$address, $context = array()) { + $format['country']['#required'] = FALSE; + $format['country']['#empty_value'] = ''; +}