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

Address::getCountry() + normalize Country fields to Country models #15463

Merged
merged 2 commits into from
Aug 1, 2024
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
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