Problem/Motivation
Drupal 10.2 changes the file validation API, replacing the existing upload validators with new ones and deprecating hook_file_validate: https://www.drupal.org/node/3363700.
The logic in Drupal\svg_image\Plugin\Field\FieldWidget\SvgImageWidget needs to be adapted to use the new FileExtension validator to reflect the changes in the FileItem class that the ImageItem class extends: FileItem class in D10.2.
Otherwise this results in 2 extension validators being present: the new FileExtension from the FileItem class and the old one from the SvgImageWidget.
Those 2 validators result in 2 "allowed types" information in the field description in the form and the svg_image one seems to imply svg images are allowed while they may not be:

Steps to reproduce
- Set up a Drupal 10.2 site
- Add the svg_image module
- Create a node type
- Create an image field using the default settings (i.e. with the extensions `png, gif, jpg, jpeg, webp`
- Create a node, check the description of the image field. There are 2 "allowed types", the first one indicating that svg is allowed (see screenshot above).
- Try to upload an svg image, this will result in an error with a message like "Only files with the following extensions are allowed: png gif jpg jpeg webp" which is normal because `svg` was not added to the list of extensions in the image field.
Proposed resolution
Replace the upload validators.
For some backward compatibility until Drupal 11, the logic in the formElement of the SvgImageWidget class, could decide which validator to use (old or new one) based on the drupal version and/or by checking if the `FileExtension` validator is already present.
| Comment | File | Size | Author |
|---|---|---|---|
| #36 | Screen Shot 2024-07-30 at 1.12.16 PM.png | 62.6 KB | pilot3 |
| #36 | Screen Shot 2024-07-30 at 1.11.44 PM.png | 128.83 KB | pilot3 |
| #35 | 3413668-svg-image-widget-drupal-10-2-30.patch | 26.34 KB | altcom_neil |
| #16 | test.png | 16 KB | chaitanyadessai |
| #16 | ext.png | 15.06 KB | chaitanyadessai |
Issue fork svg_image-3413668
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
dineshkumarbollu commentedHi @orakili
I am able to upload svg image without any error.
Comment #3
ad0z commentedIt's working for me with no issues as well, it makes sense at it is marked as deprecated and will be removed from D11.
Comment #4
orakili commentedComment #5
orakili commented@dineshkumarbollu @ad0z Thanks for testing. I've updated the description to better reflect what is happening. The implied issue and the steps to reproduce it were wrong.
Comment #7
jhedstromMoving to NR and adding a patch for use with composer.
Comment #8
jhedstromComment #9
cilefen commentedComment #10
cilefen commentedComment #11
gpz commentedIn my case this patch resolves the multiple display of "Allowed types"
but if I try to upload a svg file is FileIsImage validator says it is not valid and return an error about the type not part of
$supportedExtensions = $this->imageFactory->getSupportedExtensions();
which does not contains "svg"
without this patch I have the double display but in the end it works
Comment #12
sandeep_k commentedI've Tested the shared MR- MR !27 mergeable on Drupal Version- 10.2-dev, I was able to reproduce this issue & the patch was applied successfully.
Testing Steps:
Testing Results:
After applying the patch, the double display is removed & not able to upload the SVG file (As per the mentioned file type)
Comment #13
paulrad commentedCan confirm that after the patch implementation double allowed types disappears but couldn't load svg image anymore.
Moving the issue to "Needs work" status.
Comment #14
chaitanyadessai commentedApplied Patch #8 cleanly and works as expected.
Comment #15
paulrad commentedHi, @chaitanyadessai!
Could you please add SVG to the allowed types list and try to upload an SVG file?
Comment #16
chaitanyadessai commented@paulrad i have configured the image field to support extension type of .svg and verified that it doesnt show svg and couldn't upload svg image.
Comment #17
paulrad commentedThank you, @chaitanyadessai.
So it's basically (not) working the way it was previously described and needs work to be fixed.
Comment #18
znerol commentedPatch #8 is working for me on an existing site with existing config. Haven't tested with a fresh one though.
Comment #21
sthomen commentedThis issue finally bothered me enough so I have put together a MR that makes the module work again. Since i threw out the previous changes made in this issue, I've created a new issue branch. Please do review.
Comment #22
quicksketchThanks @sthomen! We have used your MR on our project and it has fixed all the issues with svg_image. However, the PR is full of white-space issues. It's using a mix of tabs and two spaces all over the place. Looking at the diff (https://git.drupalcode.org/project/svg_image/-/merge_requests/29/diffs) you can spot the lines that have tabs by the "over indentation", since they render as 4 spaces in GitLab.
The changes work overall, and includes new test coverage.
Comment #23
sthomen commented@quicksketch Apologies, my personal preference is tabs so my editor defaults to it. I didn't think of checking for it sneaking some in when I wasn't looking.
Comment #24
dburiak commentedCorrect me if I'm wrong @sthomen but your changes make 3.x not compatible with < Drupal 10.2.
Isn't it better to use DeprecationHelper to support older versions as well?
Comment #25
dburiak commented@orakili I added 2 comments to your PR about DeprecationHelper class and code duplication.
Comment #26
dburiak commentedComment #27
sthomen commented@dburiak You are correct, but I don't think we can use the DeprecationHelper here as (according to my quick check) it was introduced in 10.x and svg_image claims backwards compatibility to 9.3.
Just something simple like this should be sufficient, I think? It will cover the new case and fall back if the new way of doing things isn't being used. The FileIsImage change could even be stuffed in the same block, which alternatively could be a version check for clarity.
Opinions?
Comment #28
sthomen commentedI've made the change I suggested in the MR, but the upload test now fail on Drupal 9.5. I think there's an earlier version of FileIsImage getting in the way.
Comment #29
sthomen commentedWhoops, meant to add a comment last week, but totally forgot about it.
With the last two commits to the MR, all of my tests pass in 9.5 and 10.2. Please review again.
Comment #32
nicrodgersI've tested MR 29 with Drupal 10.2 and can confirm it displays the helper text correctly, validates correctly and enables svg images to be uploaded.
I have not tested with Drupal < 10.2.
I have hidden MR 27 to make it clearer which MR is 'current'.
I reviewed the changes for coding standards, and have pushed a bunch of fixes to bring the files in line with Drupal coding standards.
I am not sure why so much code has been removed in the SvgImageWidget class, relating to settings and preview - is this intentional?
Comment #33
sthomen commentedThank you for cleaning things up, I had no idea I was being so messy. Time to get started with phpcs.
The large removals from SvgImageWidget are because the widget used to inherit directly from FileWidget and duplicated large swathes of ImageWidget. My change is to directly inherit from ImageWidget and just tweak the things that are needed (i.e. adjust the validators and change the theme from image_style to just image on the preview(since an SVG isn't a bitmap, it makes little sense to try to pass it through the image style path).
There's aready an existing issue (#3420037) which is about inheriting from ImageWidget, I didn't know about it at the time I did the change, hopefully this won't cause a merge issue.
Comment #34
isalmanhaider commentedI'm experiencing the same error on Drupal 7.101 with svg_image 7.x-1.2 while the allowed types is set to `jpg svg`
The specified file stats.svg could not be uploaded. The image file is invalid or the image type is not allowed. Allowed types: Only JPEG, PNG and GIF images are allowed.https://www.drupal.org/project/drupal/issues/3458204#comment-15663448
Comment #35
altcom_neil commentedHi, patch seems to work for us in Drupal 10.3
Attached is a patch file for current changes at comment #30 for ease of use.
Cheers, Neil
Comment #36
pilot3 commentedI've also tested MR 29 with Drupal 10.3.0 and can confirm it displays the helper text correctly, and validates correctly and enables svg images to be uploaded.
Comment #37
pilot3 commentedComment #38
martijn de wit#3420037: SvgImageWidget should inherit from ImageWidget instead of FileWidget is also solving the same problem. Would be nice if the maintainer chose / close duplicates.
There are now 3 tickets that are trying to solve sort of the same problem.
Think #3420037: SvgImageWidget should inherit from ImageWidget instead of FileWidget would be the preferred one.
Comment #39
grevil commentedI agree @martijn de wit!
#3420037: SvgImageWidget should inherit from ImageWidget instead of FileWidget is committed in latest dev!