Commit 5f47f20c authored by catch's avatar catch

Issue #2329767 by lauriii, alexpott, Cottser, sqndr: Move table classes from...

Issue #2329767 by lauriii, alexpott, Cottser, sqndr: Move table classes from preprocess to templates.
parent 8e0e7d3b
......@@ -1229,29 +1229,12 @@ function template_preprocess_table(&$variables) {
}
}
// Add the 'empty' row message if available.
if (empty($variables['rows']) && isset($variables['empty'])) {
$header_count = 0;
foreach ($variables['header'] as $header_cell) {
if (is_array($header_cell) && isset($header_cell['colspan'])) {
$header_count += $header_cell['colspan'];
}
else {
$header_count++;
}
}
$variables['rows'][] = array(array(
'data' => $variables['empty'],
'colspan' => $header_count,
'class' => array('empty', 'message'),
));
}
// Build an associative array of responsive classes keyed by column.
$responsive_classes = array();
// Format the table header:
$ts = array();
$header_columns = 0;
if (!empty($variables['header'])) {
$ts = tablesort_init($variables['header']);
......@@ -1263,11 +1246,18 @@ function template_preprocess_table(&$variables) {
$responsive_index++;
if (!is_array($cell)) {
$header_columns++;
$cell_content = $cell;
$cell_attributes = new Attribute();
$is_header = TRUE;
}
else {
if (isset($cell['colspan'])) {
$header_columns += $cell['colspan'];
}
else {
$header_columns++;
}
$cell_content = '';
if (isset($cell['data'])) {
$cell_content = $cell['data'];
......@@ -1305,22 +1295,20 @@ function template_preprocess_table(&$variables) {
$variables['header'][$col_key]['content'] = $cell_content;
}
}
$variables['header_columns'] = $header_columns;
// Rows and footer have the same structure.
$sections = array('rows' , 'footer');
foreach ($sections as $section) {
if (!empty($variables[$section])) {
$flip = array('even' => 'odd', 'odd' => 'even');
$class = 'even';
foreach ($variables[$section] as $row_key => $row) {
$cells = $row;
$row_attributes = array();
$no_striping = $section === 'footer';
// Check if we're dealing with a simple or complex row
if (isset($row['data'])) {
$cells = $row['data'];
$no_striping = isset($row['no_striping']) ? $row['no_striping'] : $no_striping;
$variables['no_striping'] = isset($row['no_striping']) ? $row['no_striping'] : FALSE;
// Set the attributes array and exclude 'data' and 'no_striping'.
$row_attributes = $row;
......@@ -1328,13 +1316,6 @@ function template_preprocess_table(&$variables) {
unset($row_attributes['no_striping']);
}
// Add odd/even class.
if (!$no_striping) {
$class = $flip[$class];
$row_attributes['class'][] = $class;
}
// Build row.
$variables[$section][$row_key] = array();
$variables[$section][$row_key]['attributes'] = new Attribute($row_attributes);
......@@ -1368,9 +1349,9 @@ function template_preprocess_table(&$variables) {
$cell_content = drupal_render($cell_content);
}
}
// Add active class if needed for sortable tables.
// Active table sort information.
if (isset($variables['header'][$col_key]['data']) && $variables['header'][$col_key]['data'] == $ts['name'] && !empty($variables['header'][$col_key]['field'])) {
$cell_attributes['class'][] = 'active';
$variables[$section][$row_key]['cells'][$col_key]['active_table_sort'] = TRUE;
}
// Copy RESPONSIVE_PRIORITY_LOW/RESPONSIVE_PRIORITY_MEDIUM
// class from header to cell as needed.
......
......@@ -28,9 +28,13 @@
* - attributes: Any HTML attributes, such as "colspan", to apply to the
* table cell.
* - content: The string to display in the table cell.
* - active_table_sort: A boolean indicating whether the cell is the active
table sort.
* - footer: Table footer rows, in the same format as the rows variable.
* - empty: The message to display in an extra row if table does not have
* any rows.
* - no_striping: A boolean indicating that the row should receive no striping.
* - header_columns: The number of columns in the header.
*
* @see template_preprocess_table()
*
......@@ -58,7 +62,12 @@
<thead>
<tr>
{% for cell in header %}
<{{ cell.tag }}{{ cell.attributes }}>
{%
set cell_classes = [
cell.active_table_sort ? 'active',
]
%}
<{{ cell.tag }}{{ cell.attributes.addClass(cell_classes) }}>
{{- cell.content -}}
</{{ cell.tag }}>
{% endfor %}
......@@ -69,7 +78,12 @@
{% if rows %}
<tbody>
{% for row in rows %}
<tr{{ row.attributes }}>
{%
set row_classes = [
not no_striping ? cycle(['odd', 'even'], loop.index0),
]
%}
<tr{{ row.attributes.addClass(row_classes) }}>
{% for cell in row.cells %}
<{{ cell.tag }}{{ cell.attributes }}>
{{- cell.content -}}
......@@ -78,6 +92,12 @@
</tr>
{% endfor %}
</tbody>
{% elseif empty %}
<tbody>
<tr class="odd">
<td colspan="{{ header_columns }}" class="empty message">{{ empty }}</td>
</tr>
</tbody>
{% endif %}
{% if footer %}
<tfoot>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment