Problem/Motivation
PHP displays warning when choosing image formatter "URL to image" on 9.4 fresh installation.
The new attribute 'loading' was introduced with $image_loading = $this->getSetting('image_loading');
on Drupal 9.4 #3173180: Add UI for 'loading' html attribute to images in web/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php
Yet the Class extending it web/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php
does not make use of it while making its parent calls, and doesn't provide the missing setting in its defaultSettings()
function. Therefore returning NULL on the getSetting function, which gives the warning below on building settingsForm and settingsSummary.
Warning: Trying to access array offset on value of type null in Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter->settingsForm() (line 167 of /app/web/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php)
#0 /app/web/core/includes/bootstrap.inc(347): _drupal_error_handler_real(2, 'Trying to acces...', '/app/web/core/m...', 167)
#1 /app/web/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php(167): _drupal_error_handler(2, 'Trying to acces...', '/app/web/core/m...', 167)
#2 /app/web/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php(35): Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter->settingsForm(Array, Object(Drupal\Core\Form\FormState))
#3 /app/web/core/modules/field_ui/src/Form/EntityDisplayFormBase.php(446): Drupal\image\Plugin\Field\FieldFormatter\ImageUrlFormatter->settingsForm(Array, Object(Drupal\Core\Form\FormState))
#4 /app/web/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php(40): Drupal\field_ui\Form\EntityDisplayFormBase->buildFieldRow(Object(Drupal\field\Entity\FieldConfig), Array, Object(Drupal\Core\Form\FormState))
#5 /app/web/core/modules/field_ui/src/Form/EntityDisplayFormBase.php(209): Drupal\field_ui\Form\EntityViewDisplayEditForm->buildFieldRow(Object(Drupal\field\Entity\FieldConfig), Array, Object(Drupal\Core\Form\FormState))
#6 /app/web/core/lib/Drupal/Core/Entity/EntityForm.php(106): Drupal\field_ui\Form\EntityDisplayFormBase->form(Array, Object(Drupal\Core\Form\FormState))
Steps to reproduce
- Install Latest 9.4.x dev site
- Modify article's image display settings /admin/structure/types/manage/article/display
- Change Image format from 'Image' to 'URL to image'
- Head to /admin/reports/dblog to see the php warning report
Proposed resolution
1. Skip the generation of loading related array if setting retrieved is NULL.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#61 | 3291622-61.patch | 6.3 KB | smustgrave |
#61 | interdiff-51-61.txt | 1.19 KB | smustgrave |
#16 | 3291622-16-tests-only.patch | 1.57 KB | smustgrave |
#16 | interdiff-MR-16.txt | 3.06 KB | smustgrave |
#7 | after_patch.png | 406.55 KB | smustgrave |
Issue fork drupal-3291622
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:
- 3291622-formatter-url-to changes, plain diff MR !2416
Comments
Comment #3
naveenvalechaThank you!
Comment #4
LendudeHmm not sure about this.
ImageUrlFormatter
is loading the settingsForm from the parent but not loading the default settings from the parent, so that discrepancy is what the root cause of this problem is.See
Base64ImageFormatter
for what works: that inherits neither the default settings nor the settingsForm, so that doesn't break.Don't know if this should either inherit both or should inherit neither, but I think the fact that it's doing one of the two is what causes this to break.
Comment #5
g-brodieiHi @Lendude, according to your feedback, I'd went through the use of ImageBase64Formatter, seems like the missing puzzle on ImageUrlFormatter was to add its parent settings as a starting point.
Add additional commit to revert the changes, and added
+ parent::defaultSettings();
Thanks!
Comment #6
g-brodieiBack to need review
Comment #7
smustgrave CreditAttribution: smustgrave at Mobomo commentedTested the PR
Confirmed the issue without the patch
Confirmed the PR fixed the issue.
Attaching screenshots
Comment #8
SnowCoder CreditAttribution: SnowCoder commentedI'm having the same issue on my site. Is there a patch for this at all?
Comment #9
smustgrave CreditAttribution: smustgrave at Mobomo commentedThere's a PR up.
Comment #10
SnowCoder CreditAttribution: SnowCoder commented@smustgrave I've never had any experience using the PR before. I've always just come to find patches that I applied and have been pretty comfortable with. I seem to be a bit confused about how I would apply this to my site? Its composer run with the drupal/recommended-project starter.
Comment #11
smustgrave CreditAttribution: smustgrave at Mobomo commentedGotcha
If you open the PR https://git.drupalcode.org/project/drupal/-/merge_requests/2416 you can append .diff or .patch to the end of the URL and you can past that into composer.json file and use it just like a patch
Comment #12
SnowCoder CreditAttribution: SnowCoder commentedPerfect. Thanks for being helpful and teaching me something new :)
Comment #13
smustgrave CreditAttribution: smustgrave at Mobomo commentedIt was pointed out to me you shouldn't point directly to the URL but download the file instead. For security purposes.
Comment #14
quietone CreditAttribution: quietone at PreviousNext commentedI have only skimmed the IS and the comments. I notice that the changes made to the code in #5 have been tested but have not had a code review.
Setting to NR for a code review.
Comment #15
LendudeThe change looks good, but the fact that just using this plugin should trigger this warning, it feels we have no test coverage for this plugin, and we should probably add it.
Comment #16
smustgrave CreditAttribution: smustgrave at Mobomo commentedTook a shot at the test.
While testing with the original MR I was getting
Drupal\Core\Config\Schema\SchemaIncompleteException : Schema errors for core.entity_view_display.node.article.default with the following errors: core.entity_view_display.node.article.default:content.qymfsdps.settings.image_link missing schema, core.entity_view_display.node.article.default:content.qymfsdps.settings.image_loading missing schema
So maybe the default settings aren't needed because we aren't using those settings? Could be wrong.
Comment #17
ambikahirode CreditAttribution: ambikahirode as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedPatch in #16 Applied Cleanly and working fine on local 9.4
Comment #18
danflanagan8@smustgrave, I have ideas about the schema validation error.
The schema for the image_url formatter is as follows:
When the
parent::defaultSettings()
are added, we get additional settings forimage_link
andimage_loading
. Those settings are not defined in the schema for the image_url formatter, so that is going to lead to schema validation errors.So I would say definitely do not want to add the parent's default settings. And they aren't even needed as long as the ImageFormatter can account for empty values of
image_loading
andimage_link
, which I think has to be the goal here.Probably at this point it no longer makes sense for ImageUrlFormatter to extend ImageFormatter. But untangling that would be a lot harder than simply making the ImageFormatter code a bit more defensive.
Hope that helps!
Comment #19
carolpettirossi CreditAttribution: carolpettirossi at ImageX commentedI've just tested patch #16, and it renders the image instead of just printing the URL/path.
Comment #20
ameymudras CreditAttribution: ameymudras at Salsa Digital commentedTested on Drupal 9.4.x and php 8.1
- Issue summary is clear and describes the problem
- was able to reproduce the issue
- The patch applies cleanly and fixes the issue
- Tests pass
Marking this as RTBC
Comment #21
ppblaauw CreditAttribution: ppblaauw as a volunteer commentedApplied #16 Works
Comment #22
danflanagan8I believe 100% that the patch fixes the pho error. And the approach in the MR is 100% not what we want, as described in #18. But we can't set this to RTBC yet. We still need a fail test.
@smustgrave made an excellent attempt in #16 for a tests-only patch, but the tests passed. We as a community still need to get the fail test sorted out.
Comment #23
smustgrave CreditAttribution: smustgrave at Mobomo commentedComment #25
danflanagan8Thanks, @smustgrave! Fail test fails now, but I think it can be meaningfully improved.
1. I think
testImageLoadingAttribute()
is the wrong place for this since it doesn't really have anything to do with the loading setting. I would prefer to see this in_testImageFieldFormatters
, which is in the same class. At the very end of that method there are some existing assertions related to theimage_url
formatter. That seems like the most logical place to put an additional assertion like this.2. I think you can drop the assertion related to the absence of the php warning. If there is a warning, the test will fail before even getting to that assertion.
You should be able to just assert that some expected settings summary text is present.
Comment #26
smustgrave CreditAttribution: smustgrave at Mobomo commentedsure. Made those updates.
Comment #27
danflanagan8@smustgrave, I'm channeling my inner committer and the test is not quite there. I'm feeling like a should have been clearer and more expansive on a couple things in my previous comment.
More accurately we're testing that the settings summary is correct for the image_url formatter. I don't think we want this comment to be so specific to the error in this issue. Can we change to something like "Test the settings summary."?
After getting to the manage display page, we should assert that the settings summary appears as expected, because that's at the heart of what is being tested here. I think the expected summary would be "image_style: Thumbnail (100×100)". Can we assert that this text is present?
I also forgot to comment on this in my previous notes. This looks out of scope and we should revert this change. Even positive changes like this tend to hold up commits when they are out of scope.
Sorry again that my previous comment was not clearer. I swear I'm not trying to make extra work for you!
Comment #28
smustgrave CreditAttribution: smustgrave at Mobomo commentedNo I appreciate all the rapid feedback!
Comment #30
smustgrave CreditAttribution: smustgrave at Mobomo commentedComment #31
danflanagan8Almost done, @smustgrave!
There are two lines at the end of you patch that are duplicated a few lines above. Can you remove these lines?
Then I'll flip the switch to RTBC! Thanks for all the hard work getting this across the finish line.
Comment #32
smustgrave CreditAttribution: smustgrave at Mobomo commentedSure!
Comment #34
danflanagan8Those fails all look unrelated, but not random. I think head is broken.
Can we get the test only patch too, @smustgrave? This version of the test has not failed yet here. In #28 it failed because of the logged out state. We need to run it and show that it fails because of the settings summary bug.
I also just noticed we're targeting the wrong branch. I wouldn't expect any re-rolling to be required though.
Back to NW to get the latest test only patch.
Comment #35
smustgrave CreditAttribution: smustgrave at Mobomo commentedRe uploaded the full patch too
Comment #37
danflanagan8Alright! That fail test triggers the exact error described in the IS:
The patch fixes it.
The patch has been used by the community.
RTBC!
Comment #38
smustgrave CreditAttribution: smustgrave at Mobomo commentedthanks for all the help!
Comment #40
catchLet's open a follow-up to make ImageUrlFormatter extend directly from ImageFormatterBase instead of ImageFormatter. It looks like the new test coverage will still be relevant once we've done that though too which is great.
Comment #41
quietone CreditAttribution: quietone at PreviousNext commentedAdding tag for #40.
Comment #42
smustgrave CreditAttribution: smustgrave at Mobomo commentedOpened https://www.drupal.org/project/drupal/issues/3306066
Comment #43
lauriiiIsn't the config schema for
image_loading
also incorrect given that the value is not marked as nullable?Comment #44
smustgrave CreditAttribution: smustgrave at Mobomo commentedcan you elaborate more please?
Comment #45
danflanagan8Thanks for the review @lauriii
I only find three instances in core of
nullable:
so it does not appear to be in common usage within our schema. I had never heard of this before.Locally, I added
nullable: true
to image_loading and the fail test still fails. To me that indicates that the bug here is unrelated to image_loading being specified as nullable. If the schema should be updated, it should probably be done in a separate issue.I'm going to throw this back to RTBC.
Comment #46
jennypanighetti CreditAttribution: jennypanighetti commentedPatch #32 worked for me on 9.4.5
Comment #47
KurtTrowbridgeConfirming that #35 fixed the issue for me as well on two sites, both on 9.4.5. Thanks!
Comment #48
catchLooking at #3306066: Make ImageUrlFormatter extend directly from ImageFormatterBase I think we should fold it back into this issue. It might mean we don't need to change any logic except for what gets subclassed - test coverage will show whether or not that's the case.
Comment #49
smustgrave CreditAttribution: smustgrave at Mobomo commentedMoved over changes from https://www.drupal.org/project/drupal/issues/3306066 and closed it out.
Comment #51
smustgrave CreditAttribution: smustgrave at Mobomo commentedComment #53
smustgrave CreditAttribution: smustgrave at Mobomo commentedNot sure why it retested #49 so please ignore that.
Comment #54
dhirendra.mishra CreditAttribution: dhirendra.mishra at Axelerant for Axelerant commentedHere is the patch against 9.4.5 where only two below notices are coming.
Notice: Trying to access array offset on value of type null in /mnt/www/html/veoliad801dev4/docroot/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php on line 210
Notice: Trying to access array offset on value of type null in /mnt/www/html/veoliad801dev4/docroot/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php on line 167
Comment #55
catchCan these hunks be dropped from the patch if the url formatter no longer needs them?
Comment #56
gajendra_sharma CreditAttribution: gajendra_sharma at Dotsquares Ltd. commentedPatch No. #51 tested and applied successfully on drupal 9.4.x version.
Comment #57
catchStill needs review and an answer to #55.
Comment #58
danflanagan8Hi, @gajendra_sharma! Thanks for jumping in here.
We as a community need to be careful about setting an issue to RTBC. That's the sign to core committers that the issue is ready for their attention. Core committers have very little precious time, and we don't want them to waste any of it reviewing issues that aren't actually ready.
If you want to update the patch #54 to address the comments in #55, that would be welcome. And any new patch should (almost) always be accompanied by an interdiff.
If you need help with contribution, the #contribute channel in Drupal Slack is a great resource. And this issue has been tagged by the Bug Smash Initiative, so the #bugsmash channel would be a good place to turn as well.
Cheers!
Comment #59
smustgrave CreditAttribution: smustgrave at Mobomo commentedMoving back to 9.5.x not sure why it was moved to 9.4
#51 should be used going forward for changes. before breaking changes start getting mixed in. Hiding #54 and #56
Looking into #55
Comment #60
smustgrave CreditAttribution: smustgrave at Mobomo commentedComment #61
smustgrave CreditAttribution: smustgrave at Mobomo commentedRemoving those changes mentioned in #55 didn't cause any issues.
Comment #62
catch#61 looks like I hoped it would - no longer needs the special casing.
Comment #63
alexpottCommitted and pushed b8ad2d90b4 to 10.1.x and 3cdcdbc45f to 10.0.x and a3c8bb27f1 to 9.5.x. Thanks!
Fixed coding standards on commit.
Also fixing use of t() in plugins... shoudl be $this->t()