From 40857ab7e060daba5fc207d898135c97979730ce Mon Sep 17 00:00:00 2001 From: Iwona Just Date: Tue, 20 Aug 2024 15:06:49 +0100 Subject: [PATCH 1/3] respect field's currency when normalising the number --- src/fields/Money.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/fields/Money.php b/src/fields/Money.php index 0c04003455d..fc366ca22dc 100644 --- a/src/fields/Money.php +++ b/src/fields/Money.php @@ -100,7 +100,9 @@ public function __construct($config = []) // Config normalization foreach (['defaultValue', 'min', 'max'] as $name) { if (isset($config[$name])) { - $config[$name] = $this->_normalizeNumber($config[$name]); + // at this point the currency property isn't set yet, so we need to explicitly pass it to the _normalizeNumber() + // see https://github.com/craftcms/cms/issues/15565 for more details + $config[$name] = $this->_normalizeNumber($config[$name], $config['currency']); } } @@ -209,9 +211,10 @@ public function serializeValue(mixed $value, ElementInterface $element = null): /** * @param mixed $value + * @param string|null $currency * @return string|null */ - private function _normalizeNumber(mixed $value): ?string + private function _normalizeNumber(mixed $value, ?string $currency = null): ?string { if ($value === '') { return null; @@ -223,12 +226,12 @@ private function _normalizeNumber(mixed $value): ?string return null; } - $value['currency'] = $this->currency; + $value['currency'] = $currency ?? $this->currency; $money = MoneyHelper::toMoney($value); return $money ? $money->getAmount() : null; } - $money = new MoneyLibrary($value, new Currency($this->currency)); + $money = new MoneyLibrary($value, new Currency($currency ?? $this->currency)); return $money->getAmount(); } From b5e54e8d19c542ef7ef1889f2368e88d5a79a51f Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Wed, 21 Aug 2024 14:53:09 -0400 Subject: [PATCH 2/3] Cleanup --- src/fields/Money.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/fields/Money.php b/src/fields/Money.php index fc366ca22dc..25525834b50 100644 --- a/src/fields/Money.php +++ b/src/fields/Money.php @@ -102,7 +102,7 @@ public function __construct($config = []) if (isset($config[$name])) { // at this point the currency property isn't set yet, so we need to explicitly pass it to the _normalizeNumber() // see https://github.com/craftcms/cms/issues/15565 for more details - $config[$name] = $this->_normalizeNumber($config[$name], $config['currency']); + $config[$name] = $this->_normalizeNumber($config[$name], $config['currency'] ?? null); } } @@ -220,18 +220,20 @@ private function _normalizeNumber(mixed $value, ?string $currency = null): ?stri return null; } + $currency ??= $this->currency; + // Was this submitted with a locale ID? (This means the data is coming from the settings form) if (isset($value['locale'], $value['value'])) { if ($value['value'] === '') { return null; } - $value['currency'] = $currency ?? $this->currency; + $value['currency'] = $currency; $money = MoneyHelper::toMoney($value); return $money ? $money->getAmount() : null; } - $money = new MoneyLibrary($value, new Currency($currency ?? $this->currency)); + $money = new MoneyLibrary($value, new Currency($currency)); return $money->getAmount(); } From 6ff31b0b43dfe6a5da5aa0db63ed1bbf94011f45 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Wed, 21 Aug 2024 14:55:27 -0400 Subject: [PATCH 3/3] Release note --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a8b990a922..b39b498c84c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Fixed a bug where the `users/set-password` action wasn’t respecting `redirect` params. ([#15538](https://github.com/craftcms/cms/issues/15538)) - Fixed a bug where the “Default Values” Table field setting wasn’t escaping column headings. ([#15552](https://github.com/craftcms/cms/issues/15552)) - Fixed a bug where Craft couldn’t be installed with existing project config files, if any plugins specified their schema version via `composer.json`. ([#15559](https://github.com/craftcms/cms/issues/15559)) +- Fixed a bug where Money fields’ min, max, and default values weren’t being set to the correct currency. ([#15565](https://github.com/craftcms/cms/issues/15565), [#15566](https://github.com/craftcms/cms/pull/15566)) ## 4.11.3 - 2024-08-13