Update documentation for table.html.twig around table cells

Created on 30 March 2024, 9 months ago
Updated 12 June 2024, 6 months ago

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.

<!-- break -->

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.

🐛 Bug report
Status

Fixed

Version

10.3

Component
Theme 

Last updated about 19 hours ago

Created by

🇺🇸United States wdmartin

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024