Problem/Motivation
Error page when attempting to use layout builder with integer fields with minimum value >= 1000 and no maximum.
ValueError: mt_rand(): Argument #2 ($max) must be greater than or equal to argument #1 ($min) in mt_rand() (line 108 of core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php).
Steps to reproduce
- Add Number (integer) field to a content type.
- Set minimum value to 1000 or higher. Leave maximum field blank.
- Enable layout builder module.
- Manage Display for content type -> Use Layout Builder and save.
- Click Manage Layout.
Proposed resolution
Ensure generateSampleValue() maximum default is >= minimum value.
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$min = $field_definition->getSetting('min') ?: 0;
- $max = $field_definition->getSetting('max') ?: 999;
+ $max = $field_definition->getSetting('max') ?: $min + 999;
$values['value'] = mt_rand($min, $max);
return $values;
}
Comments
Comment #2
scottsperry commentedComment #3
cilefen commentedIt is a static function so it should be easy to add a unit test, or there may already be a test which needs updating.
Comment #5
kunalgautam commentedThis is the patch for Drupal 9.5
Comment #6
gaurav-mathur commentedpatch #5 successfully applied on 9.4.x and error solved after the patch i refer some screenshot ,thanks @kkalashnikov for your patch .
Thanks and Regards
Comment #7
scottsperry commentedHere is a potential patch for tests.
Comment #9
quietone commentedJust adding a related issue.
Comment #10
quietone commentedActually, this is a duplicate of and earlier issue #3052663: Validate the min, max and default values for Numeric fields. And while there is a patch here the other issue will address this problem for all numeric types, so let's move the work over there.
I am closing this as a duplicate. I'll add credit over there and link to the patches here.
Comment #11
jvogt commentedThis didn't end up being addressed in #3052663: Validate the min, max and default values for Numeric fields. This remains an issue if min >= 1000 and max is not set in the field settings. I've re-rolled the patch for 10.3.
Comment #12
jvogt commentedComment #13
smustgrave commentedThanks for reporting but fixes should be in an MR and most likely will need test coverage
Comment #14
arunkumarkComment #15
arunkumarkCreated MR and added test case for Integer item without max value.
Comment #17
smustgrave commentedLeft a comment on the MR.