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

  1. Create a view, either list or table format
  2. Choose Settings on format
  3. 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.

Style options for grouping label in unformatted list settings

Remaining tasks

Contributor tasks needed
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 Yes
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

Style options for grouping label in unformatted list settings

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.

Issue fork drupal-1383696

Command icon 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

Reg’s picture

It looks like it's a matter of overriding the views template views-view-unformatted.tpl.php . Is there another way or is this it?

dawehner’s picture

Status: Active » Fixed

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

Reg’s picture

Status: Fixed » Active

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

dawehner’s picture

Status: Active » Fixed

Well you could try to use the views_field_view module, but it's probably kind of hard to get the groupby working with it.

Reg’s picture

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

Status: Fixed » Closed (fixed)

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

stephen Piscura’s picture

Status: Closed (fixed) » Active

Reg,

I'm dancing around with views grouping fields with strange results. I have a users view that creates a "staff" page with these fields:

  • Username
  • Image (image field on user entity)
  • Bio (long text field on user entity)
  • Title (authored-by-user relationship: title of node)
  • Subtitle (authored-by-user relationship: text field on node)

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:

<h2>Username</h2>
<img>image of user</img>
<p>user bio</p>
<p>posts by user</p>
<h3>title</h3>
<h4>subtitle</h4>

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?

Reg’s picture

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

stephen Piscura’s picture

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

rogerrogers’s picture

Category: support » bug

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

dawehner’s picture

Component: Documentation » Code
Category: bug » feature

There are problems with doing this automatically

  • This would break all existing groupby views, because people never expected to work like that
  • You have to make it configurable based on the first argument
travis-cunningham’s picture

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

jlongbottom’s picture

I just created a template file that overrides for only that one view and changed the H3 to an H2

danny englander’s picture

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

damien_vancouver’s picture

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

<?php if (!empty($title)): ?>
  <div class="category-description"><?php print $title; ?></div>
<?php endif; ?>

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.

giorgio79’s picture

#14

I don't think this is really an issue with Views, it's a themer's task to solve things like this

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

dawehner’s picture

Just in case someone is interested, I opened a feature request against Drupal 8.

giorgio79’s picture

awesome, 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 :)

dawehner’s picture

The drop is always moving!

joakaune’s picture

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

HAg’s picture

The solution in #15, works in version 6.x too.

imperator_99’s picture

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

Abelito’s picture

#13 worked for me!

jillpadams’s picture

#15 perfect fix for Grid style as well.

aagd’s picture

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

sanjadmin’s picture

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

ladybug_3777’s picture

Issue summary: View changes

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



function YOURTHEMENAME_views_view_grouping($vars) {
  $view = $vars['view'];
  $title = $vars['title'];
  $content = $vars['content'];

  $output = '<div class="view-grouping">';
  $output .= '<div class="view-grouping-header">' . $title . '</div>';
  $output .= '<div class="view-grouping-content">' . $content . '</div>' ;
  $output .= '</div>';

  return $output;
}
scotwith1t’s picture

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

scotwith1t’s picture

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

<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>

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:

function ltl_preprocess_views_view_grouping(&$vars) {
  $vars['title'] = 'Group ' . $vars['grouping_level'];
}

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.

scotwith1t’s picture

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

andrezstar’s picture

#15 +1 cool

Dima DD’s picture

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

phily’s picture

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

millionleaves’s picture

Re: #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?

phily’s picture

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

talisa1987’s picture

#12 Simple and easy solution of the problem. Thank you very much Travis. You saved my time

eigentor’s picture

As of today the issue dawehner created did not get into core.
#15 appears to be the most manageable workaround with the least tradeoffs.

ErnoVanhala’s picture

I 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? :)

liam morland’s picture

The h3 should not be hard-coded. Will the maintainers accept a patch that makes the grouping element be configurable?

GuitarKat’s picture

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

chris matthews’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 7.x-3.x-dev » 9.1.x-dev
Component: Code » views_ui.module

I 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

kevinhbruce’s picture

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

mmlmitchell’s picture

Here's a work-around for this pesky problem...

  • Edit the field being used for grouping
  • Open "Rewrite results" > Check "Override the output"
  • Begin the override text string with </h3>, which cancels the troublesome html element
  • Insert your own tags and/or elements

Example:
The grouping field has the machine name of "field_model_component".
The override text =</h3><div class="my-style">{{ field_model_component }}</div>

liam morland’s picture

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

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

dalemoore’s picture

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

chrisgross’s picture

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

chrisgross’s picture

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

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

brianbrarian’s picture

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

theMusician made their first commit to this issue’s fork.

CarlyGerard made their first commit to this issue’s fork.

npacker made their first commit to this issue’s fork.

carlygerard’s picture

Title: How to change the HTML wrapped around a grouping output (HTML h3 tag) » Allow a custom HTML element to be selected for a grouping field
Issue summary: View changes
Issue tags: +Accessibility
Related issues: +#1880100: Allow to configure the h3 element for view style plugins
StatusFileSize
new125.35 KB
carlygerard’s picture

Issue summary: View changes

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

laura.gates’s picture

Is 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

{% if title %}
	<h3 id="{{ title|striptags|replace({"\n":'', "\r":''})|lower }}">{{ title }}</h3>
{% endif %}

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.

carlygerard’s picture

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

carlygerard’s picture

Issue summary: View changes

carlygerard’s picture

Issue summary: View changes
carlygerard’s picture

Issue summary: View changes
carlygerard’s picture

Issue summary: View changes
lendude’s picture

Status: Active » Needs work

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

carlygerard’s picture

Thanks 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):

FILE: [base-path]/test-drupal10/web/core/modules/views/src/Plugin/views/style/Grid.php
-----------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------------------------------------------------------------
 64 | ERROR | The array declaration extends to column 96 (the limit is 80). The array content should be split up
    |       | over multiple lines
-----------------------------------------------------------------------------------------------------------------

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?

carlygerard’s picture

carlygerard’s picture

I will write the change record for this ticket.

themusician’s picture

Issue summary: View changes
mgifford’s picture

Issue tags: +wcag131

I think this would apply to WCAG SC 1.3.1

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

carlygerard’s picture

StatusFileSize
new19.52 KB

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

carlygerard’s picture

StatusFileSize
new20.3 KB

Should fix Drupal command fails in 11.x re-roll patch from #80.

theMusician changed the visibility of the branch 1383696-how-to-change to hidden.

themusician’s picture

It 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

---- Drupal\Tests\editor\Functional\EditorSecurityTest ----
Status    Group      Filename          Line Function                            
--------------------------------------------------------------------------------
Exception Other      phpunit-93.xml       0 Drupal\Tests\editor\Functional\Edit
    PHPUnit Test failed to complete; Error: PHPUnit 9.6.15 by Sebastian
    Bergmann and contributors.
    
    Testing Drupal\Tests\editor\Functional\EditorSecurityTest
    ..E                                                                 3 / 3
    (100%)
    
    Time: 00:23.789, Memory: 4.00 MB
    
    There was 1 error:
    
    1)
    Drupal\Tests\editor\Functional\EditorSecurityTest::testEditorXssFilterOverride
    Behat\Mink\Exception\ExpectationException: The field "edit-body-0-value"
    value is "Hello, Dumbo Octopus!alert(0)", but "Hello, Dumbo
    Octopus!alert(0)" expected.
    
    /builds/issue/drupal-1383696/vendor/behat/mink/src/WebAssert.php:888
    /builds/issue/drupal-1383696/vendor/behat/mink/src/WebAssert.php:781
    /builds/issue/drupal-1383696/core/tests/Drupal/Tests/WebAssert.php:968
    /builds/issue/drupal-1383696/core/modules/editor/tests/src/Functional/EditorSecurityTest.php:447
    /builds/issue/drupal-1383696/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
    
    ERRORS!
    Tests: 3, Assertions: 137, Errors: 1.

fjgarlin changed the visibility of the branch drupal-1383696-82 to hidden.

fjgarlin changed the visibility of the branch drupal-1383696-82 to active.

acbramley changed the visibility of the branch gerardc/1383696 to hidden.

acbramley changed the visibility of the branch gerardc/1383696-c to hidden.

acbramley changed the visibility of the branch packern/view-grouping-header to hidden.

acbramley’s picture

All old MRs and patches are now hidden, work should be continued on https://git.drupalcode.org/project/drupal/-/merge_requests/7351

smustgrave made their first commit to this issue’s fork.

themusician’s picture

Status: Needs work » Needs review

New code appears to be passing.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs subsystem maintainer review

Left 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

CarlyGerard changed the visibility of the branch drupal-1383696-drupal-1383696-82 to hidden.

carlygerard’s picture

Status: Needs work » Needs review

The MR is now passing with the latest changes as requested (with some allowed fails in the pipeline). Updating status for the next stage of review.

smustgrave’s picture

Status: Needs review » Needs work

Moving to NW for the change record

But question what will happen if $usesGroupingLabelElement isn't present. Lot of contrib plugins will take a while to update.

themusician’s picture

Thank you for the feedback.

To answer your question about if a view does not have $usesGroupingLabelElement present, the twig templates handle that logic and print the title. https://git.drupalcode.org/project/drupal/-/merge_requests/7351/diffs?co...

If it is present, such as after this patch is applied, but not set, the code supports the current default of the grouping label being an h3 or empty.
https://git.drupalcode.org/project/drupal/-/merge_requests/7351/diffs?co...

Contrib plugins would have an option of using this way of handling additional HTML options or could continue doing it however they currently make it happen.

There are a few of the core themes that use the twig template overrides for views-view-unformatted.html.twig, views-view-list.html.twig, and a few more. You suggest updating these in this same patch. As I understand it, the twig templates would be updated where appropriate for claro, olivero, stark, and starterkit_theme but not stable9. The same approach would be for engines/twig excluding updates to stable9. Is that correct?

themusician’s picture

Issue tags: +Portland2024
themusician’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs change record

Good morning,

I have authored a proposed change record for review by others. https://www.drupal.org/node/3446094

I have also pushed an updated branch that includes theme updates to the core themes where the views templates are included. I did not, however, update Stable 9 as my understanding is that the Stable 9 theme is locked in place. I am happy to make the changes there if required.

smustgrave’s picture

Status: Needs review » Needs work

Went to review but appears MR has some failures

themusician’s picture

Status: Needs work » Needs review

The test had been failing due to this upstream bug, that has now been fixed. Thanks to nicxvan and mikelutz for troubleshooting over in #contribute on Slack.

Resolved bug that was causing test failures: https://www.drupal.org/project/drupal/issues/3448036

Tests are now passing.

nicxvan’s picture

Status: Needs review » Needs work

Coincidentally I just needed this patch myself. I suspect that this will need to add something to the ViewsConfigUpdater since it is adding config to Views.

I did this earlier this year and it can be quite complex, I'm working on documenting it here #3447755: Document ViewsConfigUpdater

I'm not sure why tests are not triggering on the changed config already, maybe cause it's a sub item under grouping, but this adds new config: grouping_label_element: h2.
I'm also not sure why it wouldn't be limited to heading tags like the views pager update linked in the documentation issue.

I'm happy to help guide someone on the views update config changes since I know it's a pretty significant wrench in the process.

This may need additional tests too.

nicxvan’s picture

It might be helpful to add an example of views-view-grouping.html.twig in the change record too since the starterkit theme generation is different than the example.

nicxvan’s picture

I am also getting this on pages with this patch, I think on views without a grouping set:
Undefined array key "grouping_label_element" in Drupal\views\Plugin\views\style\StylePluginBase->renderGroupingSets() (line 559 of core/modules/views/src/Plugin/views/style/StylePluginBase.php).

themusician’s picture

Thanks for the feedback. I agree that the undefined array key error is because the views did not get the update to set grouping_label_element to null.

The ViewsConfigUpdater code requirement makes sense to me as being necessary. On a separate issue, https://www.drupal.org/node/2770835, the test update never fires even though I wrote a function in ViewsConfigUpdater but I was not aware you have to also call the function in updateAll(). The documentation improvements for how to use ViewConfigUpdater is a great idea.

Here is a rough idea of what I think is needed. @CarlyGerard also involved with this ticket walked me through what this may end up requiring. In the protected function addGroupingLabel() I can't quite figure out what the handler represents. The parameter defines it as a display handler, so I think a view style is represented here as the handler_type "style" and that is also the plugin_id? These ideas came from one of the issues you referenced, thank you for the pointer - https://git.drupalcode.org/project/drupal/-/blob/96d21a4603df23080a34051....

  /**
   * Performs all required updates.
   *
   * @param \Drupal\views\ViewEntityInterface $view
   *   The View to update.
   *
   * @return bool
   *   Whether the view was updated.
   */
  public function updateAll(ViewEntityInterface $view) {
    return $this->processDisplayHandlers($view, FALSE, function (&$handler, $handler_type, $key, $display_id) {
      $changed = FALSE;

      if ($this->addGroupingLabelElement($handler, $handler_type, $view)) {
        $changed = TRUE;
      }

      return $changed;
    });
  }

  /**
   * Add Grouping Label to views without one.
   *
   * @param array $handler
   *   A display handler.
   * @param string $handler_type
   *   The handler type.
   * @param \Drupal\views\ViewEntityInterface $view
   *   The View being updated.
   *
   * @return bool
   *   Whether the handler was updated.
   */
  protected function addGroupingLabelElement(array &$handler, string $handler_type, ViewEntityInterface $view): bool {
    $changed = FALSE;

    // Add grouping label element to existing views.
    if (($handler_type === 'style')
      && isset($handler['plugin_id'], $handler['type'])
      && $handler['plugin_id'] === 'style'
      && $handler['type'] === 'Grid' || 'HtmlList' || 'GridResponsive' || 'DefaultStyle'
      && !isset($handler['style']['grouping_label_element'])) {
      $handler['style']= ['grouping_label_element' => NULL];
      $changed = TRUE;
    }

    return $changed;
  }

You asked, why the allowed grouping elements would not be restricted to just heading tags like the views pager update. For the grouping use cases, grid, HtmlList, GridResponsive it seemed hard to predict that one would only ever group by heading. Using labelELements, https://git.drupalcode.org/project/drupal/-/merge_requests/7351/diffs#11..., makes it so available elements do not need to be maintained in this code as well as the views.settings. I concur that the most common use case would be headings but I am not confident that there would never be a need to group by another element.

Thanks again for the feedback, the questions, and the guidance.

nicxvan’s picture

At a quick glance that looks right: also if you want to link an issue in a comment where the issue status color comes up can you use:
[#issuenumber]

like this #2770835: Add support for tables with two headers in views table display

themusician changed the visibility of the branch drupal-1383696-drupal-1383696-82 to active.

themusician changed the visibility of the branch drupal-1383696-drupal-1383696-82 to hidden.

themusician’s picture

Status: Needs work » Needs review

I believe the ViewsUpdateConfig functions now are properly looping through existing views and setting the grouping to the default if it is not already set.

If using grouping on existing views, the default is the h3 element as it is already set to, after the config update executes.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new8.03 KB

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

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

robcarr’s picture

I'm unable to apply the MR as a patch against 11.x

ehlovader made their first commit to this issue’s fork.

Attempted to rebase for rerolling the patch.
Looking into failing PHPcs and PHPstan tests next

ehlovader changed the visibility of the branch drupal-1383696-82-11.x to hidden.

ehlovader changed the visibility of the branch drupal-1383696-82-11.x to hidden.

ehlovader’s picture

Well I tried rerolling the MR but ran into a bunch of PHPCS and PHPStan errors in the build, but locally they aren't detecting any issues when I run them for Drupal standards. In addition to that the patch won't apply normally.

I believe some of my trouble applying the patch locally despite the phpcs stuff is related to rebasing across a rewrite to use class based hooks.

I'll keep looking into it, started to rebase on a new branch but was still on the main branch shown, maybe I will look into that new branch again to try and avoid introducing more issues.

When I did get the patch applied though I saw the dropdown for group markup, but I think I lost the multiple grouping levels that I'd expected.

ehlovader’s picture

Just pushed the original back to 39368b50 from another branch, I will continue trying to rebase from the other 11.x branch I started

ehlovader changed the visibility of the branch drupal-1383696-82-11.x to active.

ehlovader’s picture

@robcarr confirmed the reroll of this fix onto 11.x in MR !15366 works on 11.3, and patches cleanly.

There are a lot of PHPCS constraints added to main that will require some work to clean up and make this mergable there.

themusician’s picture

How exciting to see movement on this again. Thank you for pushing this along.

ehlovader’s picture

It does appear that there was more to the refactoring of addGroupingLabelElement than just getting it to use an array format for the display handler and styles values.

The structure of the style config appears to have changed and is deeper than it was. it was no longer just grouping_label_element but now has more and multiple group levels.

I also found that this has no effect on HTML Lists in the Bootstrap5 theme, the views-view-list template is overridden there and is also hardcoded with an h3

I might take a closer look at the display handler structure, see if it is an easy fix, but the theme situation feels like more of a blocker to me right now