Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x]: "City" field in address slideout is incorrectly labelled/populated for UK addresses #15551

Closed
peteeveleigh opened this issue Aug 17, 2024 · 6 comments
Assignees
Labels

Comments

@peteeveleigh
Copy link

peteeveleigh commented Aug 17, 2024

What happened?

Ran into this on a project site earlier today, but just confirmed the behaviour on a brand new, clean install with no plugins other than Commerce installed.

Description

For UK Addresses, the "City" dropdown field in the Address slideout is populated with a weird mix of counties and cities and there is no "County" field

Screenshot 2024-08-17 at 01 10 53

For example, the dropdown contains both "Cardiff" which is a city, and "Worcestershire" which is a county.
Furthermore, many towns and cities are missing from the list, for example "Evesham" (a town in Worcestershire) so it's not possible to enter an accurate address.

As it is, it's pretty unusable.

It feels like a mix of 3 things.

  1. The "City" field should be labelled as "County"
  2. The contents of the dropdown should only be UK counties
  3. There should be a separate plain text field for "City"

Steps to reproduce

  1. Create a new address
  2. Select "United Kingdom" for the country
  3. Examine the "City" field.

Expected behavior

  1. The "City" field should be freetext - I don't think it's practical to have a pre-populated list of all the possible towns and cities in the UK.
  2. There should be a "County" field which is a dropdown of UK counties.

Actual behavior

  1. The "City" dropdown contains a mix of towns, cities, and counties with many omissions.
  2. There is no separate "County" field.

Craft CMS version

6.3.4

Craft Commerce version

5.0.16.2

PHP version

8.2.18

Operating system and version

Linux 6.6.32-linuxkit

Database type and version

MySQL 8.0.33

Image driver and version

No response

Installed plugins and versions

Copy link

linear bot commented Aug 17, 2024

@peteeveleigh peteeveleigh changed the title [5.x]: Fields in address slideout are incorrectly populated for UK addresses [5.x]: "City" field in address slideout is incorrectly labelled/populated for UK addresses Aug 17, 2024
@lukeholder
Copy link
Member

@peteeveleigh can you confirm this is only happening with Commerce installed, or is it the same for addresses without Commerce?

@peteeveleigh
Copy link
Author

@lukeholder It's the same without Commerce. Just a Craft install with no plugins at all.

@nfourtythree nfourtythree transferred this issue from craftcms/commerce Aug 17, 2024
@nfourtythree
Copy link
Contributor

Hi @peteeveleigh

Thank you for raising this with us. We have identified the cause of the bug and are now looking to create a solution to fix the issue.

In the meantime, the following code provides a temporary solution to ensure that the counties are in the administrative area field and therefore leaving the "City" (locality) field to be free text as expected. This code can be dropped in a custom module and should get everything working for you.

use CommerceGuys\Addressing\AddressFormat\AddressField;
use craft\events\DefineAddressFieldLabelEvent;
use craft\events\DefineAddressFieldsEvent;
use craft\events\DefineAddressSubdivisionsEvent;
use craft\services\Addresses;

// ...

// Add the 'administrativeArea' field to the list of fields used by the 'GB' country code
Event::on(
    Addresses::class,
    Addresses::EVENT_DEFINE_USED_FIELDS,
    function(DefineAddressFieldsEvent $event) {
        if ($event->countryCode === 'GB') {
            $event->fields[] = AddressField::ADMINISTRATIVE_AREA;
        }
    }
);

// Change the label of the 'administrativeArea' field to "County" for the 'GB' country code
Event::on(
    Addresses::class,
    Addresses::EVENT_DEFINE_FIELD_LABEL,
    function(DefineAddressFieldLabelEvent $event) {
        if ($event->countryCode === 'GB' && $event->field === AddressField::ADMINISTRATIVE_AREA) {
            $event->label = 'County';
        }
    }
);

// Add a blank option to 'GB' country code subdivisions to avoid accidental selection when resaving an address
Event::on(
    Addresses::class,
    Addresses::EVENT_DEFINE_ADDRESS_SUBDIVISIONS,
    function (DefineAddressSubdivisionsEvent $event) {
        if (!empty($event->parents) && $event->parents[0] === 'GB' && count($event->parents) === 1) {
            $event->subdivisions = array_merge(['' => 'Select county'], $event->subdivisions);
        }
    }
);

We will keep this issue open and updated with all future updates.

Thanks!

@peteeveleigh
Copy link
Author

Nice! Thank you!

@brandonkelly
Copy link
Member

Craft 4.11.5 and 5.3.6 are out now with a fix for this, via #15584.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants