Problem/Motivation
It seems not easy to set table row attributes using the module Reference Table Formatter. However a look at Drupal core in function template_preprocess_table
in file includes/theme.inc
shows a simple solution to such a problem. Since,
foreach ($variables[$section] as $row_key => $row) {
$cells = $row;
$row_attributes = [];
// Check if we're dealing with a simple or complex row
if (isset($row['data'])) {
$cells = $row['data'];
$variables['no_striping'] = $row['no_striping'] ?? FALSE;
// Set the attributes array and exclude 'data' and 'no_striping'.
$row_attributes = $row;
unset($row_attributes['data']);
unset($row_attributes['no_striping']);
}
// Build row.
$variables[$section][$row_key] = [];
$variables[$section][$row_key]['attributes'] = new Attribute($row_attributes);
$variables[$section][$row_key]['cells'] = [];
Hence without key data the attributes of the row stay to [] and with a way to set the attributes is shown. The module Reference Table Formatter does not use such a key data for row. In function getTable
in file EntityToTableRenderer.php
,
foreach ($table_rows as $table_row) {
$row = &$table['#rows'][]['data'];
foreach ($table_columns as $field_name => $field_title) {
// If the field we need to render exists on the entity, render it
// without a title, otherwise fill the cell anyway with a default.
if (isset($table_row[$field_name])) {
$table_row[$field_name]['#label_display'] = 'hidden';
$row[] = $this->renderer->render($table_row[$field_name]);
}
else {
$row[] = $settings['empty_cell_value'];
}
}
}
Steps to reproduce
Proposed resolution
Change one line in src/EntityToTableRenderer.php
in function getTable
ligne 62
- $row = &$table['#rows'][];
+ $row = &$table['#rows'][]['data'];
Remaining tasks
User interface changes
API changes
Data model changes
For data from the module,
$table['#rows'][$row_key][$cell_key]
to,
$table['#rows'][$row_key]['data'][$cell_key]
Release notes snippet