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

  1. Create an image field with file_extensions like svg webp (no png, gif, jpg, jpeg).
  2. Configure Layout Builder for an entity type that uses this field.
  3. Trigger sample entity generation (e.g. via DefaultsSectionStorage::getContextsDuringPreviewLayoutBuilderSampleEntityGenerator::getcreateWithSampleValues).
  4. 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

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

super_romeo created an issue. See original summary.

super_romeo’s picture

Title: array_rand(): Argument #1 ($array) cannot be empty in line 358 of /var/www/html/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php » array_rand(): Argument #1 ($array) cannot be empty in line 358 of ImageItem.php
quietone’s picture

Version: 10.3.x-dev » main

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

yannickoo’s picture

Status: Active » Reviewed & tested by the community

I can confirm that this patch works as expected 🎉 I have experienced this for my SVG media bundle.

quietone’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs title update, +Needs tests, +Possible duplicate, +Bug Smash Initiative

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

dcam’s picture

Status: Needs review » Closed (duplicate)
Issue tags: -Possible duplicate

I 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):

ValueError: array_rand(): Argument #1 ($array) must not be empty in array_rand() (line 357 of core/modules/image/src/Plugin/Field/FieldType/ImageItem.php).
Drupal\image\Plugin\Field\FieldType\ImageItem::generateSampleValue() (Line: 257)
Drupal\Core\Field\FieldItemList->generateSampleItems() (Line: 252)
Drupal\Core\Entity\ContentEntityStorageBase->createWithSampleValues() (Line: 376)
Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::generateSampleValue() (Line: 257)
Drupal\Core\Field\FieldItemList->generateSampleItems() (Line: 252)
Drupal\Core\Entity\ContentEntityStorageBase->createWithSampleValues() (Line: 55)
Drupal\layout_builder\Entity\LayoutBuilderSampleEntityGenerator->get() (Line: 221)
Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage->getContextsDuringPreview() (Line: 51)

I concur that this issue is a duplicate. I'm going to close this issue and update contribution credit.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.