Problem/Motivation
#states do not work with 'datetime' form elements:
- Toggling visibility of the form element fails since there's no wrapper container to target.
- Trying to toggle disabled fails since we can't find the nested form elements to disable.
- Trying to toggle "required" fails since we can't find the label to add the right class(es) to.
- ...
Proposed resolution
- Fix datetime-wrapper twig templates to actually generate a wrapper div.
- Update logic in states.js to handle the nested elements like datetime.
Consistently useMoved to solving this in #3078334: Datetime and Datelist elements should render as fieldsets<label>not<h4>for the datetime wrapper label.
Remaining tasks
- Land #3078334: Datetime and Datelist elements should render as fieldsets
Write and verify automated tests.Complete. See comments #91-102- Update code and tests once #3078334 is in
- Markup review of all the changes to the datetime-wrapper twig templates:
- System module's default temple - Fixed, but needs final approval.
- Classy - Decided to leave broken. See #122.
- Stable - Decided to leave broken. See #122.
- Bartik - Fixed in #136 - can we do this?
- Seven - Fixed in #136 - can we do this?
- Claro - Fixed in #136
- can we do this?Yes. From #admin-ui Slack meeting 2019-11-06: @lauriii "We can make changes to Claro 🎉"
- JavaScript review of the changes to
core/misc/states.es6.js. - Other reviews + manual testing (optional).
- Decide if we can backport to 8.7.x (if so, working patch in #139).
- RTBC
- Commit.
- Unblock child issues.
User interface changes
#states actually works on datetime form elements.
Changes to the datetime-wrapper.html.twig templates (default templates from system and both the classy and stable themes) to:
- Actually include a wrapper div (!)
Always use(Now the responsibility of #3078334.<label>not<h4>for the label. The label is generated using the existing core theme templates for form labels (form_element_label).
API changes
None.
Data model changes
None.
Original Report
While trying to create a custom field widget containing a "datetime" form element, I discovered that the #states attribute does not work on it.
Some states can be achieved with a workaround: put the datetime element in a container, and put the #states on the container. But this is obviously no clean fix, and it doesn't work for all states. (Works for "visible" for example, but not for "required".)
I was not able to figure out why this is not working, but I noticed that there have been a lot of issues regarding the #states attribute in the issue tracker. Most of them for were for specific elements like submit buttons, select elements with multiple values, ...
| Comment | File | Size | Author |
|---|---|---|---|
| #182 | 2419131-182.D10_5_x.patch | 14.73 KB | dww |
| #181 | 2419131-datetime-states-181.patch | 14.11 KB | asghar |
| #170 | drupal-2419131-170.patch | 13.99 KB | mstrelan |
| #163 | drupal-2419131-163.patch | 14.11 KB | prudloff |
| #79 | datetime-element-states-2419131-79.patch | 15.75 KB | sukanya.ramakrishnan |
Issue fork drupal-2419131
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:
Comments
Comment #1
lucastockmann commentedI could confirm this bug.
We need to parse the
#statesthrough to the single form elements from datetime.Now we only need to hide the label.
Comment #2
bertramakers commentedI thought about putting the title on either the date element if it's visible, or the time element, which fixed the issue but resulted in a different style then before.
This is because the container element has the class "container-inline" which puts all form elements and their titles on a single line.
Removing this class does not seem like a fix as it will put the date and time elements on separate lines.
Formatting of the code in my patch could also use some love but I'm in a hurry at the moment :)
Comment #3
mpdonadioJust setting Needs Review so the attached patch gets tested.
Comment #6
mpdonadioComment #7
andileco commentedI'm going to re-roll this.
Comment #8
andileco commentedPatch attached.
Comment #10
sharique commented"Notice : Undefined index: #states" is the cause.
Comment #11
andileco commentedHey @Sharique, are you recommending that the above code (post #10) be added to the patch to help it pass? Thanks!
Comment #12
bertramakers commentedI think what he means is that the tests are failing because the lines he mentioned do not check that $element['#states'] is set before it is accessed.
All failing tests are complaining about an "undefined index #states", so that makes sense.
Comment #13
sharique commentedNo, I'm saying this line of code has issues. It seems like $element does not have '#states' element, hence giving warning. Changing it to
might fix the issue but we need to investigate why it is not there.
Comment #14
bertramakers commented#states is always optional on elements, so that's why it's not always set.
Comment #15
kevin.dutra commentedI've updated the patch to fix the PHP notice. I've also removed all the title changes that were added in more recent patches. Things may have been working a bit differently 6 months ago, but I don't believe those changes are necessary or the right approach. The title displays are hidden, so they shouldn't be showing up anyways. If there are still problems with that, I'd suggest opening a separate issue. This simple patch at least gets the states working.
Comment #16
mpdonadioThanks for the patch. Looks good, but can we get a quick test to demonstrate the bug in HEAD and that this fixes it?
Comment #17
pcambraI was discussing about this on IRC and we don't have a way to test states atm.
What I've done is to work in the issue of the label display, here's a patch that takes into account the element as a whole.
Comment #19
mpdonadioHere's the interdiff.
Comment #20
kevin.dutra commentedHaven't looked too deeply into the test failures, but with some manual testing, I can confirm that the patch from #17 is not quite there. To see the issue, configure a content type with a date field and try to add a new node of that type. The date field renders without any label. (#states doesn't even come into play in this example)
Comment #21
mpdonadioSince this needs manual testing, would it be possible to add a test module to datetime.module to show off how #states work for it?
Comment #22
kevin.dutra commentedI like the idea for an example module to aid in testing. In creating an example, it helped illustrate where there were still some rough spots.
Here is an updated patch with the following changes since the previous patch:
<div>to contain the the wrapper contents.<label>elements.@mpdonadio: Is this what you meant by a testing module? The one thing I couldn't figure out was how you can enable a module in the Drupal\tests namespace. They all appear to be hidden, which would be unhelpful if it's intended for manual testing. Is there a way I'm unaware of? Or were you thinking it would actually be outside the Drupal\tests namespace so that it was publicly listed as any other core module?
Comment #24
mpdonadio#23, I'll look at the patch later, but
$settings['extension_discovery_scan_tests'] = TRUE;should allow you to enable test modules from the UI. See sites/example.settings.local.php for more info.
Comment #25
mpdonadio#23, yeah, that was what I was thinking for the example module for manual testing. However, it gives me
when go goto the path defined by the module.
Comment #26
kevin.dutra commentedWhoops, typo on the directory name. This one should actually work.
Comment #28
kevin.dutra commentedHmm, that first failure may actually be valid. It looks like something goofy is happening when the element is used in the field widget for a single valued field. (Everything is inline, at least, which isn't supposed to happen.)
The second failure is probably just the test needing adjustment, although I haven't delved too deeply into either yet.
Comment #29
mpdonadioAwesome. I get a
when I have E_ALL set for warnings when I visit the URL. Just add
in the StateForm.
If we fix the tests, and this warning, I think we are good. Pining one of the theme maintainers to double check the front end changes.
Comment #30
mpdonadioComment #31
jhedstromConfirmed that the small tweak from #29 resolves the php notice. I also manually tested and verified that the 'Required' state now works, but I couldn't get the 'Disabled' or 'Invisible' checkboxes on the demo page to do anything (for instance, invisible did nothing, and disabled would appear to disable at first, but I could still use the date picker to change the value).
Small nit, this isn't the File Hosting module :)
Comment #32
jhedstromRe #31 after reloading the page both invisible and disabled are working--not sure what I had done before.
Comment #33
davidhernandezIs this .label logic in the states JS being added just for this? If so, I'd want that to use a js- prefixed class and not a simple css class name.
What is the use case exactly where this structure can't use a label element? Can someone provide an example?
Comment #36
duaelfrComment #37
kevin.dutra commentedPicking this back up.
Comment #38
kevin.dutra commentedOkay, here's the latest patch. This includes the following changes:
#default_valueis not explicitly defined.'#disabled' => TRUE<h4>to an actual<label>, per @davidhernandez's suggestion. (Returned the CSS changes back to normal.)form_elementwas used as the theme wrapper so that you can actually identify it as a datetime type item.DateTimeFieldTestto correct the Xpath expression that was failing due to the changes in the template.FormTestto fail.Comment #39
kevin.dutra commentedComment #40
kevin.dutra commentedComment #41
mpdonadioLooks like #38 has a lot of out of scope changes for the issue at hand. What is needed, compared to #26 to address the #states problem?
Out of scope, which would remove type hinting.
Out of scope, which would remove type hinting.
Out of scope for this issue?
out of scope for this issue?
Out of scope for the issue?
Out of scope for the issue?
Out of scope for the issue?
Out of scope for the issue?
Out of scope for the issue?
Missing endline.
Missing endline.
Comment #42
kevin.dutra commentedFair enough -- cleanup stuff removed. This interdiff is based of #26, for easier comparison.
Comment #43
jhedstromI'm removing the manual testing tag, as this can now be automatically tested since there's already test module here in the patch. It just needs a quick test added (see #2702233: [backport] Add JavaScript tests for Form API #states: required, visible, invisible, expanded, checked, unchecked for examples).
Comment #44
davidhernandezSorry, should have pointed this out. The Stable templates can't be changed. At least not significantly enough to add a wrapper div. The same goes for Classy. They both require backwards compatibility. The System and Datetime templates are fair game.
Comment #45
kevin.dutra commented@davidhernandez: Ah, I guess that makes sense. I don't think this bug is possible to fix without something to wrap the markup, to be able to give the context of what the label applies to. (Right now, datetime element pieces just hang out in the middle of nowhere.) I'm not totally clear here on how much change is acceptable for a patch release -- would it be viable to add a second wrapper to the datetime element, where this wrapper would just have the containing
div?Comment #46
mpdonadio#45, would it make sense to postpone this on #1918994: Improve Datetime and Daterange Widget accessibility, and then use the fieldset as the wrapper?
Comment #47
kevin.dutra commentedI'm not sure -- it would depend on where that issue goes. Right now, it looks like it's only addressing the datetime field widget, which won't help here where we're looking at the datetime form element.
Comment #49
kevin.dutra commentedRe-roll, in case anyone was relying on this for 8.2.
Comment #51
kevin.dutra commentedOops, forgot that they added that date range module -- a couple test adjustments required.
Comment #53
jhedstromPostponing as mentioned above since #1918994: Improve Datetime and Daterange Widget accessibility will impact this patch.
Comment #54
mpdonadioMissed reopening this when #1918994: Improve Datetime and Daterange Widget accessibility landed.
Comment #55
jofitzRe-rolled.
Comment #56
mpdonadioThis change is to the datetime element, which isn't part of the datetime.module. So, this should live with the element tests, most(?) of which are in system.module. NW for this part.
Not sure if this is a legit change. It is checking that the a div is there, but not necessarily the right one. The middle div probably needs a class on it.
The reroll looks good, and no datetime.module tests broke, but this has markup changes, so I am moving this to the theme system queue so the FE people can look at this.
There are several date / time / datetime element related bugs right now, and we pretty much have no test coverage of these. The tests on the issues should be coordinated to give these a common home.
Comment #57
gordon commentedHere is a version of the patch which works on 8.3.1.
I have been making some changes to date_recur and I need this patch to get the new form working.
Comment #58
gordon commentedI found another problem to do with states and the datetime field. I am not sure if this should be added to this issue or a new issue added.
Basically I have done the following.
So basically in this case the when you enter a date via the datepicker the checkbox is not enabled. however if you just type in the date the checkbox is enabled.
I managed to find a work around and this is basically because of how the value condition is checked. Using the following
Thanks.
Comment #59
realityloop commented@gordon patch at #57 doesn't apply against 8.3.2, patch at #55 does..
Comment #60
borisson_This is blocking #2866563: [Ideas] New interactive widget.
Comment #63
realityloop commentedthis is a reroll against 8.4.x
Comment #64
ada hernandez commentedadding newline at the end of file...
Comment #65
mpdonadioDid not play with this yet, but ...
JS changes need to be made to the ES6 versions of the file. NW for this.
Was this actually needed? The // should find the child?
This also needs markup review as we are changing templates.
Comment #66
_Archy_ commentedThis doesn't seem to work with state 'required': the primary label never gets the required class, I guess because it doesn't have the "for" attribute.
Comment #69
sukanya.ramakrishnan commentedHave the same problem as #66 for visible too, the label never gets hidden correctly.
Comment #70
sukanya.ramakrishnan commentedI had some issues with visible state when the operator is exposed on an exposed datetime filter in a view!.
The min/max and the value fields wouldnt become visible properly when the operator is between/not between and vice versa and the label also wouldnt get hidden correctly.
Submitting a patch for the same!
Patch in 64 wouldnt apply cleanly on 8.7 , so rolled patch to 8.7 (not shown in interdiff).
Thanks,
Sukanya
Comment #71
erik frèrejeanYou've added your
workspace.xmlThe javascript changes should be made in
core/misc/states.es6.js, and then compiled intocore/misc/states.jsComment #72
sukanya.ramakrishnan commentedThanks Erik Frèrejean for the recommendations!
Submitting updated patch. Previous patch in 64 did not have required changes in the the es6 file. Added them in this patch.
Also Submitting interdiff between 64-72.
As mentioned earlier, some changes are for reroll to 8.7
Thanks,
Sukanya
Comment #74
sukanya.ramakrishnan commentedOops, missed to add theme.inc changes in patch in 72,
Updating corrected patch and interdiff between 64 and 74 excluding roll up changes to 8.7
Thanks
Sukanya
Comment #76
sukanya.ramakrishnan commentedComment #77
sukanya.ramakrishnan commentedUploading interdiff for 64-74
Thanks,
Sukanya
Comment #78
tedbow@sukanya.ramakrishnan and @kevin.dutra thanks for the work so far.
yarn run lint:core-js-passingIs not passing for the es6 changes because
prettierhas not been run on the codeSee: https://www.drupal.org/node/2986680
Once
yarn run prettieris run I think all errors will be fixed.Comment #79
sukanya.ramakrishnan commentedThanks @tedbow,
uploading a 'prettier' patch !!
Thanks,
Sukanya
Comment #81
sukanya.ramakrishnan commentedUploading interdiff between 74 and 79 with the right file extension.
Thanks,
Sukanya
Comment #82
sukanya.ramakrishnan commentedApologies , the patch in #79 is not needed. I was trying to make states work with https://www.drupal.org/node/2648950 and https://www.drupal.org/node/2625136 and thats when i found this issue.
It can be fixed only after https://www.drupal.org/node/2648950 and https://www.drupal.org/node/2625136 are fixed!
Reverting all of my patches!
Sorry for the noise!!
Thanks,
Sukanya
Comment #83
ivnishHave this bug on datelist form element.
Comment #84
graber commentedAlmost 4 years? No kidding, maybe it's time to open the gates a bit?
Comment #85
lokapujyaThe problem isn't the gates! It is that no one has reviewed the latest patch. Also the intediff that was submitted with the patch should not be named .patch so that it doesn't get tested. This issue should be in needs review, but because of that patch, it's in "needs work."
Comment #86
lokapujyaComment #87
draenen commentedPatch #79 worked for me
Comment #88
lokapujyaIs it possible to write an automated test?
Comment #89
dwwI'm here because of #3026456: [PP-1] Remove #states hacks on end date once #2419131 is in core and #2845081: Provide a datetime_range widget to define end time via a duration offset.
Re: #88: Yes, via a FunctionalJavascript test. In fact, a previous contributor (sorry, I haven't closely read every comment and patch in here) already started that by adding core/modules/datetime/tests/modules/datetime_states. Thanks!
#79 is working okay with some limited local testing. However, no test classes are actually enabling the datetime_states test module and testing that any of this works as intended, covers all the cases, etc.
So, +1 to both "Needs tests" and "Contributed project blocker" tags...
I know this fix is wider than datetime.module itself, but that seems like a more appropriate component than the generic "theme system".
Comment #90
dwwBah, I might as well figure out how to get FunctionalJavascript tests working. ;) Stay tuned.
Comment #91
dwwIt's not obvious the right way to assert for the field label and the required status or not. So I left those as @todo for now. But this all passes locally, and now we at least assert that visible/invislble and enabled/disabled works. Mostly. ;)
Also fixed some CS and comment bugs in datetime_states/src/Form/StateForm.php.
NR to let the bot chew on this and see what it thinks.
Really NW for finishing the test. Pointers on the clean/recommended way to assert field labels and "requiredness" are most welcome. ;)
p.s. there seems to be some noise at the top of the interdiff. I didn't change *anything* in the JS files. *shrug*
Comment #93
dwwBah. ;) All kinds of weirdness there. Sorry.
I had the wrong test-only patch from a local mixup. This should apply and fail (to both 8.6.x and 8.7.x). ;)
Second, I was developing all this on 8.6.x branch. Turns out #2672950: Notice: Undefined index: #default_value in Drupal\Core\Datetime\Element\Datetime::valueCallback() (line 103 includes one of the fixes to core/lib/Drupal/Core/Datetime/Element/Datetime.php already. But that's only in 8.7.x, not (yet?) in 8.6.x. So, we actually need two slightly different patches here for the two branches.
Let's try that again...
Note:
2419131.91_93.interdiff.txt is between 2419131-91.full_.patch and 2419131-93.full_.8_6_x.patch
2419131-93.interdiff.86x_87x.txt is between 2419131-93.full_.8_6_x.patch and 2419131-93.full_.8_7_x.patch
Comment #95
dwwYay, bot is fully happy, as expected. Yee haw. Fairly close to RTBC, but there are a few lingering @todo's in the test.
I asked in Slack about nice ways to try to write test assertions for the datetime label itself. I've tried
elementExists('css')andelementExists('xpath')to absolutely no avail. WTF is going on here?Here's the raw markup from the form from datetime_states:
(Apologies for the indenting, that's how core sent it to my browser).
The part I actually care about is line #2:
A few notes:
forattribute? Isn't that an accessibility bug?data-drupal-selector="edit-datetime"Is that intended?Meanwhile, although this works fine:
none of these assertions work at all:
All of them give a variant of this:
In the markup above, isn't the "raw"
<label>Datetime</label>I care about the first child of the wrapper<div>that the working xpath for the wrapper found?If I use
//, then I get an element, but it's the wrong label!If I call getText() on this, I get "Date" since it seems to be matching this:
So
//is successfully looking into grandchildren, but it's missing the obvious first child of the wrapper div.Re-re-reading the xpath docs, I'm stumped why the first set of assertions isn't working. Wasting time trying to sort out cryptic crap like this makes me resent trying to be responsible and write tests for things. :( Does anyone spot what I'm doing wrong here?
Thanks!
-Derek
Comment #96
dwwOMFG, what a pain. Turns out my test assertions were failing because the markup is broken. If I utterly hack things locally so that the markup is no longer this:
But actually looks like this:
then lo, all my test assertions start working.
So, the actual code in here is still broken. And lo, it's all kinds of messed up and incomplete with
<label>vs<h4>in the various wrapper templates. core/themes/classy/templates/form/datetime-wrapper.html.twig uses a label. Everything else still uses an h4.I'm going to attempt to fix this now. Stay tuned.
Comment #97
dwwWow, what a mess. ;)
I think I'm doing this right. Instead of hard-coding
<h4>and<label>and all kinds of other hackery in here, if we're going to use<label>for this, we should use the existing form label templates and other plumbing.Here's the working test-only. Should fail. Removing 'Needs tests'.
I'll post patches for 8.6.x and 8.7.x next with the interdiffs.
Comment #98
dwwPatches for 8.7.x and 8.6.x, interdiff relative to #93, and between these two patches.
Meanwhile, definitely "Needs markup review", and another look from someone who groks D8's theme stuff more than I do.
Comment #99
dwwNotes for the markup review:
A) What's up with the inconsistencies in rendering errors?Edit: out of scope, see comment #102, please ignore.
8.7.x HEAD has this
core/themes/classy/templates/form/datetime-wrapper.html.twig:
core/themes/stable/templates/form/datetime-wrapper.html.twigand core/modules/system/templates/datetime-wrapper.html.twig has this:I probably screwed this up "trying to be consistent". Do we really want a raw div in the 2nd two vs. using both the class and<strong>(wtf?) in classy?B) Properly using the form label template takes care of setting these classes for us, and leaving them in the wrapper template is just pointless bloat, right?
That's what I'm seeing when I test locally. Please confirm this is a legit change.
Thanks!
-Derek
Comment #100
dwwwhoops, forgot this crucial step. ;)
p.s. for fun, here's an interdiff between #79 and #98.
Comment #102
dwwUpon further reflection...
Re: #96: Actually, it wasn't a question of
<label>vs.<label for="something">that explained why the assertions weren't working. It was really<label>vs.<h4>. In hindsight, duh! My raw markup from #95 was because I was foolishly testing "live" in the site (after enabling the datetime_states test module), not looking at the actual HTML from the test itself. Had I done that earlier, I would have immediately seen that the xpath was failing because the thing I was looking for wasn't inside a<label>at all. The reason I thought I saw it working was because I had temporarily changed my test class to use 'standard' not 'minimal', and then all of a sudden we started using the one template (from classy) that actually had<label>in it. That was the test-only I accidentally uploaded in #97.Re: #99.A: Out of scope and unrelated. I'm reverting my "fixes" that touch
{{ errors }}since that has nothing to do with this issue, and only produces more possible markup changes that might piss someone off. ;)I'm also removing the (obviously bogus) doc comment about supporting 'attribute' style labels in these templates, which I had copy/pasted without really thinking while trying to "reuse core's label plumbing". I am leaving in the after vs. before/invisible option for label_display, although a) that might be scope creep and b) I'm not really sure why anyone would want a datetime label after the elements. ;) Perhaps that should be ripped out, too.
Here's a shiny clean test-only, and new patches for 8.6.x and 8.7.x that undo the changes for
{{ errors }}.Comment #103
dwwFixed the summary to reflect the current state.
Also, tagging for 'needs JavaScript review' since I'd love some other eyes on that part of this.
Comment #105
dwwFinally made time to actually read the whole history here.
Thanks @kevin.dutra for datetime_states.module. ;)
Re: #29: That error is fixed in #2672950: Notice: Undefined index: #default_value in Drupal\Core\Datetime\Element\Datetime::valueCallback() (line 103 which is already in 8.7.x but not in 8.6.x. I'm going to stop trying to include that fix in here at all. Instead, this patch (and all the 87x patches I've uploaded) apply cleanly to 8.6.x branch, too. If you want that error to go away on 8.6.x, use #2672950. I'm trying to get that backported. See also #2992580: Custom callbacks doesn't work.
Re: #56.1: Good point. Yeah, it seemed weird to have the tests in datetime.module. Moved to system at @mpdonadio's request, fixed the module name to be "datetime_states_test" to be consistent with everything else in there, updated the route name and path, class name, etc.
Re: #56.2: Now I understand why this was in the "theme system" component. Sure, I guess that's okay for now. Perhaps "forms system" would be a more appropriate spot for the final home for this issue once the theme folks sign-off on the markup changes.
New clean test-only. I'll upload the full patch next.
p.s. I'm hiding a bunch of old files that are no longer relevant.
Comment #106
dwwComment #108
dwwForgot the interdiffs.
102 to 106 (recent fixes).
79 to 106 (everything I've touched since I started on this).
Comment #109
dwwUgh, I left one more of the bogus 'attribute' docblocks in.
Comment #110
lendudeJust took a quick look at the test, looks great, just some nits.
If you leave this out, it will use the testing profile, which is even more minimal then minimal, tried it locally and works fine.
this needs to be protected
Comment #111
dwwRe: #110: Thanks! Both fixed.
Comment #112
mpdonadioI think if we are making the change to the Classy template, the we should also mirror the change in the system one (but we don't touch stable).
And, changing markup in Classy requires frontend manager review and a CR.
Also tested this manually. In a default install, the label isn't visible.
I don't recall why we didn't tackle the h4 vs label in #1918994: Improve Datetime and Daterange Widget accessibility.
Comment #113
dww*sigh* So we leave "stable" broken? That's a win for people using stable? Weird. I don't understand this "policy". The markup is broken. Why don't we fix it? *shrug*
Meanwhile, what do you mean the label isn't visible in a default install? Here's before and after with #111 applied to a clean 8.7.x dev site. Both a date and daterange field:
Before:

After:

I submitted an initial draft CR
Comment #114
dwwBefore/after screenshots with a date-only field, too. I think something else is fubar w/ mpdonadio's test site for the screenshot in #112. ;)
I don't know what "work" this needs. I agree we need a frontend framework manager review...
I really don't think we should leave "stable" broken. That makes absolutely no sense to me.
Comment #115
mpdonadioI retract my problem. Re-applied patch and retested and it works.
Comment #116
dwwWhile we're fixing this template, we should also fix #2982187: datetime-wrapper.html.twig ignores #description_display parameter. I believe we'd only need a single CR for both issues. I don't know the best way to coordinate. These two patches conflict for now. This one here seems like the bigger bug, but I'm open to suggestions on the best way forward. Should we merge those fixes and tests into here? Should we leave them separate and try to coordinate?
Thanks,
-Derek
Comment #117
dwwProbably not worth trying to fix this until we get some clarity at #3031198: [META] Add classy_dev and minor-branch theme snapshots to allow fixing markup bugs within minor releases without front-end disruption.
Comment #118
valicHi, just to report that small change is necessary for date list element if someone using this patch at #111
For #states to work one `datelist` field type, it should be added one small line at
\Drupal\Core\Datetime\Element\Datelist::processDatelist at line 272
'#states' => empty($element['#states']) ? [] : $element['#states'],Comment #119
dwwSorry for the noise, but #3031198: [META] Add classy_dev and minor-branch theme snapshots to allow fixing markup bugs within minor releases without front-end disruption has evolved, and now I see we have #2352949: Deprecate using Classy as the default theme for the 'testing' profile as the immediate solution to our bugfix deadlock.
Comment #120
dwwp.s. the
<strong>WTF from #99 is fixed here: #3010558: Unnecessary <strong> element in Umami's form-element template may produce invalid markupComment #122
lauriiiI recommend that we fix this bug in core without fixing it in Classy. Classy is potentially getting removed from Drupal Core prior to Drupal 9 on this issue: #3050378: [meta] Replace Classy with a starterkit theme. We could provide a change record with instruction for users to override Classy template with a version that fixes this bug. What comes to the test coverage, I recommend that you use Stark theme for building the test coverage, and add a @todo for that to be removed as part of #2352949: Deprecate using Classy as the default theme for the 'testing' profile.
I'm planning to provide this issue with a more thorough review later to remove the tag.
Comment #123
d.fisher commented#111 doesn't apply for me (8.7.6) because I am also applying https://www.drupal.org/project/drupal/issues/994360#comment-12489118
Comment #125
dwwRe: #122 sounds like a plan, thanks!
I didn't have time to do all of that, but I needed a re-roll of this for composer and Getting Things Done(tm) on real sites. Here are patches for All The Branches(tm). Hopefully I can find some time to do what you suggest (change the tests to use stark, leave classy broken, write a CR, etc). This is blocking not just contrib, but also #2648950: [PP-2] Use form element of type date instead textfield when selecting a date in an exposed filter so I'd love to see a viable solution land soon.
Cheers,
-Derek
p.s. Interdiff is empty, these are straight re-rolls.
Comment #126
dwwA new day, a little sleep, and a firm desire to finally put this bug to rest. ;) I'm going to take a stab at this now. Stay tuned.
Comment #127
dwwAmazing! That was significantly easier than I feared. ;) The test required 0 changes beyond this:
Still passes locally, still has very complete coverage of this fix.
The only other changes here are reverting the fixes for the datetime-wrapper.html.twig template from classy and stable.
While the testbot chews on this, I'll update the CR: https://www.drupal.org/node/3030265 to clarify that classy and stable are not fixed, providing instructions on how to define your own
datetime-wrapper.html.twigto benefit from the fixes.p.s. Same patch applies cleanly to 8.9.x, 8.8.x and and 8.7.x, so I'll only upload 1 copy. I'll queue it for testing on all 3 branches, though.
Comment #128
dwwCR updated: https://www.drupal.org/node/3030265/revisions/view/11283996/11619669
Meanwhile, I had a thought -- are we allowed to fix Seven, Bartik and Claro? They all extend classy, so they're all broken. But I don't believe they're still subjected to the same BC rules, right? If so, we'll need different patches for 8.9.x / 8.8.x vs. 8.7.x, since Claro is new in 8.8.x
Claro already overrides
datetime-wrapper.html.twigand adds a wrapper<div>. Smart theme! ;) It would only need some minor updates, or maybe already Just Works(tm). Needs testing.Bartik and Seven are still fully broken, relying on Classy.
Thoughts?
Almost there! :)
YAY!!
-Derek
Comment #129
dwwWhoops, right, #2352949: Deprecate using Classy as the default theme for the 'testing' profile wasn't backported to 8.7.x. ;) I doubt this is going to be, either, so maybe I won't care. I'll ignore 8.7.x for the testbot for now.
Next, bot is complaining about 13 CS violations, but I don't understand them, since none of it looks like it's touched by my patch:
Preemptively hoping we can fix the other core themes, here's a patch that at least fixes Bartik and Seven. Other than copying the templates to the right places, it also adds an admin route to datetime_states_test so you can play with the test form via an admin theme at
/admin/datetime-states-test/example. I'm not sure if/how we should try to run this test N times for all the core themes, that seems out of scope and a bigger problem to solve than a 1-off solution for this one JS test.I started trying to fix Claro. Since there's already a
datetime-wrapper.html.twigtemplate, as I feared, it requires more work. I'm really not clear on the right solutions. A few issues:form-item__descriptionnotdescriptionfor the class on the form description.states.es6.jsdoesn't know about this, and only targetsdescription. Seems reasonable to havestates.es6.jstarget both, but I'm not sure.<h4>withtitle_classesand this:<h4{{ title_attributes.addClass(title_classes) }}>{{ title }}</h4>. Once again, states.es6.js doesn't know about this, and is specifically targeting<label>, not whatever happens to have theform-item__labelclass. Not sure if it's better to change Claro's template to use<label>or to try to expand states.es6.js to handle both cases.<label>, we probably want to change the title_classes stuff to do something like this:Right? Is that legit?
Sorry I'm not much of a front-ender, and not versed in the inner workings of Claro. Any help/directory would be lovely!
Thanks,
-Derek
Comment #130
dwwFor fun, here's how I think we're supposed to get a specific test to use
starkin 8.7.x. Seems to work locally. Hope the bot agrees. Still not clear if this is viable for an 8.7.x backport, but if so, this should work.Comment #131
jibranPatch looks really good to me. I'm not sure what's the policy around HTML structure change in minor/patch release.
OMG, yes to bringing this template inline like other fapi element.
Comment #132
jibranWe should be able to just set the the theme property on class.
Comment #133
dwwRe: #132:
You mean like #129 does?
That's new in 8.8.x branch. I don't think there's any other way to do it in the 8.7.x branch, which is why I posted #130 as an 8.7.x-only patch.
Comment #134
jibranRe: #133: Yeah you are correct. I confused
\Drupal\Tests\BrowserTestBase::$profilewith the theme as well.Comment #135
dwwStill haven't heard anything from anyone in Slack that knows anything about Claro. I hope we can fix Claro, too. 🤞
If so, this seems to do it. Would still love feedback from someone who knows. ;)
Here are my answers to my questions from #129:
div.description, div.form-item__description.<label>.claro_preprocess_datetime_wrapper(). So I fixed the preprocess and removed all the manual class setting in the template. Note that by solving #2, we're already gettingform-requiredandjs-form-requiredfromthemes/classy/templates/form/form-element-label.html.twig.Also note: I added the
validateForm()method on the datetime_states_test form to play with it and make sure thehas-errorclass is getting set properly on the label. Writing more automated tests for this across all browsers seems out of scope, but at least this simplifies manual testing in the meanwhile. ;)Comment #136
dwwWell, poo. The changes to
states.es6.jsin here are weird, don't fully work, and seem needlessly complex.Instead of all the mojo for
js-complex-form-itemtrying to find the right sub-elements (label, description, etc), let's just toggle the visibility of the entire thing with thejs-complex-form-itemclass. When I tested manually with stark, I realized the description wasn't getting toggled. Same problem as #129.3 only there is *no* class at all. ;)So here's better test coverage of the problem, and a fix to states(.es6)?.js that seems to work well on all the core themes.
Comment #137
dwwUpdating summary that all the core themes (except classy and stable) are fixed in #136, but we still need to review / sign-off that we're allowed to make these changes.
Comment #139
dwwSorry about the 8.7.x fail. I was hasty and pulled in the 8.8.x
$defaultThemestuff into the wrong branch. This will pass.Meanwhile, I was looking more at the
states.es6.jschanges, and was wondering why bother with all this:Why not just this?
For reasons I can't explain, that doesn't work. The automated test fails, and testing manually confirms. Attaching here for comparison / reference. #136 is still the right patch for 8.8.x and above branches.
I'll put the now-working 8.7.x patch last here, so hopefully the bot doesn't mark this NW because of the WILL-FAIL patch.
Comment #141
othdvlpr commentedFYI. The date capability in forms doesn't work for me using a container. I've attached the code.
My configuration:
Drupal core 8.7.8
PHP 7.3.11 (cli) (built: Oct 22 2019 08:11:04) ( NTS )
mysql Ver 15.1 Distrib 5.5.64-MariaDB, for Linux (x86_64) using readline 5.1
Comment #142
othdvlpr commentedresend
FYI. #STATES in containers does not appear to work with dates in forms API. I've uploaded the code excerpt in StatesDateFail.txt. My configuration appears below.
Drupal 8.7.8
PHP 7.3.11
mysql Ver 15.1 Distrib 5.5.64-MariaDB, for Linux (x86_64) using readline 5.1
Comment #143
dww@othdvlpr: Sorry, it's really hard to make sense of your code, since there's a lot of stuff commented out, it's not indented properly, and you're embedding
<table>markup all over the place using#prefixand#suffix. It's also not clear if you've applied the latest patch in here or not.More to the point, the patch in this issue is about getting #states to work properly directly on form elements of #type 'datetime'. It kinda looks like your code is trying to get a given container to appear/disappear based on if a form element of type 'date' has a value or not. If so, that'd be more appropriately sorted out in a support request in another issue, since that's not what this issue is trying to solve.
tl;dr, I don't think this is the right issue to try to help you with your form. ;)
Thanks,
-Derek
Comment #144
dwwYay. In the #admin-ui Slack meeting today (2019-11-06) I asked about fixing this bug in Claro, to which @lauriii replied:
"We can make changes to Claro 🎉"
Updating summary accordingly. ;)
Comment #145
dwwAlso from Slack, @andrewmacpherson writes:
To which I'll reply here:
A fieldset + legend would also be problematic when using a datetime element in an exposed filter on a view. That's (part of) why I'm trying to fix this bug, to unblock:
#2648950: [PP-2] Use form element of type date instead textfield when selecting a date in an exposed filter (one of the top-20 most followed issues in the core queue).
Especially in combo with #2625136: Fix label visibility and add wrapper container for exposed numeric/date filters with multiple form elements -- we'd have nested, ugly (and potentially confusing) fieldsets/wrappers/etc. Ooof. :/
Also, got a link to #3078334: Datetime and Datelist elements should render as fieldsets which I believe is duplicate with this issue, so I marked it as such. Technically, maybe we could fix that first, then take advantage of that here, but there's already so much prior discussion and work here, it seems counter-productive to fork the discussion. If @lauriii and @andrewmacpherson believe otherwise, I'm happy to be wrong, and we can re-open that one, postpone this, and get all hands on deck over there. *shrug*
Edit: @andrewmacpherson and I discussed in Slack and agreed to re-open #3078334: Datetime and Datelist elements should render as fieldsets.Comment #146
dwwUpon further discussion, @andrewmacpherson and I decided to postpone this on getting #3078334: Datetime and Datelist elements should render as fieldsets fixed, first. That's a more serious accessibility bug, that potentially is fixable in All The Templates(tm), even Classy + stable. TBD. Regardless, it's a smaller scope, so hopefully more easy to deal with separately. Then, the work in here can focus on the lack of a wrapper + the states.js updates, the JS tests, etc.
Meanwhile, they pointed out that the summary in here was somewhat lacking, didn't actually describe the bug, etc. So I tried to flesh that out a bit more. Also updated remaining tasks and other sections to indicate that the
<h4>vs.<label>parts are happening at #3078334, not here.Comment #147
heddn#136 needs a re-roll on top of #3078334: Datetime and Datelist elements should render as fieldsets. As
datetime-wrapper.html.twiggets removed in that issue in later patches.Comment #149
smustgrave commentedHello just following up on the current status of this issue? I'm running Drupal 8.8 and we need to make some date fields conditionally required. Any suggestion or help would be appreciated!
Comment #150
dww@smustgrave re: #149: If you want minimal disruption that works, you can use #136 (or a re-roll of that) and it should be fine. However, I don't think that's the direction core will (eventually) go. #3078334: Datetime and Datelist elements should render as fieldsets needs to happen for other reasons, and when it does, this bug will magically go away (since there will no longer be the broken custom "datetime wrapper" that doesn't actually include a wrapper). #3078334 is where the energy needs to go for fixing this in core, but I ran out of time/steam for that, and (as usual) I needed to simply apply this patch to the project and move on. Huzzah for technical debt! :/ For a working D8.8 site, the project where I care about this functionality needs 13 core patches in composer.json. :(
Comment #151
init90Added relevant tag
Comment #152
dpiReroll and removes core version per #3072702: Core extensions should not need to specify the new core_version_requirement in *.info.yml files so that 9.0.x can be installed
Comment #155
acbramley commentedReroll of #135 with the same fix as #152 - no generated interdiff as it's the same as #152
Comment #156
kim.pepperComment #160
scott_euser commentedRecompiled states.es6.js for reroll as its changed again in 9.5.x, no change to patch (for use until the referenced issue in #150 is resolved).
Comment #161
michel.g commentedRerolled patch against 9.5.x
Comment #162
michel.g commentedDisregard the comment above :) Was writing multiple patches and uploaded the wrong one.
This should be the correct reroll of this patch to 9.5.x
Comment #163
prudloff commentedHere is a reroll for 10.0.
Comment #164
fenstratJust confirming the #163 looks good for 10.0. It's missing the the es6 changes (no more es6) and the changes to seven theme (no more seven).
Attached is the interdiff showing the formatting changes to core/misc/states.js
Comment #165
ady1503 commentedI will confirm that it still does not work in drupal version: 9.5.3
Comment #166
oleksii_m commentedSmall corrections for 9.5.7
Comment #167
gobinathmJust confirming that #163 looks good for 9.5.9.
Agree to #164
Comment #169
amanp commentedReroll of #162 for 9.5.9. Identical interdiff with #166, just without the IDE additions and comments.
Accounts for the change to core/misc/states.js implemented in #994360: #states cannot check/uncheck checkboxes elements
Comment #170
mstrelan commentedReroll of #163 for 11.x. Also applies to 10.0.9.
Comment #171
bbombachiniPatch did not work for me on 10.0.9 so I'm uploading a new one.
Comment #172
bhanu951 commentedSince this issue is WIP here is a workaround to overcome this https://stackoverflow.com/a/6429897/
Comment #173
gaurav_manerkar commentedPatch from #170 needs work, doesn't work correctly for state:required.
Classes
js-form-required form-requirednot getting assigned to<label>tag.Comment #174
mjb3141 commentedReroll of #171 for 10.2.5.
Comment #180
joelpittetApologies for the earlier confusion—I briefly pushed and hid an MR while testing. I’ve opened #3536174: #states: alternative fix for datetime element with a cleaner branch that simply adds the same
.js-form-wrapper/.form-wrapperclasses used by<details>element. This keeps this long history intact (too many needs tags and this is getting dated due to postponed issues), so if you're interested in possibly a way simpler solution, please continue the review there and again sorry for the noise.Comment #181
asghar commentedReroll of #170 for 11.2.3
Comment #182
dwwWhee, reroll of #170 for 10.5.x 😅