Problem/Motivation
array_rand(): Argument #1 ($array) cannot be empty in ImageItem::generateSampleValue() when file_extensions contains no png/gif/jpg/jpeg.
In PHP 8+, array_rand() throws ValueError when given an empty array. In core/modules/image/src/Plugin/Field/FieldType/ImageItem.php around line 357:
$extensions = array_intersect(explode(' ', $settings['file_extensions']), ['png', 'gif', 'jpg', 'jpeg']); $extension = array_rand(array_combine($extensions, $extensions));
When file_extensions is empty or only contains unsupported extensions (e.g. svg, webp), $extensions is empty and array_rand() fails.
Environment
- Drupal 10.3.14
- PHP 8.3
- Layout Builder + custom frontend_ui
Stack trace
array_rand(): Argument #1 ($array) cannot be empty in line 358 of /var/www/html/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php. #0 /var/www/html/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php(358): array_rand() #1 /var/www/html/core/lib/Drupal/Core/Field/FieldItemList.php(253): Drupal\image\Plugin\Field\FieldType\ImageItem::generateSampleValue() #2 /var/www/html/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(245): Drupal\Core\Field\FieldItemList->generateSampleItems() #3 /var/www/html/core/modules/layout_builder/src/Entity/LayoutBuilderSampleEntityGenerator.php(55): Drupal\Core\Entity\ContentEntityStorageBase->createWithSampleValues() #4 /var/www/html/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php(235): Drupal\layout_builder\Entity\LayoutBuilderSampleEntityGenerator->get() #5 /var/www/html/core/modules/layout_builder/src/Context/LayoutBuilderContextTrait.php(51): Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage->getContextsDuringPreview()
Steps to reproduce
- Create an image field with
file_extensionslikesvg webp(no png, gif, jpg, jpeg). - Configure Layout Builder for an entity type that uses this field.
- Trigger sample entity generation (e.g. via
DefaultsSectionStorage::getContextsDuringPreview→LayoutBuilderSampleEntityGenerator::get→createWithSampleValues). - Observe
ValueError: array_rand(): Argument #1 ($array) cannot be empty.
Proposed resolution
Add fallback when $extensions is empty:
$extensions = array_intersect(explode(' ', $settings['file_extensions']), ['png', 'gif', 'jpg', 'jpeg']); $extensions = $extensions ?: ['png']; $extension = array_rand(array_combine($extensions, $extensions));
Remaining tasks
If this is a duplicate of #3011384: ImageItem::generateSampleValue() and Random::image() should allow all extensions the current toolkit supports (instead of hardcoding a fixed set), then close this issue and continue over there.
MR should be on main
Add a test
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Steps to reproduce
Issue fork drupal-3573189
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:
- 3573189-image-item-array-rand-empty-array
changes, plain diff MR !14747
Comments
Comment #3
super_romeo commentedComment #4
quietone commentedHi, Issues for Drupal core should be targeted to the 'main' branch, our primary development branch. Changes are made on the main branch first, and are then back ported as needed according to the Core change policies. The version the problem was discovered on should be stated in the issue summary Problem/Motivation section. Thanks.
Comment #5
yannickooI can confirm that this patch works as expected 🎉 I have experienced this for my SVG media bundle.
Comment #6
quietone commentedThe MR for this issue should be on main instead of 10.3.x, which is no longer supported. The code is the same in the main branch so maybe the problem exists there as well. Can anyone confirm that?
Can this be reproduced using just the core UI?
From the issue summary I understand that the problem is that ImageItem should handle extensions not in the allowed list. So, I think that would be a better title and a better commit message. I am tagging for a title update. I also think this should be tested, tagging for that as well.
I went to update the remaining tasks but that heading does not exist so I have restored the complete standard template. While doing that I decided to search for a duplicate. And yes, I think this is a duplicate of #3011384: ImageItem::generateSampleValue() and Random::image() should allow all extensions the current toolkit supports (instead of hardcoding a fixed set).
Changing back to needs review to get another opinion on whether this is a duplicate.
Comment #7
dcam commentedI response to #6 I tested to see if this is a duplicate of #3011384. I followed the steps to reproduce the other issue. It resulted in an identical stack trace when compared to the one given in this issue's summary (with the exception of line numbers):
I concur that this issue is a duplicate. I'm going to close this issue and update contribution credit.