Problem/Motivation

Grouping a table by a field adds a <caption> element with the field value above each group in the table.

Sadly the configured HTML element and classes are not used within the caption.

Steps to reproduce

See above

Proposed resolution

Print the field in the caption as it was configured (HTML element, classes, ...) like in the views result (unless 'remove tags' setting is enabled?)

Remaining tasks

User interface changes

API changes

Data model changes

Comments

anybody created an issue. See original summary.

anybody’s picture

Title: Table grouping does not use field-styles » Table grouping does not use field element and styles
Related issues: +#3170940: Missing option to group by field on accordion for 8.x-4.x

  • shelane committed bf10829d on 5.5.x
    fix: #3625896 Table grouping does not use field element and styles
    
shelane’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

anybody’s picture

Status: Fixed » Needs work

Thanks for the quick resolution @shelane! Great!

Still I think the second part is not yet solved:

Sadly the configured HTML element and classes are not used within the caption.

Sorry I think I didn't point that out clear enough.

The expected result (additionally to your fix) is that if you select an HTML element for the field you group by, that HTML element should be used, just like in the output of the field. This is especially helpful to make it a <h2> or <h3> and add classes.

The actual result is that only the plain text of the field is printed in the caption.

shelane’s picture

Ah, yes, I missed that. I'll take a look shortly.

  • shelane committed de7db609 on 5.5.x
    fix: #3625896 Table grouping does not use field element and styles
    
anybody’s picture

Thanks - quite some custom code. I would have hoped there's a helper method that can be used to render the field value first, before printing it into the caption?

['#markup'] is always risky regarding security, so we should be extra careful.

  • shelane committed 27829b3e on 5.5.x
    fix: #3625896 Correct grouped table caption wrappers
    
    Preserve the...
shelane’s picture

I just ran my changes through codex and it responded with a few suggested changes and a response on what is available. Note that I looked at what core views does for this and it does use markup.

The grouping field value is already rendered by Views. StylePluginBase::renderGrouping() calls getField(), which returns the sanitized field
output as a MarkupInterface. Core then places that rendered value in the caption.

There is no reusable helper that reapplies the field’s configured wrapper element and classes. That logic normally lives in
preprocessViewsViewFields(), which is not used for grouped table captions. Calling the field renderer again would duplicate rendering and
still would not add the wrapper.

The custom code therefore only recreates the configured wrapper using elementWrapperType() and elementWrapperClasses(). It does not mark raw
field data as safe. html_tag also escapes the tag and attributes.

I agree that extracting ['#markup'] is unnecessarily brittle. We can instead preserve the existing caption render array as a child:

$caption = $vars['caption'];
$vars['caption'] = [
'#type' => 'html_tag',
'#tag' => $wrapper_type,
'#attributes' => [
'class' => explode(' ', $wrapper_classes),
],
'content' => $caption,
];

This keeps Drupal’s existing rendering, escaping, and cacheability handling intact while adding only the configured wrapper.

anybody’s picture

Thanks @shelane that sounds good!

shelane’s picture

Status: Needs work » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

anybody’s picture

Hi @shelane just came across this again in a further project and had to think about this part again

- Theme developers can now use `.views-bootstrap-table-caption-grouping` selector in their theme CSS to style grouping captions differently

**Impact:** This enables proper visual differentiation between regular table captions and grouping headers, improving the user experience when using Views grouping with Bootstrap Table displays.

**Example theme CSS:**

/* Display grouping caption above table */
.views-bootstrap-table-caption-grouping {
  caption-side: top;
}

(from: https://git.drupalcode.org/project/views_bootstrap/-/commit/bf10829d5331...)

Is there ANY case where you'd want the grouping caption BELOW the table?

For "regular" Drupal users the outcome is still unextected in tables my eyes. Shouldn't we just add

/* Display grouping caption above table */
caption.views-bootstrap-table-caption-grouping {
  caption-side: top;
}

in general to have the expected behaivor also in tables? (Like in all other cases)?

anybody’s picture

Status: Fixed » Active