Problem/Motivation
When loading an entity display configuration page (like `admin/structure/types/manage/page/display`) that uses Layout Builder, if the entity has an options field that has no options available, the page cannot be loaded due to a PHP error.
Steps to reproduce
First noted on PHP 8.1 and core 9.5.
Enable layout_builder. Configure your entity display to use Layout Builder.
Add a list_string field to your entity that uses an allowed_values_function. Have the allowed_values_function return an empty array. (you can also just install and enable the scheduler_content_moderation_integration module which provides two of these via hook_entity_base_field_info().) Or, just create an options field with no options.
Load the configuration page for your display mode. See:
`ValueError: array_rand(): Argument #1 ($array) cannot be empty in array_rand() (line 68 of /code/web/core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php)`
Proposed resolution
The error is caused by this code in `core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php`:
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$allowed_options = options_allowed_values($field_definition->getFieldStorageDefinition());
$values['value'] = array_rand($allowed_options);
return $values;
}
Because array_rand does not accept empty arrays.
I propose we check if the `$allowed_options` is empty, and if it is, return an empty value rather than invoking `array_rand()`.
(There seem to be a couple issues attempting to resolve similar problems from the Layout Builder side, but this seems like an issue with this specific piece of code. "no options available" isn't an ideal result, but it is sensible in some use cases, and this code should handle those cases without crashing.)
Remaining tasks
May need a test.
User interface changes
None.
API changes
None?
Data model changes
N/A
Release notes snippet
Allow sample content to be generated for options fields with no valid options.
Comment | File | Size | Author |
---|---|---|---|
#5 | After-applying-patch.png | 396.78 KB | arisen |
#5 | Before-applying-patch.png | 646.79 KB | arisen |
#4 | interdiff-3-4.txt | 1.27 KB | DanielVeza |
#3 | drupal_core-generateSampleValue_with_empty_options-3331397.patch | 1.22 KB | Tatsiana |
#4 | 3331397-4.patch | 1.54 KB | DanielVeza |
Issue fork drupal-3331397
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
Comment #3
Tatsiana CreditAttribution: Tatsiana at EPAM Systems commentedHi all,
I have apllied a patch, looks like it works, thanks
Attaching a static patch file with those changes, to make it possible to use in composer until #3204538: GitLab Merge Requests Unable to Generate Incremental Patch Files is resolved.
Comment #4
DanielVezaI was able to replicate this, attached a patch that removes the
else
from the original patch and adds a test.Comment #5
arisenReviewed the patch in #4 on drupal 9.5.4 with php 8.1.
The patch applies cleanly:
Testing steps:
Comment #6
kleiton_rodrigues CreditAttribution: kleiton_rodrigues at CI&T commentedThe #4 patch applies cleanly and works as expected.
LGTM.
Comment #8
DanielVezaRandom fail. Requeing and restoring status
Comment #9
AnybodySame issue here!
Comment #10
AnybodyComment #11
larowlanConfirming the test failed as expected
Comment #15
larowlanCommitted to 10.1.x and backported to 10.0.x and 9.5.x