craftcms / cms

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

[5.x]: Missing States/Regions for Germany, Switzerland, and Austria in Address Fields #15591

Closed alexanderbuergin closed 2 months ago

alexanderbuergin commented 2 months ago

Description

It would be very helpful if the address fields in Craft CMS included the states/regions for Germany, Switzerland, and Austria. This would significantly improve the user experience in the DACH region and help avoid errors when handling addresses.

Below, I've provided a list of the respective states/regions, both in German and English, in alphabetical order:

Deutschland Germany English
Baden-Württemberg Baden-Württemberg
Bayern Bavaria
Berlin Berlin
Brandenburg Brandenburg
Bremen Bremen
Hamburg Hamburg
Hessen Hesse
Mecklenburg-Vorpommern Mecklenburg-Western Pomerania
Niedersachsen Lower Saxony
Nordrhein-Westfalen North Rhine-Westphalia
Rheinland-Pfalz Rhineland-Palatinate
Saarland Saarland
Sachsen Saxony
Sachsen-Anhalt Saxony-Anhalt
Schleswig-Holstein Schleswig-Holstein
Thüringen Thuringia
Schweiz German Switzerland English
Aargau Aargau
Appenzell Ausserrhoden Appenzell Ausserrhoden
Appenzell Innerrhoden Appenzell Innerrhoden
Basel-Landschaft Basel-Landschaft
Basel-Stadt Basel-Stadt
Bern Bern
Freiburg Fribourg
Genf Geneva
Glarus Glarus
Graubünden Grisons
Jura Jura
Luzern Lucerne
Neuenburg Neuchâtel
Nidwalden Nidwalden
Obwalden Obwalden
Schaffhausen Schaffhausen
Schwyz Schwyz
Solothurn Solothurn
St. Gallen St. Gallen
Tessin Ticino
Thurgau Thurgau
Uri Uri
Waadt Vaud
Wallis Valais
Zug Zug
Zürich Zurich
Österreich Austria English
Burgenland Burgenland
Kärnten Carinthia
Niederösterreich Lower Austria
Oberösterreich Upper Austria
Salzburg Salzburg
Steiermark Styria
Tirol Tyrol
Vorarlberg Vorarlberg
Wien Vienna

Implementing these states/regions in the address fields would be a great enhancement for Craft CMS users in the DACH region.

Thank you for considering this request!

Craft CMS version

5.3

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

-

nfourtythree commented 2 months ago

Hi @alexanderbuergin

Thank you for your message. We have tried to adhere to the detail set out with the underlying library we use for addressing in Craft. That is the commerceguys/addressing library.

In that library they do not currently implement the states/regions you helpfully provided. It could be a good idea to create a PR over there to get it implemented. This way it will filter back to Craft when it has been accepted.

All is not lost in them meantime. You can implement these yourself using code in a custom module and events from the addresses service in Craft.

Below is an abbreviated example of how to do it:

use craft\base\Event;
use craft\services\Addresses;
use craft\events\DefineAddressFieldsEvent;
use craft\events\DefineAddressSubdivisionsEvent;

// ... 

Event::on(
    Addresses::class,
    Addresses::EVENT_DEFINE_USED_FIELDS,
    function(DefineAddressFieldsEvent $event) {
        if (in_array($event->countryCode, ['AU', 'CH', 'DE'])) {
            $event->fields[] = AddressField::ADMINISTRATIVE_AREA;
        }
    }
);

Event::on(
    Addresses::class,
    Addresses::EVENT_DEFINE_ADDRESS_SUBDIVISIONS,
    function (DefineAddressSubdivisionsEvent $event) {
        if (!empty($event->parents) && $event->parents[0] === 'DE' && count($event->parents) === 1) {
            $event->subdivisions = array_merge([
                '' => 'Please select',
                'Baden-Württemberg' => 'Baden-Württemberg',
                'Bayern' => 'Bavaria',
                'Berlin' => 'Berlin',
                'Brandenburg' => 'Brandenburg',
                // ...
            ], $event->subdivisions);
        }

        if (!empty($event->parents) && $event->parents[0] === 'AU' && count($event->parents) === 1) {
            $event->subdivisions = array_merge([
                // Austrian regions
            ], $event->subdivisions);
        }

        if (!empty($event->parents) && $event->parents[0] === 'CH' && count($event->parents) === 1) {
            $event->subdivisions = array_merge([
                // Swiss regions
            ], $event->subdivisions);
        }
    }
);

Hope this helps, thanks!