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

Add update hook

parent 7d9bbb4c
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
...@@ -11,3 +11,40 @@ ...@@ -11,3 +11,40 @@
function field_update_last_removed() { function field_update_last_removed() {
return 8500; return 8500;
} }
/**
* Update existing DecimalItem fields to match the new maximum value.
*/
function field_update_8001() {
// Load all decimal field storage entities:
$decimalFieldStorageEntities = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties([
'type' => 'decimal',
]);
foreach ($decimalFieldStorageEntities as $decimalFieldStorageEntity) {
/** @var \Drupal\field\Entity\FieldStorageConfig $decimalFieldStorageEntity*/
$castedInitialPrecisionValue = (int) $decimalFieldStorageEntity->getSetting('precision');
// Adjust the precision value, if it is higher, than the allowed
// initial precision configuration option value:
$precisionValue = $decimalFieldStorageEntity->getSetting('precision');
$newPrecisionMaxValue = $castedInitialPrecisionValue <= 10 ? 10 : $castedInitialPrecisionValue;
// Set the new precision value:
$newPrecisionValue = $precisionValue > $newPrecisionMaxValue ? $newPrecisionMaxValue : $precisionValue;
// Adjust the scale value, if it is higher, than the allowed
// initial precision configuration option value:
$scaleValue = $decimalFieldStorageEntity->getSetting('scale');
// Set the new scale value:
$newScaleValue = $scaleValue > $castedInitialPrecisionValue ? $castedInitialPrecisionValue : $scaleValue;
// Set the new precision and scale values and save the entity:
$decimalFieldStorageEntity->setSetting('precision', $newPrecisionValue);
$decimalFieldStorageEntity->setSetting('scale', $newScaleValue);
$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