Skip to content

Commit

Permalink
respect field's currency when normalising the number
Browse files Browse the repository at this point in the history
  • Loading branch information
i-just committed Aug 20, 2024
1 parent daea745 commit 40857ab
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 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']);
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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();
}

Expand Down

0 comments on commit 40857ab

Please sign in to comment.