RandomEntityController not working if quantity higher than existing items

Created on 23 November 2023, about 1 year ago
Updated 24 November 2023, about 1 year ago

Problem/Motivation

The module goes into infinite loop if the quantity is higher than the existing items

Steps to reproduce

Set a entity reference field, e.g. a field on node bundle referencing paragraphs.
Configure the "Random Rendered Entity" formatter with a high quantity, e.g. 999.
Create a node with less referenced entities than the quantity above.

Open the node: no entity is shown.

This bug is caused by RandomEntityController::generateRandomNumbers, where

    while ($j <= $quantity) {
      $rand = rand(0, $max);
      // Skip existing.
      if (in_array($rand, $randomNumbers)) {
        continue;
      }
      $randomNumbers[$rand] = $rand;
      $j++;
    }

and

 if (in_array($rand, $randomNumbers)) {
        continue;
      }

will go in loop when $max < $quantity

🐛 Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

🇮🇹Italy Giuseppe87

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024