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

Issue fork drupal-3011384

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

marcoscano created an issue. See original summary.

marcoscano’s picture

Status: Active » Needs review
StatusFileSize
new4.55 KB

This is a POC for approach 2, feedback appreciated.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Looks great followup

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
--- a/core/lib/Drupal/Component/Utility/Random.php
+++ b/core/lib/Drupal/Component/Utility/Random.php

@@ -278,23 +278,63 @@ public function image($destination, $min_resolution, $max_resolution) {
+    $toolkit_supported_extensions = \Drupal::service('image.factory')->getSupportedExtensions();

Sorry, but we can't have the Random Component rely on the service container or Drupal services.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

ahebrank’s picture

StatusFileSize
new1.1 KB

Here's a minimal fix to prevent a 500 error when the required extension on the image field is not in ['png', 'gif', 'jpg', 'jpeg'].

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

kunalgautam’s picture

StatusFileSize
new244.31 KB

Patch #2 applied successfully for Drupal 9.1 and working fine.

Image item drupal 9.1

kunalgautam’s picture

Status: Needs work » Needs review
johnny5th’s picture

Patch #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.

andypost’s picture

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

andypost’s picture

Status: Needs review » Needs work

Probably core needs decorate component OTOH component can use static cache for capabilities detection of GD or other toolkit

+++ b/core/lib/Drupal/Component/Utility/Random.php
@@ -278,23 +278,63 @@ public function image($destination, $min_resolution, $max_resolution) {
+    $php_extensions = ['jpg', 'jpeg', 'png', 'gif'];
+    $toolkit_supported_extensions = \Drupal::service('image.factory')->getSupportedExtensions();

++ 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

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

pand0rx’s picture

StatusFileSize
new4.63 KB

I've combined both patches into one for Drupal 9.1.x, because patch #2 couldn't be applied to Drupal 9.1.x

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

afsch’s picture

StatusFileSize
new4.54 KB

I updated the patch in favor of Drupal 9.3.x.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

betoaveiga’s picture

StatusFileSize
new896 bytes

This patch only avoids the WSOD if the $extensions array is empty.

mayurgajar’s picture

Assigned: Unassigned » mayurgajar

mayurgajar’s picture

Assigned: mayurgajar » Unassigned
Status: Needs work » Needs review

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

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Test failures seem relevant to the fix

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.