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

  1. See if the patch still applies.
  2. Add a test. (thanks @lanchez)
  3. Commit it! (thanks @alexpott)
  4. 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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mparker17’s picture

Issue summary: View changes

Embedded mockup

mgifford’s picture

Issue tags: -a11y +Accessibility

retagging..

This was discussed before I think, but not for a while.

It would definitely be a useful thing to add to the UI.

cs_shadow’s picture

Status: Active » Needs review
FileSize
999 bytes

This 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.

tompagabor’s picture

Status: Needs review » Needs work

The 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:

.visually-hidden {
  position: absolute !important;
  clip: rect(1px, 1px, 1px, 1px);
  overflow: hidden;
  height: 1px;
  width: 1px;
  word-wrap: normal;
}
cs_shadow’s picture

Status: Needs work » Needs review
FileSize
1.1 KB

Added the correct markup for visually-hidden in this patch.

tompagabor’s picture

FileSize
94.59 KB
134.81 KB

Looks good!
Someone else to test?

mparker17’s picture

Assigned: Unassigned » mparker17

I'll test!

vorvor’s picture

Just a mention.

Maybe add the existing visually-hidden class to the label, and then we don't need the new extra class..

mparker17’s picture

Assigned: mparker17 » Unassigned
Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

I'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.

mgifford’s picture

It'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.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Needs a test aka 'if you loved it then you should've put a test on it'
Also #10 & #8 need investigation

mparker17’s picture

Assigned: Unassigned » mparker17

LOL :D

#8 and #10 ask for the same thing — I'll figure out how to do that. And add tests.

mparker17’s picture

This will be useful for writing tests https://drupal.org/contributor-tasks/write-tests

mparker17’s picture

Current behaviour

Default (Stark)

The default template (in core/modules/system/templates/field.html.twig) looks like...

  {% if not label_hidden %}
    <div class="field-label"{{ title_attributes }}>{{ label }}:&nbsp;</div>
  {% endif %}

... 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...

  <h3{{ label_attributes }}>{{ label }}: </h3>

... the preprocess function (core/themes/bartik/bartik.theme::bartik_preprocess_field()) looks like...

  if ($element['#field_type'] == 'taxonomy_term_reference') {
    $label_attributes['class'] = array('field-label');
    if ($variables['label_hidden']) {
      $label_attributes['class'][] = 'visually-hidden';
    }
    if ($variables['element']['#label_display'] == 'inline') {
      $label_attributes['class'][] = 'inline';
    }
    $variables['label_attributes'] = new Attribute($label_attributes);
  }

... 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

  1. Modify Bartik so Hidden labels are not output at all. Leave the default theme as-is.
    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).
  2. Modify core/includes/theme.inc::template_preprocess_field() and add the class attribute to the $label_attributes variable. Modify core/modules/system/templates/field.html.twig so the class attribute is not output twice (using the |without() filter). Modify bartik_preprocess_field() so it doesn't completely stomp the $label_attributes variable.
  3. In template_preprocess_field(), add the visually-hidden class when the label is set to the Visually hidden option.
mparker17’s picture

Here'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.

mparker17’s picture

Status: Needs work » Needs review

Actually, let's see if that breaks any tests...

mparker17’s picture

Assigned: mparker17 » Unassigned

Testbot?

Status: Needs review » Needs work

The last submitted patch, 15: core-added_visually_hidden_field_label-2227601-15.patch, failed testing.

mgifford’s picture

This is looking good! I do really think that this will make it easier for more sites to have accessible content.

Public facing view:
Public facing view

Admin side view:
Admin side view

mparker17’s picture

#2214241: Field default markup - removing the divitis modifies the same files and will probably cause this issue's patch(es) to stop applying.

pfugate’s picture

Assigned: Unassigned » pfugate
Issue tags: +needs-reroll

Rerolling patch

alansaviolobo’s picture

Issue tags: -needs-reroll +Needs reroll
mgifford’s picture

Just a re-roll.

Noticed that <h3{{ label_attributes }}>{{ label }}: </h3> has already been added since the last patch.

mgifford’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 23: core-added_visually_hidden_field_label-2227601-23.patch, failed testing.

swentel’s picture

Status: Needs work » Needs review
swentel’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 23: core-added_visually_hidden_field_label-2227601-23.patch, failed testing.

swentel’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
4.07 KB
761 bytes

This 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 ?

Status: Needs review » Needs work

The last submitted patch, 29: interdiff.patch, failed testing.

swentel’s picture

Status: Needs work » Needs review

right, interdiff.patch, tsss :)

mgifford’s picture

Status: Needs review » Needs work

I 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

tim.plunkett’s picture

Formatter != widget
Formatters are for the entity view itself, widgets are for the form.

mparker17’s picture

Assigned: pfugate » Unassigned

This was assigned but hasn't been touched in a while. Unassigning.

mparker17’s picture

Issue summary: View changes

@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.

lanchez’s picture

Assigned: Unassigned » lanchez
lanchez’s picture

Assigned: lanchez » Unassigned
Status: Needs work » Needs review
FileSize
5.25 KB

Here'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 :)

mparker17’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Related issues: +#2298923: Bartik's field--taxonomy-term-reference.html.twig doesn't output content_attributes, has out-of-date documentation
FileSize
3.32 KB

@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:

  1. In the future, adding an interdiff when you add a patch would be ideal... it helps reviewers see what you changed. I've attached one for the differences between #29 and #38.
  2. -    <div class="field-label {{ title_attributes.class }}"{{ title_attributes|without('class') }}>{{ label }}:&nbsp;</div>
    +    <div class="field-label{% if title_attributes.class %} {{ title_attributes.class }}{% endif %}"{{ title_attributes|without('class') }}>{{ label }}:&nbsp;</div>
    

    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).

  3. +    $this->assertText('field_test_entity_display_build_alter', 'Alter fired, display passed.');
    +    $this->assertText('entity language is ' . LanguageInterface::LANGCODE_NOT_SPECIFIED, 'Language is placed onto the context.');
    +    $array = array();
    +    foreach ($this->values as $delta => $value) {
    +      $array[] = $delta . ':' . $value['value'];
    +    }
    +    $this->assertText($setting . '|' . implode('|', $array), 'Values were displayed with expected setting.');
    +
    

    I don't think duplicating this part of the test is necessary: other code in the same method already tests this.

mparker17’s picture

Issue summary: View changes

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). This has been part of the patch since #15 but I thought I'd document it anyway.

mparker17’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 7c61f7b and pushed to 8.x. Thanks!

  • alexpott committed 7c61f7b on 8.x
    Issue #2227601 by mparker17, cs_shadow, swentel, lanchez, mgifford:...
mparker17’s picture

Added 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.

mparker17’s picture

mgifford’s picture

Thanks for updating these docs. Should definitely help.

yched’s picture

+++ b/core/modules/field_ui/src/DisplayOverview.php
@@ -226,6 +226,7 @@ protected function getFieldLabelOptions() {
+      'visually-hidden' => '- ' . $this->t('Visually Hidden') . ' -',

Nitpick followup : we don't usually use dashes in "machine names / internal array keys". Can we rename this to 'visually_hidden' ?

mparker17’s picture

Issue summary: View changes
Issue tags: -Needs tests
FileSize
2.24 KB

@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.

mparker17’s picture

Title: Add a "Visually-hidden" field label formatter » [Follow-up] Add a "Visually-hidden" field label formatter
Status: Fixed » Needs review

Whoops, change status.

yched’s picture

Status: Needs review » Reviewed & tested by the community

@mparker17: Thanks !
RTBC if green.

mparker17’s picture

Umm... 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 the field--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.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 990ec23 and pushed to 8.x. Thanks!

  • alexpott committed 990ec23 on 8.x
    Issue #2227601 by mparker17: Added [Follow-up] Add a Visually-hidden...
mgifford’s picture

Issue tags: +atag

Glad this got in! It's great to give this type of control to a content editor.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.