Problem/Motivation
Block titles can currently be displayed or hidden by site-builders. When hidden, the heading element is omitted from the HTML entirely. Hiding titles might be desirable for a visual design, however block titles can be useful for visually impaired users, providing contextual clarity and navigational cues via screenreaders.
Proposed resolution
Add another option in block configuration, to output visually-hidden block titles, with a "visually-hidden" class.
In D7 this functionality is provided by the a11y_titles module.
We have already added equivalent functionality to D8 for the FieldAPI label display. Offering visually-hidden block titles will extend the options available for site builders to fine-tune the page structure.
Before

After

Remaining tasks
| Task | Novice task? | Contributor instructions | Complete? |
|---|---|---|---|
| Create a patch | Instructions | Done | |
| Reroll the patch if it no longer applies. | Instructions | Done | |
| Add automated tests | Instructions | Done | |
| Update the patch to incorporate feedback from reviews (include an interdiff) | Instructions | Done | |
| Manually test the patch | Novice | Instructions | Done |
| Embed before and after screenshots in the issue summary | Novice | Instructions | Done |
| Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards | Instructions |
- Usability review: this accomodates an additional option by replacing a checkbox with a select element, similar to the label display for field formatters.
- Documentation: explain the visually-hidden option on the block help page?
Update settings form in\Drupal\Core\Block\BlockBase::buildConfigurationForm()Update HTML output in block module, adding "visually-hidden" class to title_attribute twig variable via template_preprocess_block. The block twig template does not need to be changed.Update block settings config schema.Not necessary -block_settings.label_displayis already type: string, not boolean.Update installation config in core profiles/modules, changingIt would be a BC break so keep the '0' value for hidden titles.block_settings.label_displayvalues of '0' to explicit 'hidden'.hook_update(). ChangeNot usefull anymore (see previous)block_settings.label_displayvalues of '0' to explicit 'hidden'.Write tests.Some blocks have custom behaviour, e.g. menu blocks already have visually hidden titles, and Bartik uses it's own CSS to hide block titles in the header region. Come up with an approach for these - we might be able to remove custom templates and CSS, relying instead on installation config.Should be done in follow-up issues.
User interface changes
Block title display option changes from a boolean checkbox option to 3 options in a select element: Visible, Visually Hidden, Hidden.
Aim for consistency with the Field API options in entity manage-display settings.
Also descriptive help text may change (D7 a11y_titles module says "Block titles can provide context to users who can’t rely on visual cues").
API changes
None expected.
Data model changes
We are introducing a third option to Block title display: 'visually-hidden'. Effect on data model are as follows:
- Config Schema: NO CHANGE. While we are introducing an additional option ('visually-hidden') for the core data type
block_settings.label_display, this is already of type: string, which accommodates all proposed values (visible, visually-hidden, hidden). Installation Config: CHANGED. Block configuration files inRemoved for BCconfig/installfolders will need to be updated, changingblock_settings.label_displayvalues of '0' to explicit 'hidden'.Existing Sites' Config: UPDATE HOOK required, changingRemoved for BCblock_settings.label_displayvalues of '0' to explicit 'hidden'.
| Comment | File | Size | Author |
|---|---|---|---|
| #139 | 23_01_8596_D11.patch | 30.22 KB | dhavalpanchal |
Issue fork drupal-2614950
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
andrewmacpherson commentedComment #3
mgiffordThis would be a great addition to Core!
Comment #4
andrewmacpherson commentedI dug into the settings form and config schema, and it looks like we won't need to update the schema at all, since
block_settings.label_displayis already a string. (I had assumed it would be a boolean).We'll be adding a new option 'hidden', but the schema already copes with this I think.
label_settingswill either have value 'visible' or '0' in existing D8.0.x installations. I don't think there's a need to mess with this for upgrading sites.Updated the issue summary to reflect this, crossed 2 tasks off the list.
Comment #5
andrewmacpherson commentedHmm, I spoke too soon. The '0' value needs changing to an explicit 'hidden'. Currently it only stores a '0' because it's a checkbox, with no explicit value for unchecked. Once we're on three options, they'll all need explicit values, regardless of whether we use a select or radio buttons. I propose 'visible', 'visually-hidden', and 'hidden' as the values.
it still doesn't require a schema change, but does need changes to installation config, and an update hook.
Updating issue summary.
Comment #6
andrewmacpherson commentedThe "Display title" form element is in
\Drupal\Core\Block\BlockBase::buildConfigurationForm().The current setting comes from a constant -
BlockInterface::BLOCK_LABEL_VISIBLE, so that's another thing to change, maybe add a couple more constants for hidden and visually-hidden, and use these for the #options in the form element. Let's check how field label options are handled, for consistency.Updating issue summary.
Comment #7
andrewmacpherson commentedThe block module implements hook_help(). We might want to update that with an explanation of the visually-hidden setting. (I looked at the help page for field_ui, but the label visibility settings aren't explained there.)
Comment #8
andrewmacpherson commentedThis patch does the following:
This patch will likely fail, as tests haven't been changed yet.
Don't be alarmed if you see lots of block titles in the seven admin theme - still need to update install config and provide an update hook.
Comment #9
andrewmacpherson commentedComment #11
Jeff Burnz commentedComment #12
andrewmacpherson commentedThanks Jeff. I'd discovered that some block titles had special behaviour/styles in core themes, such as menu blocks already having visually hidden titles. We should come up with an approach for these - it would be cool is we can remove some custom templates and CSS, relying instead on installation config.
I've added an extra task to the issue summary for this, and we can note specific examples as we find them.
The patch in #2625834: Make the visibility of block titles in the header optional. contains an example of an update hook to change all the existing block settings too, handy.
Comment #14
dcrocks commentedIn a sense a ternary version of the label_dispaly variable is implemented in the block twig.html files. Currently the status of 'label_display' is tested 2 ways in drupal twig templates. These are 8 templates that use
{% if label %}. These are relying on "template_preprocess_block" to actually test the config setting of label_display and simply skip rendering of the title if it is empty. 3 menu twig template files use{% if not configuration.label_display %}and add the "visually-hidden" class to the title element if it is not set.It seems to me any one can have local copies of block templates and make their own decisions about how to handle the label. The existing block templates actually do show the 3 possible options identified in this issue, ie. the block label can be omitted, visually hidden, or visible.
I do think some existing blocks should be reviewed(eg. site branding block) as to whether they should omit the label or visually hide it, for ada conformance.
Comment #15
dcrocks commentedI created a new issue that I think is relevant to this one, #2715687: make templates use 'display_label' in consistent manner. I am hoping to spur discussion there that could affect the eventual result of this issue.
Comment #16
mgiffordThis would give more control to the
userseditors.Comment #18
andypostRelated issue fixed, so new constants should live inside plugin interface #2829680: BLOCK_LABEL_VISIBLE is defined on the wrong interface
Comment #20
duaelfrPatch rerolled against 8.4.x including changes from #18
It still needs the tests to be fixed and new tests to be added, though.
Comment #22
duaelfrI added an upgrade path and fixed core templates that used the old way to hide block title.
I'm afraid we should use a "0" value instead of the "hidden" we introduced in this patch to preserve BC. It's up to discussion.
We still need tests here, though.
Comment #24
duaelfrI discussed about it offline at dev days Seville with Artusamak and we thought it was possible to introduce that new option without breaking BC.
Here is a patch that removes BC breaks from the previous one by reverting the
\Drupal\Core\Block\BlockPluginInterface::BLOCK_LABEL_HIDDENvalue to "0", which is it's current value, and reverting the code that were changed in templates to make it work when it was "hidden".Next step is to fix the existing tests, then write new ones.
Comment #26
duaelfrI'm still working on this one.
Comment #27
duaelfrTests should be fixed and completed.
Give me a green, dear testbot!
---
I had to fix an existing test (
\Drupal\block\Tests\BlockTest::testHideBlockTitle) that were doing strange things.In fact, that test was testing that the block title was not shown by default. But, given the existing UI, the block title IS shown by default. That's just because of the internal
\Drupal\simpletest\WebTestBase::drupalPostForm()mecanisms that mess with checkboxes that the tests didn't fail right now (not sending a value is like sending an unchecked checkbox, which is different from its default value). It would have failed when converted toBrowserTestBaseanyway.Comment #29
duaelfrFixing the last tests
Comment #30
duaelfrI think you don't need my phpunit.xml file :/
Sorry again
Comment #31
duaelfrWe could use screenshots here and there is some manual testing to do.
Novices are welcome!
Comment #32
agomezmoron commentedAfter applying the patch #30, I checked that is worked on:
Chrome
58.0.3029
57.0.2987
Firefox
52.0.1
52.0
51.0.1
51.0
Also screenshots are added (PRE and POST patch).
I also updated the summary with the tasks done.
Comment #33
mgifford@agomezmoron want to mark it RTBC?
Comment #34
agomezmoron commented@mgifford sure!
Comment #35
mgiffordNice!
Comment #36
andrewmacpherson commentedThanks for moving this patch along @DuaelFr. Getting it to RTBC way ahead of the 8.4.x releases will give site builders plenty of time to road-test it. Hopefully we can move the related issues along soon after.
Comment #37
agomezmoron commentedComment #38
wim leersAFAICT this needs usability review. The IS (issue summary) should also have a before vs after screenshot. Those screenshots already exist in #32, but should be added to the IS so that they're easy to find for a core committer and a usability maintainer.
Comment #39
andrewmacpherson commentedI've looked at the child & related issues. I don't think any of these are blockers to this issue:
0forBlockPluginInterface::BLOCK_LABEL_HIDDENuntil D9 means the data model addition is backwards compatible with existing sites, and no update hook will be required here. Yay!label_displayto0to avoid disrupting existing sites, which is compatible with this issue.template_preprocess_block(), but isn't a prerequisite for this. Whichever gets committed first will necessitate a re-roll of the other.Does this warrant a change record, or a handbook update? It's a powerful feature for site builders, and themers should probably be made aware of this in case their custom themes are doing any fancy with block labels.
Comment #40
andrewmacpherson commentedLooking at the hook_help() in block.module...
Does "toggle" imply a boolean, with 2 options only? Maybe we can say "... and choose how to display it."
Do we need to explain the visually hidden option on the block help page?
Comment #41
andrewmacpherson commentedCorrecting the "done" status for some tasks in issue summary, update the usability review task.
Comment #42
andrewmacpherson commentedThis issue has moved forward considerably, and it makes sense to treat the Bartik header issue as a follow-up to this one. I've marked this issue as the parent for #2625834: Make the visibility of block titles in the header optional..
Comment #43
agomezmoron commentedShould it be moved it into Needs Review? I think it doesn't make sense keeping it in Needs Work.
Comment #44
yoroy commentedThanks for a solid issue summary and task list! I think adding this is a good idea. Some UI considerations:
3 options in a dropdown is not really nice :-\ I know this is similar to label visibility in Field UI, but that's consistency with a not so good user interface so we can allow some room to experiment here.
Accessibility-wise, is it even a good idea to have the option to exclude the title from the html output? Could we make this a boolean that toggles between show title and visually hidden? Put differently: is visually-hidden preferable over hidden?
Comment #45
andrewmacpherson commented@yoroy
So the other straight-forward approach is 3 radio buttons. It this preferable in your view? I'm agnostic, but I can see how seeing all options will help, when there are just a few.
Accessibility is the whole reason for this feature request. The intent is to provide better control for the document outline of the page, considered as a whole.
There is certainly an accessibility case for allowing blocks without heading elements. It allows you to group some related blocks under the same heading. Even though the content comes from separate blocks in the CMS, it sometimes deserves to be treated as one section in the document outline for the resulting page. The existing options allow you to do this; you can include the heading for the first block, and omit it from the subsequent block. The blocks are just wrapped in separate divs, so semantically they appear under the same heading in the document outline.
The answer depends entirely on the content!
Right now you have to decide between a visible heading, or no heading at all for each block. For the sake of visual presentation a heading may be undesirable, but when the block offers some important functionality a hidden heading will help some users. This is especially the case for blocks which contain form elements (search, login, sign-up). The current options encourage authors to compromise the document outline for the sake of visual presentation. This is the situation which the new visually-hidden option is intended to help with.
This would force site authors to include a heading for every block, which is swapping one problem for another. A visually-hidden heading will often be preferable to no heading, but forcing a heading for every block would still hinder efforts to achieve a sensible document outline for the whole page.
To confuse matters, some blocks already behave inconsistently. These are good things to address in follow-up issues (i.e. remove these constraints, allow site builders to exercise discretion, and provide good defaults).
Comment #46
yoroy commentedI tried to come up with another way to go about this that at least initially looks quite compact, at least more compact than 3 radio buttons. Not sure I like it: very wordy and not sure it's that much clearer. My goal was to make this work more like a single choice about how to change the default of "show title". If you choose to check the box you get a second option to refine what "do not show" should mean.
*Edit* initially showed an earlier version of this design, updated the link to show the latest.
Comment #47
yoroy commentedComment #48
duaelfrWhat if we used the working version, which is totally standard and looks like any other similar thing in core, THEN in a follow-up, discuss about improving it? I'd like to get things done for accessibility first. Usability is great, but we are overthinking the problem this time.
I updated IS to add the screenshots
Comment #49
duaelfrSee the previous comment
Comment #50
mgiffordThis sounds like a good way to move forward to me. I've re-queued the patch in #30 and am fine with the idea of moving this new pattern into a separate issue.
Comment #52
duaelfrRerolled #30
Comment #53
duaelfrIt's better with the file :)
Comment #54
mgiffordTotally Agree @DuaelFr - you're totally teasing us though.. :)
Comment #55
duaelfrSorry, I don't know what happened twice :/
Comment #56
yoroy commentedYes, it's ok to start with the already existing pattern for how core handles this. I wasn't too happy with the other ideas yet either :)
Comment #57
flocondetoileSo, according to #38 and #56, can we RTBC this issue ? Is there yet remaining tasks ? Writing a change record is it really necessary ?
Comment #58
duaelfrLet's try to move it forward.
See #32, #38, #48 and #56.
Comment #60
andypostComment #62
duaelfrRerolled after #2785047: In Outside In mode, form validation messages should appear in the off-canvas tray, not the main page got commited (conflict solved interdiff attached)
I still hope it can go into 8.4 as the issue was RTBC before the alpha
Comment #64
naveenvalechaHere's the straight reroll of the patch.
Comment #65
naveenvalechaHere's the correct patch with correct status
Comment #69
naveenvalechaFixed the block module failures. Still having failures with SettingsTrayBlockFormTest
Comment #70
naveenvalechaComment #72
duaelfrFinalized the fix for 8.5 (I can't run the tests locally so I hope the testbot is gonna be happy)
Comment #74
duaelfrFixed the test failure
+ Fixed an issue where the block title field in the settings tray was not shown when needed
Comment #78
duaelfrStraight reroll of #74
Comment #80
edmonkey commentedFrom an WCAG accessibility viewpoint, I would also be looking for the 'completely remove title from output' configuration per block.
Having a heading rendered in some cases causes both heading structure issues (see image of H2 above H1), and generate superfluous headings which aren't helpful for users of assistive technologies (screen readers etc).
In some cases ARIA landmarks and HTML5 elements cover the need for menu/nav and search structure.
Any further dev/thoughts on this appreciated.
Comment #81
edmonkey commentedComment #82
duaelfr#80 the thing is that we currently only have an option to hide the title which is supposed to entirely remove it from the markup. But, some themes decided that for some blocks, like menu blocks, this could also mean to hide them visually to help users to navigate to these blocks by the heading jumping technique.
Be completing this issue, we could allow site builders to choose wisely which option they need.
Comment #83
andrewmacpherson commentedRe: #80
Over the years, the WebAIM screen reader user surveys have consistently shown that headings are the mostly likely way someone will try to find their way around the page. (Although this apparently wasn't asked in survey 6...) Landmark regions are growing in popularity but a surprising (to me) number of users aren't aware of them yet.
That's just an aside, though. As @DuaelFr says in #82, this issue is really about giving authors finer control over all block titles, by making the same options available to all block types, instead of treating menu blocks differently.
Further down the line, we might let authors choose a heading level per block - which might be very nice when combined with the new layout builder. But that can be a follow-up issue.
Comment #86
dqdThanks @duaelfr and the others working on this! That would be a massive improvement!
+1!
Comment #88
kishor_kolekar commentedRe-rolled against 9.1.x. Kindly review a patch.
Comment #89
kishor_kolekar commentedComment #93
jaykandariRerolling
Comment #96
raman.b commentedThis should resolve the majority of failed test cases
Comment #97
abhijith s commentedApplied patch #96 and it works fine.There will be visually hidden option available in the block title.
Adding screenshots
Block configuration:

markup:

Comment #98
abhijith s commentedComment #99
dqdDreditor goes "green" and I cannot see any drawbacks here ATM. Testing was without flaws but I would love to hear an opinion from somebody who is more into core block module, looking at it to make sure we do not create any bad follow ups. +1 for the work on this!
Comment #101
raman.b commentedResolving deprecation notice
Comment #102
barone@raman.b tested #101 and i confirm that all 3 options are working as expected, follow the screenshots.
UI - Select with the 3 options

Front end: Visually hidden

Front end: Hidden

Comment #103
baroneOne detail i just saw: Do we really need the - (dash + space) before the labels for Hidden and Visually hidden?
Comment #104
zebda commentedI tried both #89 and #93 on my 9.1 Drupal installation but they both cannot be applied. Since 9.2 is still in dev mode I prefer using 9.1.
Any idea what the problem could be?
Comment #105
bnjmnmThere's a missing "be" between should and hidden.
This @todo is referring to a Drupal version that has already released.
Change "visually-hidden" to "visually hidden" and remove "from end users"
This presents a backwards compatibility issue for contrib/custom that may be altering this form element, it would be difficult to get a change such as this through all the core gates. If there were a way to accomplish this by *adding* a form element instead (even if the UX isn't as good), it's more likely to be accepted as a change to core. Otherwise, this would probably need to wait until D10
An additional "make visually hidden" checkbox field that is only availavle when "visible" is selected may work. The value of that could conditionally add a
.visually-hiddenclass. Not as elegant, but more likely to land.No dashes/spaces should be added here, particularly since these characters are used elsewhere to represent hierarchy, and there's no hierarchy to these options.
Comment #106
nikitagupta commentedAddressed #105.1,2,3,5
Comment #107
BarisW commentedHere is a re-roll of #78 for Drupal 8.9.x
Comment #109
sutharsan commentedRe-roll of the #107 patch for Drupal 9.3.x.
Comments in #105 still need to be addressed.
Comment #112
louisnagtegaal commentedRe-roll of the patch in #109. Removed no longer necessary diff for
core/modules/block/tests/modules/block_test/config/install/block.block.test_block.yml
Comment #114
idiaz.ronceroRerolled patch in order to make it apply to 9.4.x version of Core.
There were (luckily) no conflicts to solve.
(edit: Forgot to attach the patch to this comment! as a result, the patch number is #114 but the comment will be #115, sorry)
Comment #115
idiaz.ronceroComment #117
sahil.goyal commentedTried to apply #114 patch but the given 9.5.x-dev version is not compatible with 10.1.x-dev so i reroll the patch and updated also attaching the reroll file.
Comment #118
pradhumanjain2311 commentedTry to fix patch #117 CCF errors.
Comment #120
bnjmnmI'm not sure why #107 attempted to reroll patch #78, despite later patches adding essential changes. It looks like there have since been 5 attempts to re-reroll the failed reroll of patch #78, even though it omits several desirable changes that have been made since.
#106 would have been the best choice as it addressed several concerns / problems that were spotted after #78. At this point, however, I recommend no more rerolls and instead creating a new patch entirely that is inspired by but not a reroll of 106. As I mentioned in #105.4, the solution is changing the configuration form in a way that breaks backwards compatibility in a manner that prevents it from being committable.
Changing this from a checkbox to a select will break form_alters for any contrib/custom that uses it. If you instead add an element to the form to set visually hidden (AJAX could be used to only make this field available when 'Display Title' is true). The UX would be slightly worse, but this would not break form API usages AND many of the tests that had to be changed to support this new feature won't have to be because the
'label_display'would still work the same, just augmented by a newly introduced'label_visually_hidden'optionComment #121
katannshaw commentedI'd love to have this feature for our current client and I like @bnjmnm's suggestions in comment #120. Is this something being actively worked on?
Comment #122
fskreuz commentedHere's a reworked #106 based on comments from #105. Added a new configuration field
label_display_typeseparate from the existinglabel_displaycheckbox that lets one select how one wants to hide the title. The hope would be that in a future version,label_displaywill just roll into the select box as the "visible" option, just like the patches prior. Name, label, and description could be better, open to suggestions.Tests removed in this patch, I wasn't sure if they'd still be applicable (I also don't have a test-capable setup at the moment). Hopefully someone can pick up the baton from here.
Comment #123
mgiffordMarking this as WCAG 2.4.6 https://www.w3.org/WAI/WCAG21/Understanding/headings-and-labels
Comment #125
fskreuz commentedReroll for 10.2.x
Comment #126
fskreuz commentedUpdated #125 for 10.3.x
Comment #127
fskreuz commentedUpdated #125 for 10.3.x, take 2.
Comment #128
andypostComment #131
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 necessarily 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 #132
guietc commentedHere is a new patch updated for Drupal 11
This update is based on the latest patch (#127), which I have rebased and adapted for compatibility with the 11.x branch.
Comment #133
mgiffordThis looks like it needs review @guietc.
Comment #134
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily 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 #135
quietone commentedTag cleanup
Comment #136
fskreuz commentedComment #137
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 necessarily 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 #139
dhavalpanchal commentedThe changes have been added as a patch file which is implemented in merge request 8596. The changes are working correctly on Drupal 11.3
Comment #141
liam morlandI have rebased the merge request.
Currently, when
label_displayis falsy,labelis empty. It should be thatlabelalways contains the label. This would allow a template to have code like this:Comment #142
smustgrave commentedHaven’t fully reviewed but tests are failing and CR is missing.
Comment #144
liam morlandI have created a draft of a different approach in merge request 15613. What this change does is put the block title in an
aria-labelattribute when a label exists but it would not otherwise be displayed. I only did this change in the default template. If this approach is accepted, then the merge request can be updated to change this template in all themes and add tests.Comment #145
smustgrave commentedThink summary should be updated if 2 approaches are being proposed.
That said if a new one is done then remaining tasks will have to be updated as this will have to go through the same checks.
Comment #146
liam morlandThe next step is to decide which approach to use.