Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cms 1261 use subdivisionupdater for addresses #14386

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading