Problem/Motivation
The documentation for table.html.twig says that you can pass an array containing the contents of a cell. Each cell is itself an array, with keys 'tag', 'attributes' and 'content' to specify exactly how to build that cell. Unfortunately, passing an array does not appear work.
I recently tried to make a table with some row headers, and quickly got bogged down when it failed to render. Here is sample code:
$build['clientDetails'] = [
'#type' => 'table',
'#rows' => [],
];
$build['clientDetails']['#rows'][] = [
[
'#tag' => 'th',
'#attributes' => ['scope' => 'row'],
'#content' => 'Client',
],
'Glinda'
];
$build['clientDetails']['#rows'][] = [
[
'#tag' => 'th',
'#attributes' => ['scope' => 'row'],
'#content' => 'Profession',
],
'Sorceress'
];
The expected output would be:
<table>
<tbody>
<tr>
<th scope="row">Client</th>
<td>Glinda</td>
</tr>
<tr>
<th scope="row">Occupation</th>
<td>Sorceress</td>
</tr>
</tbody>
</table>
The actual output is:
<table>
<tbody>
<tr>
<td #tag="th" #attributes="row" #content="Client"></td>
<td>Glinda</td>
</tr>
<tr>
<td #tag="th" #attributes="row" #content="Profession"></td>
<td>Sorceress</td>
</tr>
</tbody>
</table>
And the logs contain an error about implicitly converting an array to a string (the attributes sub-array that gets converted to "row").
I spent about fourteen hours struggling with this last week. I tried specifying 'cells' as the name of the array, which did not help. I tried it with and without a '#' at the front of the keys. I tried a whole bunch of stuff that did not help. I posted on the forums asking for assistance and Jaypan was kind enough to try it out and report that they're seeing the same behavior.
The twig template appears to be set up to handle this correctly, and the renderer is producing data that follows the structure. But somewhere between the module and the theme layer, the renderer is taking the manually-specified cell arrays and treating them as attributes arrays for a generic TD element. I spent some time trying to track down exactly where in the code this is happening, with no luck. The data seems to be intact through the end of early rendering, and that's where I lost the trail. I'm not sure where to look for the next stage of the rendering pipeline.
If this is a misunderstanding on my part and there is some way to make it work, I would be grateful for an example demonstrating how to do it.
Steps to reproduce
Pass some of the example code above to the renderer and observe the results.
Proposed resolution
Either correct the issue in the rendering pipeline, or update the documentation for table.html.twig to reflect that it is not currently possible to specify a cells array manually. Or if it is, explain how to do it.
Issue fork drupal-3437166
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
jaypanI have done some debugging and determined what is happening.
The Twig template states:
From within the context of the Twig template, this is correct. However, the template does not document how to set this variable, and digging through the code, it does not actually appear that this variable can be manually set. Instead, the variable is automatically set in code.
In theme, inc. I can see that the tag type is set here: https://git.drupalcode.org/project/drupal/-/blob/11.x/core/includes/them...
With the
$is_headervalue being set here: https://git.drupalcode.org/project/drupal/-/blob/11.x/core/includes/them...So, to set a table cell to use a TH tag, the
headerattribute for the cell must be set. Here is a simple example:Maybe an update to the table.html.twig documentation to indicate how to set a table cell as a header, would be beneficial to developers.
Comment #4
utkarsh_33 commentedComment #7
utkarsh_33 commentedUpdated the docs stating the use of header flag to identify whether a cell can be used a
Comment #8
smustgrave commentedUpdated the title to more lineup with the solution but seems straight forward.
Comment #9
jaypanThe updated documentation listed the
headerattribute as a child of a header cell, but it is not. It is a sibling todata. Also, there are multiple instances oftable.html.twigin core, and each instance needs to be updated.Comment #10
utkarsh_33 commentedComment #11
smustgrave commentedCan the MR be updated to point to 11.x vs 11.0.x
And sorry I didn't catch that earlier.
Comment #12
utkarsh_33 commentedComment #13
utkarsh_33 commentedComment #14
jaypan'header' is an key of the 'cells' array, so I think the documentation should set it as a child of that array.
Comment #15
smustgrave commentedFeedback appears to be addressed. Thanks @Utkarsh_33 for updating that target
Comment #16
nod_Can someone update the comment so that it wraps at 80 chars? after that it's good to go.
Comment #17
nod_Comment #18
smustgrave commentedWrapped to 80
Comment #24
nod_Committed and pushed 8778ac1801 to 11.x and 8a300d4337 to 11.0.x and 34abc72b52 to 10.4.x and 3923915994 to 10.3.x. Thanks!