Problem/Motivation
Similar, but not quite the same as #2715859: ImageWidget::validateRequiredFields() produces an error message if an image field is hidden with hook_entity_field_access().
ImageWidget::validateRequiredFields() currently makes an assumption that a #submit array will be present on the return value of $form_state->getTriggeringElement() but that's sometimes not the case (e.g. if a form has been submitted by a non-button via ajax).
This can result in PHP Warnings like this:
Warning: in_array() expects parameter 2 to be array, null given in Drupal\image\Plugin\Field\FieldWidget\ImageWidget::validateRequiredFields() (line 256 of ...docroot/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php).
Proposed resolution
A simple tweak to avoid this would be to check for the presence of the array before using it in a function call e.g.:
public static function validateRequiredFields($element, FormStateInterface $form_state) {
// Only do validation if the function is triggered from other places than
// the image process form.
- if (!in_array('file_managed_file_submit', $form_state->getTriggeringElement()['#submit'])) {
+ if (is_array($form_state->getTriggeringElement()['#submit']) && !in_array('file_managed_file_submit', $form_state->getTriggeringElement()['#submit'])) {
...patch on the way in #15 but minor changes requested in #26.
Remaining tasks
Write patch(done in #15)Review patch.(done in #15 and #26)- Revisions (minor issues found in #26)
- RTBC (success stories for the patch in #15 are in #18, #20, #22, and #27)
- Core maintainer review and feedback
- Commit
User interface changes
None.
API changes
None.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #43 | 2745491-43.patch | 7.88 KB | mcdruid |
Comments
Comment #2
mcdruid commentedComment #3
mcdruid commentedComment #4
mcdruid commentedComment #5
mcdruid commentedActually, if the intention of the check is to:
...the simple approach of checking for the
#submitarray on the triggering element in theifcondition (before looking for a specific key within the array) avoids a possible PHP Warning, but would also mean that the validation doesn't take place if the form's submitted by a non-button. I don't think that's how this is supposed to work.However, there's already an
elseclause in the logic... So I'm taking another look at how to avoid the PHP Warning without stopping the validation from happening when it should, or causing other problems.Comment #6
tim.plunkettComment #7
mcdruid commentedNew patch which reverses the check for the form being submitted by a
file_managed_file_submitbutton, which I think avoids the previously outlined problem where non-button submissions would skip validation.Also changed to use
issetinstead ofis_arrayas with the latter PHP still emits aNotice: Undefined index: #submit.I believe this works okay now (I've verified there are no PHP errors but that the validation runs when the form is submitted via a non-button / ajax callback), but will look at tests next per tim.plunkett's update.
Comment #9
juampynr commentedHere is an alternative version where I am just adjusting the condition. I have verified that this works at the project I am working at, where there is a form with an image field plus a custom field with an AJAX handler. It is when I trigger the custom field's AJAX request that this error happens.
I will now see if I can reproduce this in a test.
Comment #10
juampynr commentedHere are two patches. One is #9 with a test and the other is the test that proves the bug.
Comment #11
juampynr commentedRemoving Need Tests tag.
Comment #13
juampynr commentedComment #14
juampynr commentedIs there anything missing at #10 that I could improve?
Comment #15
mcdruid commentedGreat work getting the test done juampynr!
The only tweak that I'd suggest is that I don't think it's necessary to test for the absence of the PHP Notice:
So here are the same two patches with that removed.
We expect the test-only patch to produce two exceptions:
I ran this past tim.plunkett and he agreed that's sufficient.
Comment #17
mcdruid commentedJust to confirm, the test that failed is the one we expect to fail; it's the proof that there is a problem before the code is changed:
So this just needs further review, and is not awaiting more work AFAIK.
Comment #18
mariacha1 commentedThis patch works great for me (also applies in 8.2!), but given the feedback in #15, I'm setting this to Needs Work.
Comment #19
juampynr commented@mariacha1, I think that what @mcdruid means at #15 is that his patch makes a small adjustment. We believe that there is nothing left to do but waiting for core reviewers to chime in. Thanks for testing the patch though!
Comment #20
tobias.grasse commentedPatch in #15 works great, applied on 8.2.x site.
Comment #22
xsdx commentedTested patch #15 works fine. Fixed issues with Address module when using on same form with imagefield.
Comment #23
jasonawantCross referencing: Adding related issue: https://www.drupal.org/node/2346893. That issue's patch conflicts with this one in /core/modules/image/src/Tests/ImageFieldValidateTest.php.
Comment #24
mcdruid commentedPatch from #15 still applies at the moment, but may need a re-roll if #2346893: Duplicate AJAX wrapper around a file field lands first; thanks for the link @jasonawant
Comment #26
gambryMinor issues:
Inline comments should try to get to 80chars limit as close as possible.
entity_get_form_display() is deprecated. Its docblock gives hints on what to replace it with.
These need to be updated as they refer to a video widget.
Comment #27
mparker17The problem addressed in this issue is occurring for me on a client site running
drupal-8.3.7andaddress-8.x-1.1— selecting a country from the select list in the first step of an address widget fails to AJAX-load the rest of the address widget fields because the validation for an image widget much lower down on the node/add page breaks validation for the address widget.The patch in #15 fixes the issue; but Composer can't apply it automatically because the patch context doesn't match exactly (
Hunk #1 succeeded at 256 (offset 3 lines)whilepatching file core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php), so I'm uploading a copy of the patch in #15, but generated againstdrupal-8.3.xto make Composer happy.Since I've confirmed this is a bug in 8.3.x., and I daresay these are not "disruptive changes", I'm adding the "Needs backport to 8.3.x" tag. Feel free to remove if necessary. There doesn't seem to be a "Needs backport to 8.4.x" tag yet.
Comment #28
mparker17Updating issue summary (especially, Remaining tasks).
Comment #29
gambryThere won't be any more patch releases for 8.3.x so it's unlikely - I would say impossible - that this patch will ever see 8.3.x. Even more 8.2.x which is now close.
So removing both from remaining tasks and removing the Needs backport 8.3.x tag.
Then I'm not 100% sure what 'Backport to 8.4.x' would mean, as we backport between major versions only AFAIK (i.e. D8->D7). Patches are cherry-picked between minor versions by committers and normally it's at their discretion. As this patch is a non-disruptive bugfix has all the chances to get into 8.4.x.
See https://www.drupal.org/core/backport-policy and https://www.drupal.org/core/d8-allowed-changes .
I see what you mean, however I'm using #15 with composer against 8.3.7 and applies nicely.
Comment #30
sanduhrsAddressing minor issues in #26 and some code style fixes in touched files.
Comment #32
rodrigoaguileraJust a rebase
Comment #34
gambryHi #30 and #31. Thanks for your work! Much appreciated.
However your patches are changing pieces of code out of this issue's scope. I can see all of them are trying to fix Coding Standards issues, but they will need to be fixed in a separated issue.
Can you re-roll patch on #15 and apply feedback from #26?
Adding re-roll tag + setting the current patch back to #15.
Also adding Novice tag as both re-rolling task and comments on #26 are not that complicated. (Global sprint weekend 2018 is coming after all, and we need Novice tasks!)
Thanks a lot to everybody!
Comment #35
snehi commentedComment #36
snehi commentedRe-rolled and created the patch. Please review.
Comment #38
mcdruid commentedThanks, but it looks like most of the test(s) got chopped out in that reroll somehow.
Here's another attempt at rerolling patch #15 for 8.5.x
This is just the reroll, I've not tried to address the points in #26 yet.
Comment #39
jofitzAddressed points in #26.
Comment #40
jofitzSorry, only saw this was tagged as Novice after I'd created the patch. :#
Comment #41
snehi commentedLooks great now.
+1 for RTBC.
Comment #42
gambryThank you for the hard work guys!
#26.3 is still outstanding.
Comment #43
mcdruid commentedRemoved references to video widgets etc.. in the comments in DummyAjaxWidget.
Comment #44
snehi commentedLooks ok to me +1 for RTBC.
Comment #45
gambryLooks good to me. The only things I'm not sure is if we should re-write the test to use phpunit instead of simpletest (and so postpone this issue until #2863626: Convert web tests to browser tests for image module is in).
To me it makes sense to add it in there and migrate then all test class at once in its own issue, but not sure if committers will agree.
Adding the related issue and RTBCing.
Thanks everyone for the hard work!
Comment #49
catchCommitted/pushed to 8.6.x and cherry-picked to 8.5.x. Thanks!
It's OK to add the new method here and convert all the tests in one go together.