Problem/Motivation
Migrating node content from older Drupal system to Drupal 8 may carry some contents that have image with image_style that doesn't exist in the Drupal 8 system. In our case, image styles (Default, Preview) are exists in Drupal 7, but not in Drupal 8.
After migrating such content, and viewing it, the image module throw an error and preventing the node page render.
Call stack:
<strong>Call to a member function transformDimensions() on null in template_preprocess</strong>
template_preprocess_image_style(Array, 'image_style', Array) (Line: 287)
Drupal\Core\Theme\ThemeManager->render('image_style', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 490)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 54)
__TwigTemplate_b990021d9c53bb62452fd087f46528a41d0dcfebc30a9afe45ba263b28f82647->doDisplay(Array, Array) (Line: 432)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 403)
Twig_Template->display(Array) (Line: 411)
Twig_Template->render(Array) (Line: 64)
twig_render_template('core/themes/classy/templates/field/image-formatter.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('image_formatter', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 450)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 154)
Drupal\entity_embed\Plugin\Filter\EntityEmbedFilter->Drupal\entity_embed\Plugin\Filter\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 155)One way to solve this is to recreate the missing image style in the new system. But in case someone doesn't want that, at lease the page rendering should failed gracefully.
Instead of throw error upfront and prevent the entire page from rendering. I suggest it should "Inform the site builders why their image didn't work." Part of the image module has done that, but not in above senario.
Attached is the patch, open for suggestion.
Steps to reproduce
Have code that tries to render an image style that does not exist anymore:
$build = [
'#theme' => 'image_style',
'#style_name' => 'doesnotexist',
'#uri' => 'public://2026-01/Before patch.png'
];
Proposed resolution
If the image style does not exist, we should avoid triggering a PHP error and instead log a warning.
The image will not be rendered.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #33 | After-apply-patch-2957368-14.png | 1.02 MB | amit.drupal |
| #26 | Before_patch_broken_pic_2.png | 8.3 KB | vikashsoni |
| #26 | before_patch_recent_log_message_error_1.png | 341.7 KB | vikashsoni |
| #23 | error-on-media-upload.png | 256.4 KB | abhisekmazumdar |
Issue fork drupal-2957368
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:
- 2957368-convert-patch-50
changes, plain diff MR !9200
- 2957368-call-to-a
compare
Comments
Comment #2
phuang07 commentedComment #3
berdirThis is an entity_embed content filter. I'm not convinced that it should be handled like that there, preventing a fatal is indeed a good thing, but I think we'd more likely just throw a clear exception.
You could suggest instead to check the image style in entity_embed, having old references in content is a valid use case, but code calling/using a non-existing image style is not something we do not want to fail on (fail nicer yes, fail not at all, not sure..)
Comment #4
alonaoneill commentedAlso space needs to be removed.
Thanks
Comment #5
br0kenI stumbled upon this not having the `media_library` image style which is, however, in the `config/install` of the `media_library` module. Don't know how this happened, but it is.
Comment #6
dasginganinja+1 for my installation with respect to #5. Once I created `media_library` style it worked as expected and I no longer was receiving that error.
Comment #7
marco-s+1 for the reason and solution in #5.
Comment #8
anybodyIt DEFINITELY makes sense to log an error instead if an image style doesn't exist (anymore) instead of a hard error!
+1 for a patch like #2 concerning #3.
Comment #9
renrhaf+1
Comment #10
roman.haluska commented+1
Comment #11
anybodyStill open, I'm switching to the current Drupal version.
This error may also occur if the default image preview imagecache style has been deleted. Even if the form display settings show "No preview", the default image formatter seems to be set in the database by default and leads to this error as JS error on file upload / selection!
Comment #12
jordik commentedReceived this error when creating new custom content with an image field.
At the "Manage form display" of the content entity, in the Image field settings, the "Preview image style" was set to "no preview".
Selecting there a predefined image style, e.g. "Thumbnail 100x100" solved the issue for me.
The error disappeared and the edit form of the content type was properly shown.
Comment #14
ravi.shankar commentedFixed some CS issue and comment #4.
Comment #15
ravi.shankar commentedI think it's ready for review.
Comment #16
tanubansal commentedTested #14, space related issues are resolved mentioned in #4
Comment #17
nathan tsai commentedThis occurred to me when using the Image Widget Crop module. I deleted the only required crop type and had to select another crop type in the "form display" settings. (Thanks JodiK!)
Comment #18
seppe beelprez commented+1 for #12
Comment #19
alexborsody commented14 works for me
Comment #20
pranav45 commentedThanks, @JordiK #12 Works for me.
Comment #22
bjcooper commented#14 works for me
Comment #23
abhisekmazumdarI'm working on Simplytestme issue
Facing the same issue while trying to upload an image via a media entity. The Ajax throws the following error.
The patch #14 works for me.
Comment #24
abhisekmazumdarI tested #23 issue on a vanilla drupal but wan't able to recreate it.
Comment #25
baluertlIndeed, patch #14 solved the exception on the
/media/{MID}/editpage for me as well.Comment #26
vikashsoni commentedAfter patch the issue is fixed
for ref sharing screenshot
Comment #27
anybodyAs manual testing for #14 seems successful, I think we need tests to ensure it won't break again?
Comment #29
safetypinPatch #14 was extremely helpful to me in figuring out exactly what was going wrong. Thanks!
Comment #30
phuang07 commentedComment #31
dqd#30 Thanks for the report and the initial work on the patch. But please do not close a core issue without agreement of core or sub system maintainers or a certain amount of users commenting here, especially not when there is nothing committed yet to core and last comments recommend further work on the issue like stated in #27.
Comment #32
dqdRelated but affecting another preprocess function: #3056841: null in template_preprocess_image_style()
Comment #33
amit.drupal commentedAfter apply #14 I got error.
Comment #34
amit.drupal commentedUpdate Patch #14.
Issues are Fixed.
Comment #35
amit.drupal commentedComment #36
amit.drupal commentedComment #38
wranvaud commentedLooks good to me on Drupal 9.3.8.
Comment #39
quietone commentedThanks for moving this along!
This should be on 10.0.x now, changing version. This is tagged as needing tests and there are none in the patch, changing status. Adding tag for an issue summary update so that there is a 'proposed resolution' for future reviewers and committers.
A brief scan of the patch and I saw this
The user interface standards states to not use 'Please' See, https://www.drupal.org/node/604342#interface-text-style. And I think a link to the image-styles configuration should be used instead of a path.
Comment #40
ameymudras commentedComment #41
virk commentedAmeymudras you are huge!
I have been facing this problem for days. It is resolved. Big thanks !!!!
Comment #42
xaa commented#40 seems also working fine on 9.4.5 for what it's worth. thank you
Comment #43
gaurav-mathur commentedPatch #40 works on drupal version 9.4, 9.5, and drupal 10 successfully. It is resolved the my issue.
Thank you
Comment #44
smustgrave commentedMoving back to NW for the tags
Was tagged for tests in #27
Tagged for IS update in #39
Comment #45
prudloff commented#40 does not apply to 10.1.0.
Here is a reroll.
Comment #46
frozenoctopus commented#12 seems to be the trick for me.
Comment #47
solideogloria commentedOngoing issues should be set to target 11.x
Comment #48
solideogloria commented#45 is necessary for 10.1.x; #40 doesn't work with it.
Comment #49
hoporr commentedPatch #45 worked on Drupal 10.1.6
Comment #50
hoporr commentedConflict with other issue:
After running patch #45, I got this error, ONLY after cache clear
Deprecated function: pathinfo(): Passing null to parameter #1 ($path) of type string is deprecated in Drupal\image\Entity\ImageStyle->supportsUri() (line 385 of core/modules/image/src/Entity/ImageStyle.php).
This error is addressed in this issue, and there is a patch
Deprecated function: pathinfo() on template_preprocess_image_style
https://www.drupal.org/project/drupal/issues/3306131
There is a patch:
https://git.drupalcode.org/project/drupal/-/merge_requests/2680.patch
It just changes one line of code, and adds a saftely check.
I added this into this patch here
- if ($style->supportsUri($variables['uri'])) {
+ if (isset($variables['uri']) && $style->supportsUri($variables['uri'])) {
Comment #51
caspervoogt commented#45 worked for me. I could not get #50 to apply (Drupal 10.1.6)
Comment #53
hzswdef commented#45 applies on D10.2.4
Comment #54
solideogloria commentedYes, I've been using #45 on 10.2 successfully.
Comment #56
dexiecarla commented#50 applies on D10.2.4 and created merge request for the Patch
Comment #57
dexiecarla commentedComment #58
dexiecarla commentedComment #59
dexiecarla commentedComment #61
visabhishek commentedRe-rolled patch for drupal 10.3.2
Comment #62
solideogloria commentedMerge requests need to target 11.x.
Please update the merge request instead of uploading new patches. That way the issue can advance.
Comment #64
b0gucki3 commented#61 works perfectly on 10.3.2, thanks.
Comment #65
dqd#62: Some points made before are still not addressed in the latest patches (like tests) so a re-roll only won't make it. To prevent wasted efforts and resources of contributors, we also maybe need some thoughts of project maintainers if this issue is outdated or still relevant, since it had its most momentum in the Drupal 8 migration live cycle.
Additionally: #57 and below are these tags added in cooperation with the project maintainers or in agreement with meetups on Drupal events? And are these still required? Since there was no comment added to the tags, they fade a little bit into curiosity.
Comment #66
solideogloria commentedI know a reroll isn't all that's needed, and that's why I left the status Needs Work.
Comment #67
dqdUsually NR is rather used to review patches, but it also creates attention. The situation needs some more reviews and opinions like explained in #65. Repeating posted patches triggered by Needs Work just flood the issue but do not bring us closer to a final route to go. Usually this state would require Postponed (maintainer needs more info) ("PMNMI") but since I am not the maintainer of the image framework I would leave it up to the maintainers to choose this Status. There is sadly no Status in between these both.
Further Descriptions of the Priority and Status values can be found in the Drupal project issues documentation.
Comment #68
spudley commentedThis crash caused me a major headache today when a new content type went live on our production system. Our editor tried to import pages from the stage system; they imported successfully but crashed when he tried to open them to edit.
Turned out that the image field in the content type had been configured without a preview image style selected, and was defaulting to "thumbnail", which doesn't exist, and was then causing this crash.
This raises two questions for me:
Firstly, why does the image style default to a style that doesn't exist? If the style doesn't exist, don't use it as a default.
And secondly, why is a crash like this allowed to persist in core? It's a relatively easy crash to make happen, and easy to fix (the patch is already done ages ago), and the crash message itself doesn't include the key detail of what image style is causing the problem, which makes it much harder to diagnose.
Please can this patch get reviewed and pushed into the next release. Thank you. :)
Comment #69
smustgrave commentedComment #70
mdsohaib4242 commentedThis worked for me!
Comment #71
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #72
solideogloria commentedPlease make any changes to the merge request rather than submitting new patches. Thanks.
Comment #73
mdsohaib4242 commentedComment #74
apparatchik commentedI have been bumping into this issue from time to time, and then I come here to grab the latest patch for the current Drupal version.
I also think some simpler patches / fixes shouldn't need to take 8 years and 4 major Drupal versions to get applied, regardless if there are specific tests developed.
Comment #75
eduardo morales albertiWe will commit a validation to know if the URI is set.
Comment #76
eduardo morales albertiNeeds review (Pending to add tests)
Comment #77
smustgrave commentedBefore we review can steps be added to reproduce this?
Based on the summary and the error this seems more like a bug then a feature request.
Comment #78
oily commentedI agree with #77 that we need steps to reproduce so anybody who knows basic site building or greater can reproduce and test the issue.
#16 delivers a set of steps to reproduce:
How does this sound?:
Is it as simple as that? Or are extra steps required?
Comment #79
scotwith1t#61 still applies to 10.3.10. Thanks!
Comment #80
lbesenyei commented#45 works for me
Comment #81
b0gucki3 commentedI can confirm that #45 still works on 11.2.4.
Comment #82
den tweed commented#45 Does not apply on 11.3.x anymore
Comment #83
den tweed commentedFor those who used #45, I re-rolled #45 for 11.3.x
Comment #85
prudloff commentedI fixed conflicts with main (based on the patch from #83) and added a test.
I could not reproduce with steps from #78 but I added a PHP snippet that triggers the problem.
Comment #86
smustgrave commentedAppears to have two open threads, 1 is super minor but the 2nd one may be a bit more hence I didn't just apply suggestion to MR.