Problem/Motivation
After #1014816: Allow image fields to use any extensions the current image toolkit supports (instead of hard-coding jpg, png and gif only), it's possible to use all supported extensions in image fields. However, \Drupal\image\Plugin\Field\FieldType\ImageItem::generateSampleValue() and \Drupal\Component\Utility\Random::image() still assume jpg, png and gif are the only extensions that could exist.
One way of reproducing this issue is:
- Setup a site where Imagemagick is the active toolkit
- Create a media type called "Icon", with an image file that allows only SVG files to be uploaded
- Create a content type that has an entity reference to this media type
- Go to the "Manage Display" of this content type, enable layout builder
- Click "Manage Layout"
You will have a WSOD because it tries to create an SVG sample, and obviously fails.
Proposed resolution
I can see 2 possible approaches:
1) Add a new method to \Drupal\Core\ImageToolkit\ImageToolkitInterface (and implement it in ImageToolkitBase) so that the toolkit plugin is responsible for generating a sample image with given extension and dimensions. So \Drupal\Component\Utility\Random::image() would just call that, instead of assuming the extensions supported or how to generate images.
2) Modify \Drupal\Component\Utility\Random::image(), allowing for other extensions to be special-cased.
Remaining tasks
1) Decide on approach
2) Write patch + tests
3) Review
4) Commit
User interface changes
None
API changes
TBD
Data model changes
None
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | issue-3011384.patch | 896 bytes | betoaveiga |
| #18 | imageitem-random-3011384-18.patch | 4.54 KB | afsch |
| #16 | 2011384-16.patch | 4.63 KB | pand0rx |
Issue fork drupal-3011384
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 #2
marcoscanoThis is a POC for approach 2, feedback appreciated.
Comment #3
andypostLooks great followup
Comment #4
larowlanSorry, but we can't have the Random Component rely on the service container or Drupal services.
Comment #7
ahebrank commentedHere's a minimal fix to prevent a 500 error when the required extension on the image field is not in
['png', 'gif', 'jpg', 'jpeg'].Comment #9
kunalgautam commentedPatch #2 applied successfully for Drupal 9.1 and working fine.
Comment #10
kunalgautam commentedComment #11
johnny5th commentedPatch #2 applied successfully and resolved my problem.
I had a SVG only image on a field. Opening up Layout Builder for the page was throwing a 500 error.
Comment #12
andypostComment #14
andypostProbably core needs decorate component OTOH component can use static cache for capabilities detection of GD or other toolkit
++ to more extensive detection of capabilities of GD
As PHP 7.3 the linked GD and image extensions support could vary, see #2340699: Let GDToolkit support WEBP image format
Comment #16
pand0rx commentedI've combined both patches into one for Drupal 9.1.x, because patch #2 couldn't be applied to Drupal 9.1.x
Comment #18
afschI updated the patch in favor of Drupal 9.3.x.
Comment #22
betoaveigaThis patch only avoids the WSOD if the $extensions array is empty.
Comment #23
mayurgajar commentedComment #25
mayurgajar commentedAdded MR: https://git.drupalcode.org/project/drupal/-/merge_requests/14031
Which extend the support the extensions.. ['jpg', 'jpeg', 'png', 'gif']
It needs review to work further more.. on concreate solution.. this might be one option.
Steps:
- Setup a site where Imagemagick is the active toolkit
- Create a media type called "Icon", with an image file that allows only SVG files to be uploaded
- Create a content type that has an entity reference to this media type
- Go to the "Manage Display" of this content type, enable layout builder
- Click "Manage Layout"
Apply the MR and check.
Comment #26
smustgrave commentedTest failures seem relevant to the fix