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.
Problem/Motivation
[Drupal 8.6.x] #states breaks when OR is used.
The below Form API code from the Webform module…
select:
'#type': select
'#title': select
'#options':
1: 1
2: 2
number:
'#type': number
'#title': number
textfield:
'#type': textfield
'#title': textfield
'#states':
visible:
- ':input[name="select"]':
value: 1
- or
- ':input[name="number"]':
value: 1
…or PHP code (which can be added to any form)…
$form['select'] = [
'#type' =>'select',
'#title' => 'select 1',
'#options' => [0 => 0, 1 => 1, 2 => 2],
];
$form['number'] = [
'#type' => 'number',
'#title' => 'enter 1',
];
$form['textfield'] = [
'#type' => 'textfield',
'#title' => 'textfield',
'#states' => [
'visible' => [
[':input[name="select"]' => ['value' => '1']],
'or',
[':input[name="number"]' => ['value' => '1']],
],
],
];
Logs the below exception…
This issue is being caused by the changes #2981652: [Aug. 9 2018] Format core JavaScript using recently add Prettier.
When I revert core/misc/states.js from the 8.6.x version to 8.5.x the issue/error is resolved.
I also confirmed (by removing the code) that the enhancements to #states contained in js/webform.states.js is not causing this issue.
Proposed resolution
TBD
Remaining tasks
TBD
User interface changes
N/A
API changes
N/A
Data model changes
N/A
Comment | File | Size | Author |
---|---|---|---|
#28 | interdiff-2995570-19-28.txt | 647 bytes | AaronBauman |
#28 | 2995570-28.patch | 7.7 KB | AaronBauman |
#20 | interdiff-2995570-13-19.txt | 608 bytes | jrockowitz |
#19 | interdiff.txt | 718 bytes | lauriii |
#19 | 2995570-19.patch | 7.69 KB | lauriii |
Comments
Comment #2
GrandmaGlassesRopeManComment #3
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #4
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #5
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe attached patch adds a test form which replicates the issue.
Comment #6
GrandmaGlassesRopeManAlright.
@jrockowitz - This was not related to the Prettier change at all. This actually happened in #2925064: [1/2] JS codestyle: no-restricted-syntax where we tried to move away from using restricted syntax, including
for...in
loops.This patch reverts that minor change + Prettier changes because this code is slightly different. I'm not 💯👌 with this and we should probably figure out a better solution.
Comment #7
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commented@drpal Thanks for looking at this issue.
I reviewed the patch and it fixes the regression. For the Webform module, this is a critical issue because it breaks the form builder and any generated form that uses an OR condition.
Drupal 8.6.0 is going to be released on September 5th.
Is there any way this patch can get committed to 8.6.x.? Who is the person that can make this decision?
I will ask around on Slack but I wanted to start this discussion here first.
Comment #8
lauriiiFor me, this approach seems fine. Let's make sure a follow-up gets opened for an actual fix.
In my opinion, we can backport this to 8.6.x but I will double check with the release managers.
Comment #9
seanBWe just found that some forms broke in production after updating to 8.6 and the patch at least fixes the issue. It would be nice to get this fixed in 8.6 as well.
Comment #10
mattjones86Hi, I applied this patch on my 8.6 install and the states now work correctly in the webform admin UI, however they don't seem to be applying to the front end until after achange()
is triggered on the element.Please ignore, the bug I was experiencing is specific to the webform module (#3002812: Default #state not honoured for select field).
Comment #11
lauriiiCould we add some actual tests that are using this form? It would be best if we could write a test that fails at the moment since the bug still exists.
Comment #12
Pascal- CreditAttribution: Pascal- commentedAmazing how this issue was known & patched a week before 8.6 was released and nothing was done with it ...
This is happening more and more and I'm really starting to dislike Drupal because of it!
I'm a junior/medior developer and finding these issues is not exactly easy for me either.
On to fix the next bug updating to 8.6 caused!
(Thx for the patch, it works.)
Comment #13
lauriiiHere's a very minimal test coverage using Nightwatch for this.
Comment #14
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI manually tested my original example and the patch from #13 fixes the regression.
@Pascal- The challenge here is that the JavaScript Modernisation is working hard on improving and replacing Drupal's legacy JavaScript which lacks test coverage. As a community, not enough people, including myself, are fully up-to-speed on modern JavaScript. Finally, as usually more people need to get involved. I know a lot of people who are using this patch but did not provide any simple feedback and thanks as you did.
Comment #16
phenaproximaThe tiniest of nitpicks (fixable on commit): "Value b" should end with a period.
Otherwise, this looks right to me.
Comment #17
GrandmaGlassesRopeMan@phenaproxima - I think this was an issue with the original comment block? This just reverts a change to a previous bit of code.
@lauriii - Looks good to me. 🎉✌️👍
Comment #18
lauriiiCreated a follow-up #3005682: Remove no-restricted-syntax eslint rule override from states.es6.js.
Comment #19
lauriiiI discussed this with @xjm and she mentioned that we should create a post-update hook to ensure that aggregated JS files are being regenerated when updating a site.
Comment #20
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedAttached is the interdiff between #10 and #19 which shows that only the post-update hook was added.
Using the post-update hook to clear cache makes complete sense to me.
The patch was otherwise marked as RTBC via #16 and #17, so I think this can back to RTBC.
Comment #21
dawehnerHypothesis: Running update.php already clears the css/js aggregates
Experiment:
Visit the frontpage, look for JS aggregate
Visit update.php, finish the update without running any actual updates
Visit the frontpage again, compare the JS aggregates
Result:
No updates result in the same css/js aggregates.
Given that an update function is needed everytime we change JS/CSS and we want it to land at the user.
Comment #22
shimar007 CreditAttribution: shimar007 commentedHey,
Just wanted to check by this patch would be ready to use on production sites ?
Regards
Shiva
Comment #23
itamair CreditAttribution: itamair as a volunteer commentedHi, I confirm all this.
Indeed this strongly affects the Geofield Map module (version up to 8.x-2.15) in the not-correct rendering of the Map Theming Options section of the Geofield Google Map View Display Style, even without the OR #states option use.
Everything was working correctly till D8 core 8.5.x) ...
Really needed this fixes committed into new stable D8 core release.
Comment #25
phenaproximaSeems like some weird Nightwatch fail.
Comment #26
phenaproximaBah, meant to make that RTBC.
Comment #28
AaronBauman#19 RTBC +1
This patch addresses 2 coding standards complaints (spacing), otherwise identical.
Comment #29
Oulalahakabu CreditAttribution: Oulalahakabu commented#28 works on drupal 8.6.2 thanks
Comment #30
lauriiiBack to RTBC. Thanks for addressing the coding standard violations.
Comment #31
mikelutzComment #32
lauriiiThis adds very minimal test coverage that proves that the bug has been fixed. We should add additional test coverage in #2702233: [backport] Add JavaScript tests for Form API #states: required, visible, invisible, expanded, checked, unchecked.
Comment #33
GrandmaGlassesRopeManAlright. This looks good to me. Thanks for the test @lauriii 👏
Comment #36
lauriiiThere's sign-off from a subsystem maintainer and multiple comments stating people are using this patch in production - so I'm going ahead and committing this myself prior to 8.6.3 release.
Committed 2662480 and pushed to 8.7.x. Also cherry-picked to 8.6.x. Thanks!
Comment #38
idebr CreditAttribution: idebr at ezCompany commented