Skip to content
Snippets Groups Projects
Commit 884ec874 authored by Joshua Sedler's avatar Joshua Sedler :cartwheel_tone2:
Browse files

Fix typo in update hook, cast string values and adjust "scale" max value

parent 51341d63
No related branches found
No related tags found
2 merge requests!8282Issue #2230909: Simple decimals fail to pass validation,!579Issue #2230909: Simple decimals fail to pass validation
...@@ -74,7 +74,7 @@ public function storageSettingsForm(array &$form, FormStateInterface $form_state ...@@ -74,7 +74,7 @@ public function storageSettingsForm(array &$form, FormStateInterface $form_state
'#type' => 'number', '#type' => 'number',
'#title' => $this->t('Precision'), '#title' => $this->t('Precision'),
'#min' => 10, '#min' => 10,
'#max' => ini_get('precision') <= 10 ? 10 : ini_get('precision'), '#max' => max((int) ini_get('precision'), 10),
'#default_value' => $settings['precision'], '#default_value' => $settings['precision'],
'#description' => $this->t('The total number of digits to store in the database, including those to the right of the decimal.'), '#description' => $this->t('The total number of digits to store in the database, including those to the right of the decimal.'),
'#disabled' => $has_data, '#disabled' => $has_data,
...@@ -84,7 +84,7 @@ public function storageSettingsForm(array &$form, FormStateInterface $form_state ...@@ -84,7 +84,7 @@ public function storageSettingsForm(array &$form, FormStateInterface $form_state
'#type' => 'number', '#type' => 'number',
'#title' => $this->t('Scale', [], ['context' => 'decimal places']), '#title' => $this->t('Scale', [], ['context' => 'decimal places']),
'#min' => 0, '#min' => 0,
'#max' => ini_get('precision'), '#max' => max((int) ini_get('precision'), 10),
'#default_value' => $settings['scale'], '#default_value' => $settings['scale'],
'#description' => $this->t('The number of digits to the right of the decimal.'), '#description' => $this->t('The number of digits to the right of the decimal.'),
'#disabled' => $has_data, '#disabled' => $has_data,
......
...@@ -25,25 +25,21 @@ function field_update_10001() { ...@@ -25,25 +25,21 @@ function field_update_10001() {
foreach ($decimalFieldStorageEntities as $decimalFieldStorageEntity) { foreach ($decimalFieldStorageEntities as $decimalFieldStorageEntity) {
/** @var \Drupal\field\Entity\FieldStorageConfig $decimalFieldStorageEntity*/ /** @var \Drupal\field\Entity\FieldStorageConfig $decimalFieldStorageEntity*/
$castedInitialPrecisionValue = (int) $decimalFieldStorageEntity->getSetting('precision'); $precision = (int) ini_get('precision');
$precisionMax = max($precision, 10);
// Adjust the precision value, if it is higher, than the allowed // Adjust the precision value, if it is higher, than the allowed
// initial precision configuration option value: // initial precision configuration option value:
$precisionValue = $decimalFieldStorageEntity->getSetting('precision'); $precisionValue = (int) $decimalFieldStorageEntity->getSetting('precision');
$newPrecisionMaxValue = $castedInitialPrecisionValue <= 10 ? 10 : $castedInitialPrecisionValue; $newPrecisionValue = $precisionValue > $precisionMax ? $precisionMax : $precisionValue;
// Set the new precision value:
$newPrecisionValue = $precisionValue > $newPrecisionMaxValue ? $newPrecisionMaxValue : $precisionValue;
// Adjust the scale value, if it is higher, than the allowed // Adjust the scale value, if it is higher, than the allowed
// initial precision configuration option value: // initial precision configuration option value:
$scaleValue = $decimalFieldStorageEntity->getSetting('scale'); $scaleValue = (int) $decimalFieldStorageEntity->getSetting('scale');
$newScaleValue = $scaleValue > $precisionMax ? $precisionMax : $scaleValue;
// Set the new scale value:
$newScaleValue = $scaleValue > $castedInitialPrecisionValue ? $castedInitialPrecisionValue : $scaleValue;
// Set the new precision and scale values if they changed: // Set the new precision and scale values if they changed:
if ($precisionValue != $newPrecisionValue || $scaleValue != $newScaleValue) { if ($precisionValue !== $newPrecisionValue || $scaleValue !== $newScaleValue) {
$decimalFieldStorageEntity->setSetting('precision', $newPrecisionValue); $decimalFieldStorageEntity->setSetting('precision', $newPrecisionValue);
$decimalFieldStorageEntity->setSetting('scale', $newScaleValue); $decimalFieldStorageEntity->setSetting('scale', $newScaleValue);
$decimalFieldStorageEntity->save(); $decimalFieldStorageEntity->save();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment