Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I have a document file element with conditional visibility. On the webform in the front-end, after selecting a file, an ajax request should be triggered and the widget markup should be updated (the selected file should show up, the button should change from Choose to Remove etc) but this doesn't happen.
A difference that I noticed in the markup between the element with states and without states is this div that wraps the markup, including the ajax wrapper div (<div id="ajax-wrapper">
). Maybe this is somehow at fault:
<div class="js-form-wrapper" data-drupal-states="...">
Comment | File | Size | Author |
---|---|---|---|
#13 | webform.webform.issue_2937674.yml | 3.46 KB | jrockowitz |
#11 | files_with_states_does_not_work_correctly-2937674-11.patch | 895 bytes | ElegguaDP |
|
Comments
Comment #2
adinac CreditAttribution: adinac as a volunteer commentedComment #3
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe below two core issue are most likely the problem.
#2346893: Duplicate AJAX wrapper around a file field
#2847425: #states not affecting visibility/requirement of managed_file
A simple workaround is to wrap your image upload in a container which manages the conditional logic (#states).
Comment #4
adinac CreditAttribution: adinac as a volunteer commented@jrockowitz I applied both of the patches for the core issues but it still does not work. I will try the workaround you suggested.
Comment #5
adinac CreditAttribution: adinac as a volunteer commentedThe workaround did the trick for the conditional visibility part but my problem is that the document field should also be required (conditionally)...
Do you have any suggestions?
Comment #6
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI think you could apply required conditional logic to a file upload and server-side validation will check for the file.
Comment #7
sanyok_medved CreditAttribution: sanyok_medved at ImageX commentedOr you can simulate click on upload button for your file.
Comment #8
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI postponing this issue because it is a core related issue.
Comment #9
Yunqiang CreditAttribution: Yunqiang commentedI have found the same issue. After doing a little bit investigation, I have found the library has been changed if we add some condition for the managed_file element.
So my workaround is simple: add following code into template_preprocess_form_element()
if(!empty($vars['element']['#type']) && $vars['element']['#type'] == 'managed_file') {
$vars['#attached']['library'][] = 'file/drupal.file';
}
After attach the file library it works as expected.
Of course need to do more code digging to see which part change the library for this scenario.
Comment #10
ElegguaDP CreditAttribution: ElegguaDP commentedWell, I found some solution for that problem. Unfortunately I should work with old version
8.x-5.0-rc6
, so I'll attach two patches - fordev
and for8.x-5.0-rc6
.Trouble is in
webform.states.js
.States try to trigger events for elements with conditions, and also run ajax for file input. That's why our file input crashed.
Comment #11
ElegguaDP CreditAttribution: ElegguaDP commentedAnd patch for current version
8.x-5.x-dev
Comment #12
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #13
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI can't replicate this issue using the attached webform and the bartik theme.
What theme are you using?
Comment #14
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #15
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI think this issue was fixed via #2975533: Do not trigger file upload event when element is hidden or shown via #states API
Comment #16
brianjcook CreditAttribution: brianjcook commentedI'm also experiencing the orginial issue, though as in #13, I can't reproduce on simplytest.me. Nevertheless, here's the YAML:
Comment #17
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedCan you replicate this issue using the Bartik theme?
Comment #18
brianjcook CreditAttribution: brianjcook commentedThanks for the quick reply. It works as expected using the Bartik theme, and is breaking on a subtheme of the Stable theme.
Comment #19
andrewkamm CreditAttribution: andrewkamm as a volunteer commentedWe were able to get this running by updating
[theme]/templates/content-edit/file-managed-file.html
in our theme to match Bartik (I work with @brianjcook). Specifically, our file lacked{{ attach_library('classy/file') }}
and thejs-form-managed-file
CSS class.