Problem/Motivation
As you can see from below the H3 tag which is generated from using grouping (@ Format: Unformatted list | Settings:Grouping field) has no divs around it like the regular fields. In my case I would like to wrap some HTML around it but I can't see where. On the fields it's easy, just override the output with what you want but on this generated group title I haven't found how to do this so any help will be much appreciated.
<div class="view-content">
<h2><a href="/blogs/83" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Internet Department</a></h2>
<div class="views-row views-row-1 views-row-odd views-row-first views-row-last">
<div class="views-field views-field-picture">
<div class="field-content">
Steps to reproduce
- Create a view, either list or table format
- Choose Settings on format
- Choose a grouping field to create a grouped display
The output sets the grouping label at a static <h3>
, which can cause heading structure inaccessibility by outputting grouping as incorrect heading levels. This creates possible WCAG violations with no way to remedy as a site builder.
Proposed resolution
Expose a grouping label tag for each grouping field where the user can specify a wrapping label element by which to group the records via the Views UI.
Remaining tasks
Task | Novice task? | Contributor instructions | Complete? |
---|---|---|---|
Create a merge request | Yes | Merge request instructions | Yes, see gerardc/1383696-c |
Get feedback on which tests should be created or modified (or grab tests from related issue) | No | Instructions | Yes |
Write change record for the API changes | Yes | Change record Instructions | No |
Manually test the patch or merge request | Novice | Patch test Instructions | No |
Add steps to reproduce the issue | Novice | Instructions | Yes |
User interface changes
Adding a grouping label tag to the style options dialog allows for adjusting the tag per grouping field
API changes
A new grouping label tag element is being added to the existing view templates where a static <h3>
was used to wrap a grouping label.
{% if title %} {% if grouping_label_element is empty %} {{ title }} {% else %} <{{ grouping_label_element }}>{{ title }}</{{ grouping_label_element }}> {% endif %} {% endif %}
This code is added to the following files:
- views-view-grouping.html.twig
- views-view-grid-responsive.html.twig
- views-view-grid.html.twig
- views-view-list.html.twig
- views-view-unformatted.html.twig
The {{ grouping_label_element }}
value comes from StylePluginBase.php, which adds a new grouping_label_element array key in the form grouping array and applies if usesGroupingLabelElement = true
. The following PHP style templates have that value set to TRUE:
- DefaultStyle.php
- Grid.php
- GridResponsive.php
- HtmlList.php
By default the value of this key is an <h3>
element, which should prevent breakage of views that don't override the template.
Data model changes
No changes affecting the data model.
Release notes snippet
To be determined.
Comment | File | Size | Author |
---|---|---|---|
#57 | MicrosoftTeams-image.png | 125.35 KB | CarlyGerard |
Issue fork drupal-1383696
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
Reg CreditAttribution: Reg commentedIt looks like it's a matter of overriding the views template views-view-unformatted.tpl.php . Is there another way or is this it?
Comment #2
dawehnerEven overriding a tpl doesn't help as this is not themable at the moment.
There is an issue #477338: Multiple group: group by more than one field which would allow you to theme that.
Comment #3
Reg CreditAttribution: Reg commentedThanks for that, I think that issue is a little more complex than my needs however now seeing that it's an issue even for one group I'm thinking a little more creatively.
Have you ever considered using a view within a view as a solution? Perhaps it will put a wrapper around the inner view so we have some way of distinguishing what's group from a major heading. If so, in theory you can have an unlimited number of views in views so that may solve the issue of groups within groups as well.
Comment #4
dawehnerWell you could try to use the views_field_view module, but it's probably kind of hard to get the groupby working with it.
Comment #5
Reg CreditAttribution: Reg commentedIf you do it correctly you won't need group by. In the parent view you make a simple list by what you would normally use as a group by and pass the value as an argument to the child view. Use the passed parameter to limit the results in the child view and you are mimicking a one to many relationship in the database. You should be able to do this to as many levels as you like barring any limitations in the module.
Comment #7
stephen Piscura CreditAttribution: stephen Piscura commentedReg,
I'm dancing around with views grouping fields with strange results. I have a users view that creates a "staff" page with these fields:
If i choose to group by username, the grouping gets surrounded by an H3. I then exclude this field from display so that it doesn't display in both the grouping andinterior fields. But if i follow the same logic by adding the bio field as a second grouping field, then it gets the H3 and the username becomes a simple div. Obviously wrapping a long text field in a H3 is not what i want to do. What i ultimately want is something like this:
I don't know how to build the kind of views parent/child relationship using contextual filters which you've described. Can you help me and others understand how this works?
Comment #8
Reg CreditAttribution: Reg commentedI think the answer is I use a module called "views_field_view" (http://drupal.org/project/views_field_view) not contextual filters. Have a look at that module and see if it will do what you want.
Comment #9
stephen Piscura CreditAttribution: stephen Piscura commentedThanks for your reply Reg,
I was hoping to solve my use-case without an extra module, but if i absolutely cannot, then i'll definitely look into Views Field View.
Comment #10
RogerRogers CreditAttribution: RogerRogers commentedPersonally, I think this could use a basic fix: Honor the HTML tag setting of the field that the grouping is based on ('Grouping field nr.N').
All the functionality is there, including the ability to override the content, change the surrounding tags, etc. If the grouping setting just used the related fields settings this would be a breeze. I also think this would naturally be expected (at least I anticipated this would be the default behavior).
Comment #11
dawehnerThere are problems with doing this automatically
Comment #12
travis-cunningham CreditAttribution: travis-cunningham commentedIf you change the format to HTML list and select a grouping field it gives you options to add classes to the wrapping element as well as the list and the list parent.
Comment #13
jlongbottom CreditAttribution: jlongbottom commentedI just created a template file that overrides for only that one view and changed the H3 to an H2
Comment #14
Danny EnglanderI just ran into this issue as well. It was a simple matter of switching the display to Semantic Views (you'll need that module to do this) and then change the "GROUPING TITLE > Element" to h2. I don't think this is really an issue with Views, it's a themer's task to solve things like this (i.e. #13 above using a template file is a good example as well). Views gives you what are reasonable defaults but it can't anticipate every possible scenario and use case for a themer and a design.
Comment #15
damien_vancouver CreditAttribution: damien_vancouver commentedI solved this problem using the solution in #13 of overriding the views template file in my custom theme.
I can confirm that it does work despite the comment in #2. You must override the views style template for your currently selected style (I used unformatted list so that is the template I describe). Here are the exact steps I used.
1. Copy the views-view-unformatted.tpl.php template file from sites/all/modules/views/theme and place it in your theme's templates folder.
2. Figure out the machine name of your view. Mine was called products_by_category. One way to see this machine name is to edit the view and then look at the URL in your browser address bar. It will contain the machine name: admin/structure/views/view/products_by_category/edit
3. Rename the template file you copied in step one to have your view name after --. So I renamed mine to: views-view-unformatted--products_by_category.tpl.php so it would only override for this particular view.
4. Edit the template file and adjust the line displaying the title to use whatever you want instead of h3. I made mine use a <div class="category-description"> instead of the <h3>:
5. Save everything and clear your site caches.
6. Now when you load your view it should use the new overridden template and your group field will have the markup you want.
Comment #16
giorgio79 CreditAttribution: giorgio79 commented#14
That's why Views should not be adding h3 tags to every group 2 level...
Views adds h3 tags for both HTML List and Unformatted list for group 2. Group 1 gets no heading at all it seems.
One can already provide custom style settings for a field. Views should use that instead of this hardcoded h3...
Comment #17
dawehnerJust in case someone is interested, I opened a feature request against Drupal 8.
Comment #18
giorgio79 CreditAttribution: giorgio79 commentedawesome, here is the ref #1880100: Allow to configure the h3 element for view style plugins
Just peeked at your post tracker dawehner. Wow, you're doing a massive amount of work. A thread updated around every 10 minutes :)
Comment #19
dawehnerThe drop is always moving!
Comment #20
joakaune CreditAttribution: joakaune commentedI just encountered the same problem.
And I can't for the life of me understand the logic behind views adding a random h3 tag in the middle of everything.
It makes no sense at all, and just creates extra work since you have to create a workaround.
Please remove the h3 tags and make everything unformatted unless you add custom style settings to it.
Comment #21
HAg CreditAttribution: HAg commentedThe solution in #15, works in version 6.x too.
Comment #22
imperator_99 CreditAttribution: imperator_99 commentedSolution in #15 works perfectly. I changed the H3 to H2 and it all works beautifully. Should not have been an issue at all though - views really should allow you to select the style for the grouping fields just as it does for other fields, as giorgio79 suggested.
Comment #23
Abelito CreditAttribution: Abelito commented#13 worked for me!
Comment #24
jillpadams CreditAttribution: jillpadams commented#15 perfect fix for Grid style as well.
Comment #25
aagd CreditAttribution: aagd commented#15 (damien_vancouver) So easy! Thanks for the instructions. With this custom template I could also add an extra div wrapper around the list to simplify placing on a susy grid.
Comment #26
sanjadmin CreditAttribution: sanjadmin commentedThanks. Post #15 worked for an html list presented in a block. In my case I am using an html list view so my filename is views-view-list--rss_block.tpl.php.
Comment #27
ladybug_3777 CreditAttribution: ladybug_3777 commentedThere is a themeable function that you can override to style the group however you want. Simply use this function in your template.php file in your theme: (This function originally comes from theme.inc within the view module)
Comment #28
scotwith1tAm I crazy, or does this (the theme function above, as well as the views-view-grouping.tpl.php file) only work if you have multiple levels of grouping? I suppose that's because of dawehner's comment about breaking backwards compatibility?
Comment #29
scotwith1tSo a nice little trick I just figured out:
1) Set the grouping field to the same field for level 1 and 2
2) Remove this chunk from your views-view-style.tpl.php file (copy it to your theme and modify there)
This removes the grouping markup for level 1
3) Copy the views-view-grouping to your theme and go nuts. You can (probably want to, unless you want to modify all groupings) rename to target just the view you're working on (views-view-grouping--view_machine_name--display_id.tpl.php or the like). If you're ok with targeting the way ALL groupings are rendered, the theme function that @ladybug mentioned should do the trick too.
In fact, I created a views-view-grouping--view_machine_name--display_id.tpl.php, which worked great, but then I tried a preprocess function named MYTHEME_preprocess_views_view_grouping__view_machine_name__display_id(&$vars) but it didn't work. I tried getting less specific, like just MYTHEME_preprocess_views_view_grouping__view_machine_name(&$vars) but to no avail. Only the MYTHEME_preprocess_views_view_grouping(&$vars) works from what I tried:
Finally, note that there is a known bug that is being worked on, where $vars['grouping_level'] is always 0 to the template from the preprocess function above. #1488744: Grouping level is always zero in views_plugin_style::render_grouping_sets() has a reasonable approach to handle in your theme and patches underway.
Comment #30
scotwith1tAlso, while I'm here, any ideas how to get a count of the groups? I don't see any way to do so at first glance...
Comment #31
andrezstar CreditAttribution: andrezstar commented#15 +1 cool
Comment #32
Dima DD CreditAttribution: Dima DD commentedRegarding formatting titles of the 1-st (outmost) grouping level in multi-level grouping case, my solution: I've used firebug and noticed that they have css class="view-grouping-header". This class is used in views-view-grouping.tpl.php (and in theme.inc as well). So I've simply declared this class in vews.css (just for testing) and set desired font setting there, and now my 1-st level titles looks great.
Comment #33
PhilYWhy not rewrite the output of the field that is used for grouping, wrapping it with the required HTML tags?
No hook nor template nor extra module needed (at least with Views 7.x-3.18).
This also works for multiple groupings: rewrite each field output as wished.
Comment #34
millionleaves CreditAttribution: millionleaves as a volunteer and commentedRe: #33 - I am doing that, but all the rewriting is being wrapped in a H3 tag. My rewriting is in a custom field which is used as the grouping field. @PhilY - how did you get it working?
Comment #35
PhilY@millioleaves: on the view's fields list, edit the one you wish to use for grouping to rewrite its output to (for example):
<h2>[node:field_myfield]</h2>
. Usually, you would also hide that field.Then, when you use it for grouping, it outputs as rewritten rather than with native
<h3>
wrapper.Comment #36
talisa1987 CreditAttribution: talisa1987 commented#12 Simple and easy solution of the problem. Thank you very much Travis. You saved my time
Comment #37
eigentor CreditAttribution: eigentor commentedAs of today the issue dawehner created did not get into core.
#15 appears to be the most manageable workaround with the least tradeoffs.
Comment #38
ErnoVanhala CreditAttribution: ErnoVanhala commentedI stumbled upon this issue now. I have to say that this really sounds strange that you have no way to select the tag for grouping when you have it with normal fields. And then there is forced H3 for the first last grouping item.
How in the Earth this kind of thing has passed testing years ago and is still not fixed? :)
Comment #39
Liam MorlandThe
h3
should not be hard-coded. Will the maintainers accept a patch that makes the grouping element be configurable?Comment #40
GuitarKat CreditAttribution: GuitarKat as a volunteer commentedh3 should not be hard-coded, I agree. I have found a workaround since then by applying a class on the text affected by it via a custom field. It is so strange.
Comment #41
Chris Matthews CreditAttribution: Chris Matthews commentedI just ran into this myself in Drupal 8 core Views UI. There is probably very little chance this will get 'fixed' in Drupal 7 contrib Views so updating the IS accordingly.
Project: Drupal core
Version: 9.1.x-dev
Component: views_ui.module
Comment #42
kevinhbruce CreditAttribution: kevinhbruce commentedChiming in with this, as well. I also just ran into this. Although, I found that the previous dev hardcoded it. I'd love to be able to delete this comment now, but it seems that's not an option.
Comment #43
mmlmitchell CreditAttribution: mmlmitchell commentedHere's a work-around for this pesky problem...
</h3>
, which cancels the troublesome html elementExample:
The grouping field has the machine name of "field_model_component".
The override text =
</h3><div class="my-style">{{ field_model_component }}</div>
Comment #44
Liam Morland@#43: This likely works visually in many themes, but it would result in invalid HTML and would have accessibility problems due to the empty header element.
Comment #47
dalemoore CreditAttribution: dalemoore commentedI've solved this for years using the template copy method in D7, but also never understood why you can change the H-level in the field UI but not for the grouping. I ran into it yet again just today and so came back to see if anyone else had recently posted. 😄 IMO, headings should always be configurable, whether in a field, a block, view, layout builder, whatever for proper outline/accessibility.
Comment #48
chrisgross CreditAttribution: chrisgross commentedI just ran into an issue in Drupal 8.9 where I cannot override the views grouping template at all if I don't have at least two grouping levels. views-view-grouping.html.twig does not seem do anything at all in this situation, nor can I trigger template_preprocess_views_view_grouping(). If I use two grouping levels, the second one does not have a template that I can override. This seems similar to what scotwith1t mentioned in #28, but his solution appears to have been for D7 and I cannot find a corresponding workaround for D8.
If I enable the twig markup debugging, it says the grouping title uses views-view-field.html.twig, but there does not seem to be any actual grouping going on in the templates at all. I don't have an h3 at all to remove, but I desperately need to be able to wrap field groupings in custom markup and I cannot. Has anyone discovered a similar workaround for this? Drupal's theme layer is basically telling me that the grouping I set in the views UI is not actually a grouping. It is maddening.
Comment #49
chrisgross CreditAttribution: chrisgross commentedSo far, the only simple workaround I've found is to add a dummy custom text field to the view, hide it from display, and use that as the first grouping level. Then, the real group I actually wanted to create can be grouping level two and Drupal will let me theme that.
Alternatively, you can edit your twig template, in this case,
views-view-unformatted.html.twig
and do some complicated logic where you repeatedly check the condition{% if view.style_plugin.options.grouping %}
and then insert and modify whatever markup is needed to properly group things together. For second level grouping and deeper, you'd still have to modify
views-view-grouping.html.twig
.But this seems like a really bad bug that needs to be fixed. I imagine it shouldn't be so confusing to figure out that grouping is treated differently depending on the grouping level and then come up with completely different approaches.
Comment #52
brianbrarian CreditAttribution: brianbrarian commentedThe Semantic Views contrib module mentioned in #14 seems to work as a workaround, as long as you want your view to display as an unformatted list or html list. And I was able to use CSS to make an unformatted list in a Semantic Views Style display look like a multi-column vertical-alignment grid display.
But I'm a +1 that this needs to be fixed in core.
Comment #57
CarlyGerardComment #58
CarlyGerardComment #60
laura.gatesIs it possible for this to be back-ported to 9.4.x? I'm running into an issue that I think this issue resolves where grouping by a date field results in duplicate content because when editing the views-view-unformatted.html.twig file it thinks that
is the title field, when in fact it's the grouped by field. Thus making it almost impossible to then create a
{% for row in rows %}
loop.Comment #61
CarlyGerardHi Laura, since this is a new feature, Drupal recommends we develop for 10.1.x (see comment #59), so our team can't backport this fix to 9.4.x. If you have the dev time and need this for your site, you're welcome to use the dev branch as a baseline and make a custom 9.4.x patch for your site and/or theme.
Comment #65
CarlyGerardComment #70
CarlyGerardComment #71
CarlyGerardComment #72
CarlyGerardComment #73
LendudeJust a warning, since I have no idea what the actual state of this is, the MR currently contains a massive amount of unrelated code clean up, if this is to have any chance of landing those need to be removed.
Comment #74
CarlyGerardThanks for taking a look and giving the pull request a review @lendude. I think "needs work" as a status makes sense for now--it is an active ticket but the next major task on our list is writing tests, and I think there's redundant line cleanup I agree needs to happen before we go on to that step (it seems like there were some rebase/conflict issues I missed, causing the repeated lines).
Some code changes, though, are based on results we get from running the PHPCS Drupal standard, would you consider that "unrelated code"? When we run Drupal PHPCS on the 10.1.x version of Grid.php on line 64, for example, we get the following error (this becomes line 71 with the MR 2922 changes):
Since we're developing on this file and should push code that meets Drupal's coding standards, it seems like it would be our responsibility then to help get the newer version of the file up to standard. Agreed all of our new code should follow that standard, but if there are minor changes like that one (1-5 lines tops), should we not get the code syntax up to standard as well?
Comment #75
CarlyGerardComment #76
CarlyGerardI will write the change record for this ticket.
Comment #77
theMusician CreditAttribution: theMusician at Western Washington University commentedComment #78
mgiffordI think this would apply to WCAG SC 1.3.1
Comment #80
CarlyGerardThis patch file re-rolls previous MRs from Gitlab workflow as a patch file for 11.x Work still needs to be done in figuring out caching so the wrapper tag option is stored properly. A new patch is needed once that's addressed.
Comment #81
CarlyGerardShould fix Drupal command fails in 11.x re-roll patch from #80.
Comment #83
theMusician CreditAttribution: theMusician at Western Washington University commentedIt appears the patch was applied cleanly. There is what seems to be an unrelated error in a unit test when testing the merge request.
https://git.drupalcode.org/issue/drupal-1383696/-/jobs/1255878
Drupal\Tests\editor\Functional\EditorSecurityTest 0 passes 1 exceptions
FATAL Drupal\Tests\editor\Functional\EditorSecurityTest: test runner returned a non-zero error code (2).
Drupal\Tests\editor\Functional\EditorSecurityTest 0 passes 1 fails
Comment #90
acbramley CreditAttribution: acbramley at PreviousNext commentedAll old MRs and patches are now hidden, work should be continued on https://git.drupalcode.org/project/drupal/-/merge_requests/7351
Comment #92
theMusician CreditAttribution: theMusician at Western Washington University commentedNew code appears to be passing.
Comment #93
smustgrave CreditAttribution: smustgrave at Mobomo commentedLeft some comments.
Didn't check but if any of those templates are declared in a theme in core they'll have to be updated too.
Definitely will need a CR and probably submaintainer sign off