Updated: Comment #48
Problem/Motivation
User stories:
As a site builder managing the display of a node, I want the option to hide field labels from sighted users, but make them visible to screenreaders, so I can make my site as accessible as possible without a custom theme.
As a site builder managing the display of a node, I want the option to hide field labels from both screenreaders and sighted users, so that my complex pages are not confusing to people using screenreaders.
Core currently contains three label positions: Above
, Inline
and - Hidden -
. The Above and Inline positions display the field label on the page. The - Hidden - position prevents the field label from being output in the HTML.
If you're trying to make an accessible site, there are instances when you want to output the label for assistive technology, but not show it visually (e.g.: visually-impaired visitors will want to know that this list of links are tags on the page, but it's obvious what they are to sighted users via placement or visual cues).
There are other instances when you want to hide labels BOTH visually and from screenreaders (e.g.: you want to hide the "Body" field label — the Body field is the purpose of the page: neither sighted nor visually-impaired users care about how you store the data in your site).
Note this patch supersedes the changes made in #1106344: Taxonomy term reference field headers always should be rendered with a HTML header (invisible or not), i.e.: if this patch gets in, Bartik shouldn't visually hide "hidden" taxonomy term field labels anymore (because someone could choose "visually hidden" instead).
Proposed resolution
Add a - Visually hidden -
option to the Label drop-down on the "Manage display" page (e.g.: admin/structure/types/manage/article/display
). Selecting this will cause the field label to be output in the HTML, but have the visually-hidden
class.
I chose the text - Visually hidden - because it matches the new CSS class names and it's short. Admittedly, Visible only to screenreaders is more-descriptive, but verbose. Bikeshedding warning: deciding on the best label text could delay this issue significantly... remember we can always document things in the site building guide.
Remaining tasks
See if the patch still applies.Add a test.(thanks @lanchez)Commit it!(thanks @alexpott)- Commit a follow-up to change the internal key for the label option from "visually-hidden" to "visually_hidden" as suggested by @yched
User interface changes
A new item in the entity field label position drop-down.
API changes
None.
Manual testing notes
Due to #2298923: Bartik's field--taxonomy-term-reference.html.twig doesn't output content_attributes, has out-of-date documentation, manually testing this patch on taxonomy term reference fields in Bartik will fail (e.g.: after installing with the standard
install profile, changing the label display of field_tags
in the article
content type will not work).
Create a different type of field and test it that way.
Comment | File | Size | Author |
---|---|---|---|
#48 | add_a_visually_hidden-2227601-48.patch | 2.24 KB | mparker17 |
Comments
Comment #1
mparker17Embedded mockup
Comment #2
mgiffordretagging..
This was discussed before I think, but not for a while.
It would definitely be a useful thing to add to the UI.
Comment #3
cs_shadow CreditAttribution: cs_shadow commentedThis patch adds a "Visually-hidden" field label formatter. If the user selects this label, the label is displayed in a div with a class 'field-display-visually-hidden', though the display property of the field-label is set to none.
Comment #4
tompagabor CreditAttribution: tompagabor commentedThe
display: none
style remain unreadable by screen readers.in /core/modules/system/css/system.module.css, line 303, you can see, what's the proper solution for create visually hidden text. I copy here:
Comment #5
cs_shadow CreditAttribution: cs_shadow commentedAdded the correct markup for visually-hidden in this patch.
Comment #6
tompagabor CreditAttribution: tompagabor commentedLooks good!
Someone else to test?
Comment #7
mparker17I'll test!
Comment #8
vorvor CreditAttribution: vorvor commentedJust a mention.
Maybe add the existing visually-hidden class to the label, and then we don't need the new extra class..
Comment #9
mparker17I've tested the patch in #5 on the latest drupal-8.x HEAD (via Simplytest.me) and I can confirm it presents a
- Visually Hidden -
option for field labels on node, user, taxonomy term and comment "Manage display" forms.I've confirmed that selecting the new Visually Hidden label option gives the field label the same CSS styles as the
visually-hidden
CSS class.Aside: I've confirmed these styles effectively hide the field in at least Safari 7.0.2, Firefox 28, Chrome 33, Opera 20, IE8 (Win7), IE9 (Win7), IE10 (Win7) and IE11 (Win8), and make it visible to screenreaders (or at least, VoiceOver on OS/X 10.9.2) — but note that any problems with screenreader / browser compatibility are a separate issue.
***
I've reviewed the patch: it follows Drupal coding standards.
While I personally would have tried adding the
visually-hidden
CSS class to the label, I can see now that would have required much-more-complex changes to the code. I'm all for Keeping It Simple, so I'm happy to mark this as Reviewed and Tested By the Community.Comment #10
mgiffordIt's a trivial amount of CSS, but shouldn't we reuse the visually-hidden class? Particularly as a client may want to update this in the future.
Comment #11
larowlanNeeds a test aka 'if you loved it then you should've put a test on it'
Also #10 & #8 need investigation
Comment #12
mparker17LOL :D
#8 and #10 ask for the same thing — I'll figure out how to do that. And add tests.
Comment #13
mparker17This will be useful for writing tests https://drupal.org/contributor-tasks/write-tests
Comment #14
mparker17Current behaviour
Default (Stark)
The default template (in
core/modules/system/templates/field.html.twig
) looks like...... note that hidden field labels will not be included in the HTML, the class is not included in the
$title_attributes
variable, and there is currently no way to modify the classes on the label.Bartik
Bartik, on the other hand, has a template (at
core/themes/bartik/templates/field--taxonomy-term-reference.html.twig
) that looks like...... the preprocess function (
core/themes/bartik/bartik.theme::bartik_preprocess_field()
) looks like...... note that, for hidden fields, instead of simply not outputting the field label, it applies the class we want to use in order to hide it from sighted users.
Analysis
Default (Stark)
I'm going to guess that the reason
core/modules/system/templates/field.html.twig
doesn't lump the class variable in the template is so that it's easy for novice themers to modify. This is a great way to make theming discoverable, but, IMO there should at least be a way to add classes.If we took Bartik's lead and added the class attribute to the
$title_attributes
variable, we could|without()
filter should allow us to continue to provide more-discoverable theming. Using this filter will expose more of Twig's powerful features to novice themers, but that could be considered A Good Thing.Bartik
When there was only one Hidden option, it's unclear whether Bartik's approach was correct... in theory, it was more accessible; but it also exposes unnecessary (perhaps even sensitive) information about the data structure of the site to screenreaders or people who examine the HTML directly.
If there are to be two options (Hidden and Visually hidden), then Bartik's approach is now incorrect.
Proposed resolution
Alternately, add the
hidden
class so the label stays in the HTML but is invisible to BOTH sighted and assistive-technology users (it will be visible in the markup).core/includes/theme.inc::template_preprocess_field()
and add the class attribute to the$label_attributes
variable. Modifycore/modules/system/templates/field.html.twig
so the class attribute is not output twice (using the|without()
filter). Modifybartik_preprocess_field()
so it doesn't completely stomp the$label_attributes
variable.template_preprocess_field()
, add thevisually-hidden
class when the label is set to the Visually hidden option.Comment #15
mparker17Here's a patch that implements the resolution proposed in #14.
I've confirmed on my local copy that the label visibility settings Above, Visually-hidden and Hidden work as proposed in #14 in Bartik and Stark.
Tests still need to be written, but it's progress.
Comment #16
mparker17Actually, let's see if that breaks any tests...
Comment #17
mparker17Testbot?
Comment #19
mgiffordThis is looking good! I do really think that this will make it easier for more sites to have accessible content.
Public facing view:
Admin side view:
Comment #20
mparker17#2214241: Field default markup - removing the divitis modifies the same files and will probably cause this issue's patch(es) to stop applying.
Comment #21
pfugate CreditAttribution: pfugate commentedRerolling patch
Comment #22
alansaviolobo CreditAttribution: alansaviolobo commentedComment #23
mgiffordJust a re-roll.
Noticed that
<h3{{ label_attributes }}>{{ label }}: </h3>
has already been added since the last patch.Comment #24
mgiffordComment #26
swentel CreditAttribution: swentel commented23: core-added_visually_hidden_field_label-2227601-23.patch queued for re-testing.
Comment #27
swentel CreditAttribution: swentel commentedComment #29
swentel CreditAttribution: swentel commentedThis fixes the existing tests. However, as you can see in the interdiff, the patch adds an empty space in the field label if there's nothing there, I guess we don't really want that right ?
Comment #31
swentel CreditAttribution: swentel commentedright, interdiff.patch, tsss :)
Comment #32
mgiffordI don't see the results I was expecting. The label wasn't hidden here /node/add/page
I see the dropdown fine fine here /admin/structure/types/manage/page/display
Comment #33
tim.plunkettFormatter != widget
Formatters are for the entity view itself, widgets are for the form.
Comment #34
mparker17This was assigned but hasn't been touched in a while. Unassigning.
Comment #35
mparker17@mgifford you were testing in the wrong place. The setting does not affect the node-edit form: it affects the node-view form.
This issue still needs tests. Updating the issue summary.
Comment #37
lanchez CreditAttribution: lanchez commentedComment #38
lanchez CreditAttribution: lanchez commentedHere's a patch for tests. Also corrected empty space needed in twig template.
I'm not so sure if the test is in correct place so review please :)
Comment #39
mparker17@lanchez, thanks for adding tests!
For the most part, the code looks good, and it appears to function correctly (except if you manually test with taxonomy term reference fields in Bartik, it doesn't work because of #2298923: Bartik's field--taxonomy-term-reference.html.twig doesn't output content_attributes, has out-of-date documentation).
However, that shouldn't postpone this issue because that bug only applies to certain types of fields in a certain theme. I'm updating the issue summary to mention manually testing that particular thing will fail.
I'm marking this as RTBC, but I do have some minor nit-picks, which you may change if you want, but aren't necessary:
Because leading and trailing whitespace is ignored for the HTML class attribute, I'd rather leave this the way it was (short and clear, but with a trailing space).
I don't think duplicating this part of the test is necessary: other code in the same method already tests this.
Comment #40
mparker17Note this patch supersedes the changes made in #1106344: Taxonomy term reference field headers always should be rendered with a HTML header (invisible or not), i.e.: if this patch gets in, Bartik shouldn't visually hide "hidden" taxonomy term field labels anymore (because someone could choose "visually hidden" instead). This has been part of the patch since #15 but I thought I'd document it anyway.
Comment #41
mparker17Adding related issue.
Comment #42
alexpottCommitted 7c61f7b and pushed to 8.x. Thanks!
Comment #44
mparker17Added a follow-up task: #2299477: Don't display taxonomy term reference field headers when field label display is set to "hidden". Edit: never mind, that was done in this patch. :P
Need to update the site building guide and other documentation pages. I'll post links to the changed pages once I find the appropriate place to do so.
Comment #45
mparker17Hide content properly has been updated to mention this feature.
The documentation on specifying how fields are displayed has been updated to mention this feature.
Comment #46
mgiffordThanks for updating these docs. Should definitely help.
Comment #47
yched CreditAttribution: yched commentedNitpick followup : we don't usually use dashes in "machine names / internal array keys". Can we rename this to 'visually_hidden' ?
Comment #48
mparker17@yched I would be fine with that.
Here's a patch to do that.
Note this is a follow-up to something already committed, so there is no interdiff.
Comment #49
mparker17Whoops, change status.
Comment #50
yched CreditAttribution: yched commented@mparker17: Thanks !
RTBC if green.
Comment #51
mparker17Umm... I just noticed the original patch introduced a problem in Bartik: the bartik-taxonomy-term-reference-field-only
$variables['label_attributes']
variable was replaced with the standard$variables['title_attributes']
but the corresponding line in thefield--taxonomy-term-reference.html.twig
was not changed. And there's an array wrapper that wasn't supposed to be there:$variables['title_attributes']['class'][] = array('field-label');
.Should I file a follow-up ticket, add it to the follow-up patch from #48 or wait until #48 gets in and make another follow-up?
EDIT: @alexpott suggested this new issue should be filed as a follow-up ticket, so I've filed #2301599: Regression: Bartik's field--taxonomy-term-reference.html.twig doesn't properly output label attributes.
Comment #52
alexpottCommitted 990ec23 and pushed to 8.x. Thanks!
Comment #54
mgiffordGlad this got in! It's great to give this type of control to a content editor.