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

Issue fork drupal-2957368

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

ph53 created an issue. See original summary.

phuang07’s picture

StatusFileSize
new3.78 KB
berdir’s picture

This 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..)

alonaoneill’s picture

Status: Needs review » Needs work
+++ w/core/modules/image/image.module
@@ -289,48 +289,55 @@ function image_style_options($include_empty = TRUE) {
+      '@style' => $variables["style_name"],      ¶

Also space needs to be removed.
Thanks

br0ken’s picture

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

dasginganinja’s picture

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

marco-s’s picture

+1 for the reason and solution in #5.

anybody’s picture

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

renrhaf’s picture

+1

roman.haluska’s picture

+1

anybody’s picture

Version: 8.5.1 » 9.1.x-dev

Still 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!

jordik’s picture

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

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.

ravi.shankar’s picture

StatusFileSize
new3.77 KB
new920 bytes

Fixed some CS issue and comment #4.

ravi.shankar’s picture

Status: Needs work » Needs review

I think it's ready for review.

tanubansal’s picture

Tested #14, space related issues are resolved mentioned in #4

nathan tsai’s picture

This 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!)

seppe beelprez’s picture

+1 for #12

alexborsody’s picture

14 works for me

pranav45’s picture

Thanks, @JordiK #12 Works for me.

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.

bjcooper’s picture

#14 works for me

abhisekmazumdar’s picture

StatusFileSize
new256.4 KB

I'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.

abhisekmazumdar’s picture

I tested #23 issue on a vanilla drupal but wan't able to recreate it.

baluertl’s picture

Indeed, patch #14 solved the exception on the /media/{MID}/edit page for me as well.

vikashsoni’s picture

After patch the issue is fixed
for ref sharing screenshot

anybody’s picture

Issue tags: -image +Needs tests

As manual testing for #14 seems successful, I think we need tests to ensure it won't break again?

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.

safetypin’s picture

Patch #14 was extremely helpful to me in figuring out exactly what was going wrong. Thanks!

phuang07’s picture

Status: Needs review » Fixed
dqd’s picture

Status: Fixed » Needs work

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

dqd’s picture

Related but affecting another preprocess function: #3056841: null in template_preprocess_image_style()

amit.drupal’s picture

StatusFileSize
new1.02 MB

After apply #14 I got error.

amit.drupal’s picture

StatusFileSize
new1.02 MB

Update Patch #14.

Issues are Fixed.

amit.drupal’s picture

StatusFileSize
new4.54 KB
amit.drupal’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 35: 2957368-34.patch, failed testing. View results

wranvaud’s picture

Status: Needs work » Reviewed & tested by the community

Looks good to me on Drupal 9.3.8.

quietone’s picture

Version: 9.4.x-dev » 10.0.x-dev
Issue summary: View changes
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update

Thanks 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

+++ b/core/modules/image/image.module
@@ -252,48 +252,56 @@ function image_style_options($include_empty = TRUE) {
+    \Drupal::logger('image')->warning('Image style (@style) missing for @image. Please add the missing style under /admin/config/media/image-styles.', [

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.

ameymudras’s picture

Status: Needs work » Needs review
StatusFileSize
new4.54 KB
new1.66 KB
virk’s picture

Ameymudras you are huge!

I have been facing this problem for days. It is resolved. Big thanks !!!!

xaa’s picture

#40 seems also working fine on 9.4.5 for what it's worth. thank you

gaurav-mathur’s picture

Patch #40 works on drupal version 9.4, 9.5, and drupal 10 successfully. It is resolved the my issue.
Thank you

smustgrave’s picture

Status: Needs review » Needs work

Moving back to NW for the tags

Was tagged for tests in #27
Tagged for IS update in #39

prudloff’s picture

StatusFileSize
new4.43 KB

#40 does not apply to 10.1.0.
Here is a reroll.

frozenoctopus’s picture

#12 seems to be the trick for me.

solideogloria’s picture

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

Ongoing issues should be set to target 11.x

solideogloria’s picture

#45 is necessary for 10.1.x; #40 doesn't work with it.

hoporr’s picture

Patch #45 worked on Drupal 10.1.6

hoporr’s picture

Conflict 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'])) {

caspervoogt’s picture

#45 worked for me. I could not get #50 to apply (Drupal 10.1.6)

Primsi made their first commit to this issue’s fork.

hzswdef’s picture

#45 applies on D10.2.4

solideogloria’s picture

Yes, I've been using #45 on 10.2 successfully.

dexiecarla made their first commit to this issue’s fork.

dexiecarla’s picture

#50 applies on D10.2.4 and created merge request for the Patch

dexiecarla’s picture

dexiecarla’s picture

dexiecarla’s picture

pameeela changed the visibility of the branch 2957368-call-to-a to hidden.

visabhishek’s picture

StatusFileSize
new4.8 KB

Re-rolled patch for drupal 10.3.2

solideogloria’s picture

Merge requests need to target 11.x.

Please update the merge request instead of uploading new patches. That way the issue can advance.

b0gucki3’s picture

#61 works perfectly on 10.3.2, thanks.

dqd’s picture

Status: Needs work » Needs review

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

solideogloria’s picture

I know a reroll isn't all that's needed, and that's why I left the status Needs Work.

dqd’s picture

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

spudley’s picture

This 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. :)

smustgrave’s picture

Issue tags: +Needs tests
mdsohaib4242’s picture

StatusFileSize
new4.22 KB

This worked for me!

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new92 bytes

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

solideogloria’s picture

Please make any changes to the merge request rather than submitting new patches. Thanks.

mdsohaib4242’s picture

apparatchik’s picture

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

eduardo morales alberti’s picture

We will commit a validation to know if the URI is set.

eduardo morales alberti’s picture

Status: Needs work » Needs review

Needs review (Pending to add tests)

smustgrave’s picture

Category: Feature request » Bug report
Status: Needs review » Needs work

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

oily’s picture

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

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

How does this sound?:

  1. Install Drupal 11 using the standard profile and the default theme.
  2. Add an image field to the Basic page content type.
  3. At the "Manage form display" of the content entity, in the Image field settings, set the "Preview image style" to "no preview".
  4. The error message will prevent the edit page for the Basic content type from rendering. It will fill the page.

Is it as simple as that? Or are extra steps required?

scotwith1t’s picture

#61 still applies to 10.3.10. Thanks!

lbesenyei’s picture

#45 works for me

b0gucki3’s picture

I can confirm that #45 still works on 11.2.4.

den tweed’s picture

#45 Does not apply on 11.3.x anymore

den tweed’s picture

StatusFileSize
new4.67 KB

For those who used #45, I re-rolled #45 for 11.3.x

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.

prudloff’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs tests

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

smustgrave’s picture

Status: Needs review » Needs work

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