From 481882a364474e243e5f6c93c5332a31b4ff6657 Mon Sep 17 00:00:00 2001 From: Iwona Just Date: Fri, 23 Aug 2024 11:05:34 +0100 Subject: [PATCH 1/3] improve how we get parents for subdivision fields --- src/helpers/Cp.php | 50 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/src/helpers/Cp.php b/src/helpers/Cp.php index abed3472b50..27721f85948 100644 --- a/src/helpers/Cp.php +++ b/src/helpers/Cp.php @@ -7,6 +7,7 @@ namespace craft\helpers; +use CommerceGuys\Addressing\Subdivision\SubdivisionRepository as BaseSubdivisionRepository; use Craft; use craft\base\Element; use craft\base\ElementInterface; @@ -1301,6 +1302,8 @@ public static function addressFieldsHtml(Address $address): string $addressesService->getUsedSubdivisionFields($address->countryCode), )) + $requiredFields; + $parents = self::_getSubdivisionParents($address, $visibleFields); + return static::textFieldHtml([ 'status' => $address->getAttributeStatus('addressLine1'), @@ -1337,7 +1340,7 @@ public static function addressFieldsHtml(Address $address): string $belongsToCurrentUser ? 'address-level2' : 'off', isset($visibleFields['locality']), isset($requiredFields['locality']), - [$address->countryCode, $address->administrativeArea], + $parents['locality'], true, ) . self::_subdivisionField( @@ -1346,7 +1349,7 @@ public static function addressFieldsHtml(Address $address): string $belongsToCurrentUser ? 'address-level3' : 'off', isset($visibleFields['dependentLocality']), isset($requiredFields['dependentLocality']), - [$address->countryCode, $address->administrativeArea, $address->locality], + $parents['dependentLocality'], false, ) . static::textFieldHtml([ @@ -1378,6 +1381,49 @@ public static function addressFieldsHtml(Address $address): string ]); } + /** + * Get parents array that needs to be passed to the subdivision repository getList() method to get the list of subdivisions back. + * + * For the administrativeArea, the parent is always just the country code. + * + * For the locality: + * - it could be just the country code + * - for countries that don't use administrativeArea field; that's the case with Andorra + * - it could be the country code and the administrative area code + * - for countries that use both administrative areas and localities; e.g. Chile (Chile => Araucania > Carahue) + * - the administrative area can be passed as null too; + * this will be triggered for the United Kingdom (GB), where you can conditionally turn on administrativeArea; + * in the case of GB, not passing null as the second value would result + * in the administrativeAreas list being returned for the locality field (https://github.com/craftcms/cms/issues/15551); + * + * For the dependentLocality: + * - as above but taking locality into consideration too; e.g. China has all 3 levels of subdivisions and has lists for all 3 of them + * (China => Hoilongjiang Sheng > Gegang Shi > Dongshan Qu) + * + * @param Address $address + * @param array $visibleFields + * @return array + */ + private static function _getSubdivisionParents(Address $address, array $visibleFields): array + { + $baseSubdivisionRepository = new BaseSubdivisionRepository(); + + $localityParents = [$address->countryCode]; + $administrativeAreas = $baseSubdivisionRepository->getList([$address->countryCode]); + + if (array_key_exists('administrativeArea', $visibleFields) || empty($administrativeAreas)) { + $localityParents[] = $address->administrativeArea; + } + + $dependentLocalityParents = $localityParents; + $localities = $baseSubdivisionRepository->getList($localityParents); + if (array_key_exists('locality', $visibleFields) || empty($localities)) { + $dependentLocalityParents[] = $address->locality; + } + + return ['locality' => $localityParents, 'dependentLocality' => $dependentLocalityParents]; + } + private static function _subdivisionField( Address $address, string $name, From 4036db1992516e92b12728afefcec4618c5055dc Mon Sep 17 00:00:00 2001 From: Iwona Just Date: Fri, 23 Aug 2024 11:34:44 +0100 Subject: [PATCH 2/3] type-os --- src/helpers/Cp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/Cp.php b/src/helpers/Cp.php index 27721f85948..b639c898b45 100644 --- a/src/helpers/Cp.php +++ b/src/helpers/Cp.php @@ -1398,7 +1398,7 @@ public static function addressFieldsHtml(Address $address): string * * For the dependentLocality: * - as above but taking locality into consideration too; e.g. China has all 3 levels of subdivisions and has lists for all 3 of them - * (China => Hoilongjiang Sheng > Gegang Shi > Dongshan Qu) + * (China => Heilongjiang Sheng > Hegang Shi > Dongshan Qu) * * @param Address $address * @param array $visibleFields From 65473027b6107824bce5786435f67aab35046f28 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Mon, 26 Aug 2024 10:03:43 -0400 Subject: [PATCH 3/3] Release note [ci skip] --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72bc4cf2ea0..fc52cebd1a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Fixed a bug where it wasn’t possible to override named transforms in GraphQL queries. ([#15572](https://github.com/craftcms/cms/issues/15572)) +- Fixed a bug where address subdivision fields could be incorrectly labelled and/or populated with the wrong options. ([#15551](https://github.com/craftcms/cms/issues/15551), [#15584](https://github.com/craftcms/cms/pull/15584)) ## 4.11.4 - 2024-08-21