Problem/Motivation
It's fairly easy to configure a repsonsive image formatter without selecting a responsive image style. The front-end does not warn or give any kind of notice meaning that your typical drupal site builder might think everything is fine. This can be done in both Views and Field display on entities. The result is a fatal error when the content is being rendered:
Call to a member function getBreakpointGroup() on a non-objec
This is happening in template_preprocess_responsive_image.
To reproduce:
• Add image field to an entity
• Select Responsive image formatted to be used for display, but don't select a responsive image style
• Create and view an entity.
Proposed resolution
I'm not sure we are able to get a general long term solution for this in before RC: #2547925: Possible to save malconfigured field display, so we should at least make sure that this doesn't cause a fatal error and instead watchdog log an error and simply don't display anything.
Remaining tasks
Make sure Drupal doesn't make a fatal error.
User interface changes
API changes
None
Beta phase evaluation
| Issue category | Bug because a fatal error is created |
|---|---|
| Issue priority | Major because it's an avoidable fatal error |
| Comment | File | Size | Author |
|---|---|---|---|
| #38 | interdiff.txt | 1.09 KB | googletorp |
| #38 | can_configure-2489162-38.patch | 3.88 KB | googletorp |
| #37 | 2489162-after.png | 87.07 KB | duaelfr |
| #37 | 2489162-before.png | 33.76 KB | duaelfr |
| #33 | interdiff-2489162-27-33.txt | 1.92 KB | rainbowarray |
Comments
Comment #1
duaelfrComment #2
hazaFirst solution from googletorp have an issue : when you did not have created any responsive image style at all, and change the formatter of the field, you won't be able to save the page at all, since there is not even a default style to be selected.
I think, like everywhere else, responsive image should provide a default style from itselft when you enable it. It solves multiple problems :
The attached patch :
Comment #3
duaelfrAccording to your comment that's not needed anymore :)
Comment #4
hazathanks, I just removed this line.
Comment #11
rainbowarrayFixing typo on issue.
I'm not sure that I agree that a default responsive style is the best way to address this, as this default style is not likely to provide good defaults for the wide variety of image use cases where it might end up being selected.
Throwing an error after trying to save the selection of a responsive image formatter with no style selected seems like a much better solution.
Comment #12
hazaif we want to throw an error when no style is selected, we also need to handle the case where no style at all was created in the system. And since we won't let user submit the page, he will be "locked" in the "Manage display" page.
For me, the biggest issue here is that this whould introduce some kind of new behaviour in Drupal. Right now, I can't find any field formatter that does not work with a 100% default settings. The resposive image formatter would be the only one that requiere a specific configuration before you'll be able to use it.
Comment #13
rainbowarrayComment #14
googletorp commentedNo reason to postpone this.
Comment #15
googletorp commentedCorrected the title
Comment #16
rainbowarrayThe proposed solution in the patch is to add a default responsive image style and associated breakpoints. However the defaults in the patch, while well-intentioned, aren't terribly useful. If we are going to add defaults, they should be sensible. That's what #2513604: Create default responsive image styles does. Hence, this issue is being postponed on that going in, which will go a long way towards solving this issue.
Comment #17
googletorp commented#16 please read IS before postponing issue
Comment #18
googletorp commentedUpdated IS, also #2513604: Create default responsive image styles is really not relevant for this issue, as the idea about a default responsive image style has been thrown away as a possible solution for this error.
Comment #19
googletorp commentedAdded the test which we want to fix
Comment #20
googletorp commentedAnd added the patch that fixes this problem.
Comment #21
googletorp commentedAdded beta evaluation
Comment #23
rainbowarrayComment #24
rainbowarrays/fetal/fatal
Comment #25
rainbowarrayManually tested this.
If I go enable responsive images, then configure the image field on the article to use the responsive image field formatter and then press save, when I add content with an image then the page displays but with no image at all. There is no warning as a site builder that I did anything wrong when I press save on manage display. That seems like a problem.
If when I select the responsive image field formatter, I go to configure the field formatter but don't select a responsive image style, then pressing update returns me to the same field formatter config with an error message. That seems like a correct behavior: I shouldn't be able to leave that field formatter empty.
#2547925: Possible to save malconfigured field display indicates this is a general problem for the manage display tab, not solely for the responsive image field formatter.
I'd rather see the root problem fixed rather than letting a site builder think everything is okay until they view their content and the image does not show up.
Comment #26
rainbowarrayTested this situation without this patch. That fatal error is definitely worse than what is happening with this patch applied. I'd still rather see the root problem fixed, but if the typo is fixed, I'll mark this RTBC.
Comment #27
googletorp commentedFixed the typo.
Also note that the error is logged, so it will appear in watchdog if database logging is enabled.
Comment #28
rainbowarraySorry I missed this the first time through, but we should not use contractions in comments as per the coding standards.
Maybe:
"If a responsive image style is not selected, log the error and stop execution."
Comment #29
googletorp commentedChecked with coding standards + jhongdon.
Coding standards is not against contractions: coding-standards.
If that is the only objection I think this is RTBC, let's not waste time here arguing contractions please.
Comment #30
rainbowarrayThanks for working on this, googletorp and Haza.
Comment #31
swentel commentedHmm, why don't we do the same as template_preprocess_image_formatter() does ? If there's no image style, it defaults to #theme => 'image. That actually makes more sense to me as you will actually see an image (probably a 'big' one then, so you'll see the 'problem' way faster), instead of seeing nothing and a watchdog entry. Default to at least something looks more elegant to me. And then at least both formatters act consistently.
- edit (added code example) -
For reference, here's the top of template_preprocess_image_formatter()
I'm tempted to set it to needs review/work, but I'll let core committers decide to push back or not.
Comment #32
catchYes that makes more sense to me too.
Comment #33
rainbowarrayComment #34
rainbowarrayI think we may still want the error detection in preprocess_responsive_image, because it is very possible to create a responsive image render element in code without using the formatter (and sometimes necessary to do so from my experience).
So, to be clear, by falling back to an image, are we falling back to the original image? Or does this fall back to the image style selected as the fallback? Or do we fall back to the fallback style, but if there isn't one, then we fallback to the original image.
I may be up too late.
Comment #35
googletorp commentedThanks for making the patch for this mdrummond - I've been wanting to do it myself, but haven't had the time.
I think this is good to go, would be nice to kill a fatal error .
Comment #36
duaelfrThank you very much @mdrummond for that patch!
I have a few comments, though.
Why do you use concatenation here? It seems more readable to only use a simple string.
You should use $this->container->get('entity.manager') here as done a few lines before in the same test. That ensure that the container can be mocked if necessary for the tests.
To answer your questions in #34 I'd say that it would be nice to have that fallback to the fallback image style then to the original image but it can be postponed to 8.1 as soon as this fatal error is fixed.
Comment #37
duaelfrAnd there is the functional review.
Before:

After:

\o/
Comment #38
googletorp commentedComment #39
rainbowarrayI'm not sure I think that falling back to the fallback style would be the best behavior. I just wasn't sure what would happen.
But now I've remembered that the fallback style is defined within the responsive image style, not the field formatter. So if there's no valid responsive image style, then original image it is.
Just tested this, and it works fine. Looks like code changes requested have been made.
It does feel strange that you can select a responsive image formatter, not select a responsive image style and get no onscreen warning about that. There really should be some sort of user message about that.
That's a separate issue, however, and we can file a follow-up issue for that. Moving this back to RTBC.
Comment #40
duaelfrYou're totally right, I think that's the best we can do without a deep change in the way that formatter settings are validated.
In the best possible case, submitting the view mode form would validate each formatter settings to ensure that everything is good. It seems to be quite hard to achieve right now, though.
Comment #41
webchickHaving some kind of default here is a lot better for UX as well.
Committed and pushed to 8.0.x. Thanks!