Skip to content

Commit

Permalink
Merge pull request #14386 from craftcms/feature/cms-1261-use-subdivis…
Browse files Browse the repository at this point in the history
…ionupdater-for-addresses

Feature/cms 1261 use subdivisionupdater for addresses
  • Loading branch information
brandonkelly authored Feb 14, 2024
2 parents e8faa36 + 570af4e commit ebb69e1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/elements/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CommerceGuys\Addressing\AddressFormat\AddressField;
use CommerceGuys\Addressing\AddressInterface;
use CommerceGuys\Addressing\Subdivision\SubdivisionUpdater;
use Craft;
use craft\base\Element;
use craft\base\NameTrait;
Expand Down Expand Up @@ -583,6 +584,32 @@ protected function cacheTags(): array
return $tags;
}

/**
* @inheritdoc
*/
public function beforeSave(bool $isNew): bool
{
// commerceguys/addressing 2.0.x - remap changed subdivision IDs
// update the subdivision ID to its ISO code where available
if (isset($this->countryCode)) {
if (isset($this->administrativeArea)) {
$this->administrativeArea = SubdivisionUpdater::updateValue(
$this->countryCode,
$this->administrativeArea,
);
}
// Andorra is the only country with remapped localities.
if ($this->countryCode == 'AD' && isset($this->locality)) {
$this->locality = SubdivisionUpdater::updateValue(
$this->countryCode,
$this->locality,
);
}
}

return parent::beforeSave($isNew);
}

/**
* @inheritdoc
* @throws InvalidConfigException
Expand Down
11 changes: 9 additions & 2 deletions src/helpers/Cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,10 @@ public static function addressFieldsHtml(Address $address): string
$belongsToCurrentUser ? 'address-level2' : 'off',
isset($visibleFields['locality']),
isset($requiredFields['locality']),
[$address->countryCode, $address->administrativeArea],
array_values(array_filter([
$address->countryCode,
array_key_exists('administrativeArea', $visibleFields) ? $address->administrativeArea : false,
], fn($v) => $v !== false)),
true,
) .
self::_subdivisionField(
Expand All @@ -2091,7 +2094,11 @@ public static function addressFieldsHtml(Address $address): string
$belongsToCurrentUser ? 'address-level3' : 'off',
isset($visibleFields['dependentLocality']),
isset($requiredFields['dependentLocality']),
[$address->countryCode, $address->administrativeArea, $address->locality],
array_values(array_filter([
$address->countryCode,
array_key_exists('administrativeArea', $visibleFields) ? $address->administrativeArea : false,
array_key_exists('locality', $visibleFields) ? $address->locality : false,
], fn($v) => $v !== false)),
false,
) .
static::textFieldHtml([
Expand Down

0 comments on commit ebb69e1

Please sign in to comment.