Closed alexanderbuergin closed 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!
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:
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
-