Skip to content

Commit

Permalink
Merge pull request #15463 from craftcms/feature/country-models
Browse files Browse the repository at this point in the history
Address::getCountry() + normalize Country fields to Country models
  • Loading branch information
brandonkelly authored Aug 1, 2024
2 parents ccb0d65 + 24a7d48 commit ac1ebbf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
- The `allowedGraphqlOrigins` config setting is now deprecated. `craft\filters\Cors` should be used instead. ([#15397](https://github.com/craftcms/cms/pull/15397))
- The `permissionsPolicyHeader` config settings is now deprecated. `craft\filters\Headers` should be used instead. ([#15397](https://github.com/craftcms/cms/pull/15397))
- `{% cache %}` tags now cache any asset bundles registered within them.
- Country field values are now set to `CommerceGuys\Addressing\Country\Country` objects. ([#15463](https://github.com/craftcms/cms/pull/15463))
- Auto-populated section and category group Template settings are now suffixed with `.twig`.

### Extensibility
- Added `craft\config\GeneralConfig::addAlias()`. ([#15346](https://github.com/craftcms/cms/pull/15346))
- Added `craft\elements\Address::getCountry()`. ([#15463](https://github.com/craftcms/cms/pull/15463))
- Added `craft\elements\Asset::$sanitizeOnUpload`. ([#15430](https://github.com/craftcms/cms/discussions/15430))
- Added `craft\filters\Cors`. ([#15397](https://github.com/craftcms/cms/pull/15397))
- Added `craft\filters\Headers`. ([#15397](https://github.com/craftcms/cms/pull/15397))
Expand Down
12 changes: 12 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\Country\Country;
use Craft;
use craft\base\BlockElementInterface;
use craft\base\Element;
Expand Down Expand Up @@ -407,6 +408,17 @@ public function getCountryCode(): string
return $this->countryCode;
}

/**
* Returns a [[Country]] object representing the address’ coutry.
*
* @return Country
* @since 4.11.0
*/
public function getCountry(): Country
{
return Craft::$app->getAddresses()->getCountryRepository()->get($this->countryCode);
}

/**
* @inheritdoc
*/
Expand Down
25 changes: 24 additions & 1 deletion src/fields/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace craft\fields;

use CommerceGuys\Addressing\Country\Country as CountryModel;
use CommerceGuys\Addressing\Exception\UnknownCountryException;
use Craft;
use craft\base\ElementInterface;
use craft\base\Field;
Expand Down Expand Up @@ -43,7 +45,19 @@ public static function valueType(): string
*/
public function normalizeValue(mixed $value, ElementInterface $element = null): mixed
{
return !in_array(strtolower($value), ['', '__blank__']) ? $value : null;
if ($value instanceof CountryModel) {
return $value;
}

if (!$value || strtolower($value) === '__blank__') {
return null;
}

try {
return Craft::$app->getAddresses()->getCountryRepository()->get($value);
} catch (UnknownCountryException) {
return null;
}
}

/**
Expand All @@ -62,6 +76,15 @@ protected function inputHtml(mixed $value, ?ElementInterface $element = null): s
]);
}

/**
* @inheritdoc
*/
public function serializeValue(mixed $value, ?ElementInterface $element = null): mixed
{
/** @var CountryModel|null $value */
return $value?->getCountryCode();
}

/**
* @inheritdoc
*/
Expand Down

0 comments on commit ac1ebbf

Please sign in to comment.