Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Currently when parsing an address field, country code is used to specify country. In some conditions, for instance when having an address field having only the country part populated, this may lead to incorrect results. This is caused by country codes being ambiguous in this context, e.g. IL may indicate Illinois or Israel.
Proposed resolution
Adopt the Country name (CODE)
pattern to make country unambiguous. This would lead to:
IL,United States (US)
vs
Israel (IL)
Comment | File | Size | Author |
---|---|---|---|
#20 | geocoder-2245189-20.patch | 1.12 KB | basvredeling |
#13 | geocoder-make_address_field-2245189-13.patch | 1.73 KB | basvredeling |
#9 | geocoder-make_address_field-2245189-9.patch | 1.4 KB | basvredeling |
#2 | geocoder-country-2245189-2.patch | 1.01 KB | plach |
Comments
Comment #1
plachComment #2
plachComment #3
nowidid CreditAttribution: nowidid commentedThis patch works for me.
This code should be a regular part of the module.
Thanks
nowi
Comment #4
dubcanada CreditAttribution: dubcanada commentedI'd also like to see
if (!empty($field_item['organisation_name'])) $address .= $field_item['organisation_name'] . ',';
added. So you can get the organization name as well. The more data the better the results.
Comment #5
Simon Georges CreditAttribution: Simon Georges commentedComment #6
basvredelingWhy do we need the country suffix at all? Just the country name would suffice, wouldn't it?
Also there are cases like "Canada (ca)" leading to multiple results in both Canada and California whereas just "Canada" gives a single correct result.
I'll test this later today. Seems like a minor issue to fix.
Comment #7
basvredelingNew patch. Things changed:
Comment #8
basvredelingforgot to upload the patch
Comment #9
basvredelingThis new patch ensures the /includes/iso.inc is loaded. (also leveraged by the Drupal installer)
This functionality overlaps with #1871962: Use Countries module to improve geocoding results
This uses Drupal core countries list, whereas 1871962 uses countries module.
Comment #10
kerasai CreditAttribution: kerasai commented@basvredeling, this works great but a couple suggestions.
Let's use
country_get_list()
instead of_country_get_predefined_list()
. This is better as it includes an alter hook, to give other modules a chance to tweak the list of countries if desired. You'll also need to update the include to grablocale.inc
instead ofiso.inc
.Looks like the trailing comma is missing when you append the country. Should be something like this:
$address .= $field_item['country'] . ',';
If you would like to upload a new patch, I'll test and mark RTBC. This (referencing the country by its full name) is an important improvement and it'd be great to get this in.
Comment #11
PolComment #12
NWOM CreditAttribution: NWOM commentedAwesome. #9 in combination with #10, finally fixed my country issue. Locations located in Germany (DE), are no longer matched up with Delaware.
Thank you so much.
Comment #13
basvredeling@kerasai #10 Initially I've opted for the private function instead because it didn't imply the locale module to be enabled. Usually the locale.inc is loaded by calling locale_init(). And I didn't want to add an additional dependency to geocoder. But we can always include it manually of course.
Here's a new patch. While I was at it, I added a todo to the last undocumented function
geocoder_widget_array_recursive_diff()
.Comment #14
basvredelingReview please
Comment #15
PolThanks mate! Committed!
Comment #20
basvredelingThe current solution works but taking a new look at it, it seems silly why we just didn't use the
_addressfield_country_options_list()
function.Here's a patch, just for posterity.
Comment #21
basvredelingI've included the concept of #20 into the patch #24 here: #1967520: Use Google geocoding components filter for better results with addressfield
Comment #22
izmeez CreditAttribution: izmeez commented@basvredeling Would it help to put the patch in comment #20 in a new issue for review to simplify the now existing code?
Comment #23
basvredelingNo, I don't think it's necessary to open a new issue. I'm ever so sorry if the new patch is bigger and harder to review. I can try and explain if that helps. To summarize: the old code leverages either the countries module or core locale country list to transform a country code into a country name. But since we're parsing an addressfield anyway, there already is a list of country names. So let's just use that.
The old code is much more complex that the new code and reads roughly like this.
old:
new:
As a final note, the patch also affects the solution from #1871962: Use Countries module to improve geocoding results.
Comment #24
izmeez CreditAttribution: izmeez commentedYes, I understand from reading the patch originally. I was just concerned that in a closed issue queue the patch may be overlooked.
Simple is usually good way to go for code.
Thanks.