Problem/Motivation
#3024975: Add Drupal JavaScript theme function for checkbox added theme function for rendering markup for checkboxes. Drupal.behaviors.MediaLibrarySelectAll also needs to render a label.
Proposed resolution
Create a new theme function that allows rendering labels for form inputs.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Added JavaScript theme function for form labels
| Comment | File | Size | Author |
|---|---|---|---|
| #47 | 3082598-nr-bot.txt | 2.32 KB | needs-review-queue-bot |
| #46 | 3082598-46.patch | 2.55 KB | gauravvvv |
| #43 | 3082598-nr-bot.txt | 144 bytes | needs-review-queue-bot |
| #36 | 3082598-36.patch | 4.43 KB | boulaffasae |
| #33 | 3082598-33.patch | 4.23 KB | kostyashupenko |
Issue fork drupal-3082598
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 #2
lauriiiComment #3
lauriiiComment #4
lauriiiComment #5
phenaproximaAt @lauriii's request, took an initial stab at this patch.
Comment #6
lauriiiThis looks good! Would be nice to replace the hardcoded label in
Drupal.behaviors.MediaLibrarySelectAllas part of this to prove that this works 😄Comment #7
tinko commentedHello,
I've tried several different ways to pass the checkbox to theme function, but the shown checkbox in administration was always without attached js events. So i've made formLabel theme and pass only label params and left the prepend function to attach the checkbox.
What do you think about this solution?
Comment #8
andrewmacpherson commentedHow are labels associated with inputs? I don't see any handling of the for attribute.in these patches.
Comment #9
lauriiiPatch in #5 rendered input inside the label element so it didn't need id and for attribute. However, if we go with the approach proposed in #7, we do have to add support for id. My main concern with the approach in #7 is that we miss tooling for generating unique ids in JavaScript.
Comment #10
andrewmacpherson commentedMost of our inputs (in twig-generated markup) use
<label for>rather than wrapping inputs. Media libary's select-all is an odd case of nesting the checkbox inside the label, but it's OK because it satisfies WCAG "Name, Role, Value".Patch #7 is better, in a way, because it doesn't force the input to be nested inside the label. Having them as distinct elements is useful for layout (label above vs label inline, say).
On the other hand, we don't want to force developers to use the
forattribute. Nesting inputs inside labels is attractive too, because it means you don't have to manage IDs to get accessible names.I'm not clear what the scope of this issue is. The new JS theme function is intended for general use, I think? Media-library is just an existing use which needs to be updated. As it happens, media-library does a separate
jQuery.prepend()to put the checkbox inside the label afterwards.So I don't know whether this theme function needs to have special handling of the
forattribute. Developers can manually add aforattribute to any label easily enough, usingjQuery.attr('for'), say. (Similarly, tableselect doesn't bother with a label element; it makes an accessible name withjQuery.attr('title'). You could do the same witharia-label.Maybe it would be better to explicitly document the fact that developers are responsible for ensuring that inputs have accessible names? The docblock would note that there is no special handling of the for attribute, and a change record could contain several examples.
Comment #11
zrpnrI agree that this theme function is more flexible if it just handles the label and doesn't enforce the nested input.
It also allows for this specific use case in media_library to have unchanged markup, without the extra div.
I modified the theme function to allow for an optional "for".
In this case I'm not adding that attribute since the input is nested.
In this particular case it won't matter because of the prepend - the input gets nested by jQuery so the
forisn't necessary.I was searching core for other places labels are created, I found one in
Drupal.theme.quickeditImageToolbarand theforis static, it could be that it could just be passed as a string or generated inline.However, if it made sense to add a
uniqueIDmethod, that could be a small utility which gets required separately case-by-case.Maybe it shouldn't be part of this patch- but the "announcement" logic was backwards, so I swapped that ternary around.
Comment #12
nod_I would separate the label and attributes
Drupal.theme.formLabel = (label, attributes) => {}and make the attribute thing generic, if it's a theme function may want to make it support more than just 2 attributes.Comment #13
nod_@lauriii pointed me to #3070521-11: Trigger deprecation notifications on JavaScript deprecations to notify developers that deprecated code is being used ok with having a single object as argument.
The rest of the patch looks good to me. The ternary swap should be in another patch though.
Comment #14
lauriiiI assume we should still create the attributes object requested in #12?
Comment #15
zrpnr@nod_ would it work to have
Drupal.theme.formLabel = ({ label, attributes })and then destructure attributes in the function?I'll remove that from this patch and make a new issue.
Comment #16
nod_I'd be ok with that, it's dangerously starting to ressemble FormAPI though let's be careful not to go overboard in other issues :)
Comment #17
zrpnrRemoved the ternary swap and updated the object passed into the theme function.
It was tempting to make an even more nested object like a render array!
The single object should be a good compromise on flexibility, making it simpler for a developer to override which properties are passed in and how to handle them.
Comment #18
zrpnr#13
Posted #3085935: Media library "select all" button announcement is reversed
Comment #19
wim leersWhat's left here?
@nod_ perhaps you can RTBC this? :)
Comment #21
nod_Looks good to me, thanks!
Comment #22
lauriiiComment #23
shashikant_chauhan commentedRerolled the patch from #17
Comment #24
zrpnrThanks for the re-roll @ shashikant_chauhan
There was just a single line change, patch still applies fine and tests are green. This was RTBC in #21, setting it back.
Comment #25
lauriiiNit: We probably don't need the options variable. I would prefer initializing this inline on the function arguments.
Instead of attaching this library manually here, we should add this as a dependency for the
media_library/viewlibrary. This way we can ensure that whenever the library is loaded, the dependency exists as well.Comment #26
tinko commentedHello, @lauriii, changes are added.
Comment #27
zrpnrThis addresses #25.1, The object is now directly inline with the theme function.
This formatting looks correct to me but Prettier shows an error here unless the line breaks are slightly different:
#25.2 looks good as well, the library is no longer attached in the form alter and instead as a dependency in
media_library/viewThe only other nits are some extra spaces in:
and 2 places (form-label.js, media-library.view.js) where the ending newlines are not removed.
For me the extra space and newlines are removed by running
yarn build:js.Comment #28
bnjmnm#27 fixes
Comment #29
zrpnrThanks @bnjmnm, now running
yarn build:jsdoes not produce any changes and the es6 file matches Prettier formatting suggestions.This was RTBC in #21, rerolled in #23 and @tinko addressed @lauriii feedback in #25 with #26.
Comment #30
zrpnrAdded a change record, I wasn't totally sure what to put for the "introduced in" since this is on the 8.9.x branch now but will hopefully be backported to 8.8.
Comment #31
zrpnrComment #32
lauriiiLet's document supported properties in the docblock 📄
We probably should update the documentation since this doesn't seem like an Attributes object.
Comment #33
kostyashupenkoReroll of #28
Comment #34
kostyashupenkoComment #35
kostyashupenkoNeed to fix #32 feedbacks
Comment #36
boulaffasae commentedHello @lauriii, Here's a fix for #32
Comment #43
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #45
madhaze commentedThis would still be nice. It would also need to be added to tableselect.js so the checkbox there also has a label for accessibility. https://git.drupalcode.org/project/drupal/-/blob/11.x/core/misc/tablesel...
Comment #46
gauravvvv commentedI have attached a patch for 11.x, please review
Comment #47
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #50
rpayanmI rerolled it, please review.
Comment #51
smustgrave commentedNew feature will need test coverage. Tagging for that.