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

  1. Add Number (integer) field to a content type.
  2. Set minimum value to 1000 or higher. Leave maximum field blank.
  3. Enable layout builder module.
  4. Manage Display for content type -> Use Layout Builder and save.
  5. 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;
   }

Issue fork drupal-3327767

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

scottsperry created an issue. See original summary.

scottsperry’s picture

Issue summary: View changes
cilefen’s picture

Issue summary: View changes
Status: Active » Needs work

It is a static function so it should be easy to add a unit test, or there may already be a test which needs updating.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kunalgautam’s picture

Status: Needs work » Needs review
StatusFileSize
new768 bytes

This is the patch for Drupal 9.5

gaurav-mathur’s picture

Version: 9.5.x-dev » 9.4.x-dev
StatusFileSize
new85.08 KB
new140.8 KB
new241.5 KB

patch #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

scottsperry’s picture

Version: 9.4.x-dev » 9.5.x-dev
StatusFileSize
new918 bytes

Here is a potential patch for tests.

Status: Needs review » Needs work

The last submitted patch, 7: tests-3327767.patch, failed testing. View results

quietone’s picture

Just adding a related issue.

quietone’s picture

Status: Needs work » Closed (duplicate)
Issue tags: +Bug Smash Initiative
Related issues: +#3052663: Validate the min, max and default values for Numeric fields

Actually, 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.

jvogt’s picture

This 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.

jvogt’s picture

Version: 9.5.x-dev » 10.3.x-dev
Status: Closed (duplicate) » Needs review
smustgrave’s picture

Version: 10.3.x-dev » 11.x-dev
Status: Needs review » Needs work

Thanks for reporting but fixes should be in an MR and most likely will need test coverage

arunkumark’s picture

Assigned: Unassigned » arunkumark
arunkumark’s picture

Assigned: arunkumark » Unassigned
Status: Needs work » Needs review

Created MR and added test case for Integer item without max value.

smustgrave’s picture

Status: Needs review » Needs work

Left a comment on the MR.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.