Skip to content

Commit

Permalink
Merge pull request #15566 from craftcms/bugfix/15565-money-field-curr…
Browse files Browse the repository at this point in the history
…ency-formatting

respect field's currency when normalising the number
  • Loading branch information
brandonkelly authored Aug 21, 2024
2 parents e8d2e8a + 6ff31b0 commit 058b152
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions src/fields/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ?? null);
}
}

Expand Down Expand Up @@ -209,26 +211,29 @@ 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;
}

$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'] = $this->currency;
$value['currency'] = $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));
return $money->getAmount();
}

Expand Down

0 comments on commit 058b152

Please sign in to comment.