craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.22k stars 626 forks source link

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

Closed peteeveleigh closed 3 weeks ago

peteeveleigh commented 1 month ago

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

-

linear[bot] commented 1 month ago

PT-2082 [5.x]: Fields in address slideout are incorrectly populated for UK addresses

lukeholder commented 1 month ago

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

peteeveleigh commented 1 month ago

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

nfourtythree commented 4 weeks ago

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 commented 3 weeks ago

Nice! Thank you!

brandonkelly commented 3 weeks ago

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