Issue #1843766 by joelpittet: Convert views/templates/views-view-table.tpl.php to twig.

8.0.x
Alex Pott 2013-05-24 10:24:22 -07:00
parent 19613d9b70
commit 6ac1d47a27
3 changed files with 81 additions and 69 deletions

View File

@ -0,0 +1,54 @@
{#
/**
* @file
* Default theme implementation for displaying a view as a table.
*
* Available variables:
* - attributes: Remaining HTML attributes for the element.
* - class: HTML classes that can be used to style contextually through CSS.
* - title : The title of this group of rows.
* - header: Header labels.
* - header_classes: HTML classes to apply to each header cell, indexed by
* the header's key.
* - rows: Table row items. Rows are keyed by row number, fields within rows
* are keyed by field ID.
* - field: Table data field ID.
* - content: Table data content.
* - row_classes: HTML classes to apply to each row, indexed by row number.
* This matches the index in rows.
* - field_classes: HTML classes to apply to each row, indexed by row number.
* This matches the index in columns and rows.
*
* @see template_preprocess()
* @see template_preprocess_views_view_table()
*
* @ingroup themeable
*/
#}
<table{{ attributes }}>
{% if title is not empty %}
<caption>{{ title }}</caption>
{% endif %}
{% if header %}
<thead>
<tr>
{% for key, field in header %}
<th{{ header_classes[key] }} scope="col">
{{ field }}
</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tbody>
{% for row_count, row in rows %}
<tr{{ row_classes[row_count] }}>
{% for field, content in row %}
<td{{ field_classes[field][row_count] }}>
{{ content }}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>

View File

@ -1,48 +0,0 @@
<?php
/**
* @file
* Template to display a view as a table.
*
* - $title : The title of this group of rows. May be empty.
* - $header: An array of header labels keyed by field id.
* - $header_classes: An array of header classes keyed by field id.
* - $fields: An array of CSS IDs to use for each field id.
* - $classes: A class or classes to apply to the table, based on settings.
* - $row_classes: An array of classes to apply to each row, indexed by row
* number. This matches the index in $rows.
* - $rows: An array of row items. Each row is an array of content.
* $rows are keyed by row number, fields within rows are keyed by field ID.
* - $field_classes: An array of classes to apply to each field, indexed by
* field id, then row number. This matches the index in $rows.
* @ingroup views_templates
*/
?>
<table <?php print $attributes; ?>>
<?php if (!empty($title)) : ?>
<caption><?php print $title; ?></caption>
<?php endif; ?>
<?php if (!empty($header)) : ?>
<thead>
<tr>
<?php foreach ($header as $field => $label): ?>
<th <?php print $header_classes[$field]; ?> scope="col">
<?php print $label; ?>
</th>
<?php endforeach; ?>
</tr>
</thead>
<?php endif; ?>
<tbody>
<?php foreach ($rows as $row_count => $row): ?>
<tr <?php print $row_classes[$row_count]; ?>>
<?php foreach ($row as $field => $content): ?>
<td <?php print $field_classes[$field][$row_count]; ?>>
<?php print $content; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>

View File

@ -429,44 +429,52 @@ function template_preprocess_views_view_summary_unformatted(&$vars) {
} }
/** /**
* Display a view as a table style. * Prepares variables for views table templates.
*
* Default template: views-view-table.html.twig.
*
* @param array $vars
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/ */
function template_preprocess_views_view_table(&$vars) { function template_preprocess_views_view_table(&$vars) {
$view = $vars['view']; $view = $vars['view'];
// We need the raw data for this grouping, which is passed in as $vars['rows']. // We need the raw data for this grouping, which is passed in
// as $vars['rows'].
// However, the template also needs to use for the rendered fields. We // However, the template also needs to use for the rendered fields. We
// therefore swap the raw data out to a new variable and reset $vars['rows'] // therefore swap the raw data out to a new variable and reset $vars['rows']
// so that it can get rebuilt. // so that it can get rebuilt.
// Store rows so that they may be used by further preprocess functions. // Store rows so that they may be used by further preprocess functions.
$result = $vars['result'] = $vars['rows']; $result = $vars['result'] = $vars['rows'];
$vars['rows'] = array(); $vars['rows'] = array();
$vars['field_classes'] = array(); $vars['field_classes'] = array();
$vars['header'] = array(); $vars['header'] = array();
$options = $view->style_plugin->options; $options = $view->style_plugin->options;
$handler = $view->style_plugin; $handler = $view->style_plugin;
$default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE; $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
$row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE; $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
$fields = &$view->field; $fields = &$view->field;
$columns = $handler->sanitize_columns($options['columns'], $fields); $columns = $handler->sanitize_columns($options['columns'], $fields);
$active = !empty($handler->active) ? $handler->active : ''; $active = !empty($handler->active) ? $handler->active : '';
$order = !empty($handler->order) ? $handler->order : 'asc'; $order = !empty($handler->order) ? $handler->order : 'asc';
// A boolean variable which stores whether the table has a responsive class. // A boolean variable which stores whether the table has a responsive class.
$responsive = FALSE; $responsive = FALSE;
$query = tablesort_get_query_parameters(); $query = tablesort_get_query_parameters();
if (isset($view->exposed_raw_input)) { if (isset($view->exposed_raw_input)) {
$query += $view->exposed_raw_input; $query += $view->exposed_raw_input;
} }
foreach ($columns as $field => $column) { foreach ($columns as $field => $column) {
// Create a second variable so we can easily find what fields we have and what the // Create a second variable so we can easily find what fields we have and
// CSS classes should be. // what the CSS classes should be.
$vars['fields'][$field] = drupal_clean_css_identifier($field); $vars['fields'][$field] = drupal_clean_css_identifier($field);
if ($active == $field) { if ($active == $field) {
$vars['fields'][$field] .= ' active'; $vars['fields'][$field] .= ' active';
@ -516,11 +524,10 @@ function template_preprocess_views_view_table(&$vars) {
$vars['header_classes'][$field]['class'][] = $options['info'][$field]['responsive']; $vars['header_classes'][$field]['class'][] = $options['info'][$field]['responsive'];
$responsive = TRUE; $responsive = TRUE;
} }
// Add a CSS align class to each field if one was set // Add a CSS align class to each field if one was set.
if (!empty($options['info'][$field]['align'])) { if (!empty($options['info'][$field]['align'])) {
$vars['header_classes'][$field]['class'][] = drupal_clean_css_identifier($options['info'][$field]['align']); $vars['header_classes'][$field]['class'][] = drupal_clean_css_identifier($options['info'][$field]['align']);
} }
// Add a header label wrapper if one was selected. // Add a header label wrapper if one was selected.
if ($vars['header'][$field]) { if ($vars['header'][$field]) {
$element_label_type = $fields[$field]->element_label_type(TRUE, TRUE); $element_label_type = $fields[$field]->element_label_type(TRUE, TRUE);
@ -528,17 +535,16 @@ function template_preprocess_views_view_table(&$vars) {
$vars['header'][$field] = '<' . $element_label_type . '>' . $vars['header'][$field] . '</' . $element_label_type . '>'; $vars['header'][$field] = '<' . $element_label_type . '>' . $vars['header'][$field] . '</' . $element_label_type . '>';
} }
} }
} }
// Add a CSS align class to each field if one was set // Add a CSS align class to each field if one was set.
if (!empty($options['info'][$field]['align'])) { if (!empty($options['info'][$field]['align'])) {
$vars['fields'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']); $vars['fields'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
} }
// Render each field into its appropriate column. // Render each field into its appropriate column.
foreach ($result as $num => $row) { foreach ($result as $num => $row) {
// Add field classes // Add field classes.
$vars['field_classes'][$field][$num] = array(); $vars['field_classes'][$field][$num] = array();
if ($fields[$field]->options['element_default_classes']) { if ($fields[$field]->options['element_default_classes']) {
$vars['field_classes'][$field][$num]['class'][] = 'views-field'; $vars['field_classes'][$field][$num]['class'][] = 'views-field';
@ -575,12 +581,12 @@ function template_preprocess_views_view_table(&$vars) {
else { else {
$vars['rows'][$num][$column] = ''; $vars['rows'][$num][$column] = '';
} }
$vars['rows'][$num][$column] .= $field_output; $vars['rows'][$num][$column] .= $field_output;
} }
} }
// Remove columns if the option is hide empty column is checked and the field is not empty. // Remove columns if the option is hide empty column is checked and the
// field is not empty.
if (!empty($options['info'][$field]['empty_column'])) { if (!empty($options['info'][$field]['empty_column'])) {
$empty = TRUE; $empty = TRUE;
foreach ($vars['rows'] as $num => $columns) { foreach ($vars['rows'] as $num => $columns) {
@ -639,7 +645,7 @@ function template_preprocess_views_view_table(&$vars) {
$vars['attributes']['class'][] = 'cols-' . count($vars['header']); $vars['attributes']['class'][] = 'cols-' . count($vars['header']);
if (!empty($handler->options['summary'])) { if (!empty($handler->options['summary'])) {
$vars['attributes_array'] = array('summary' => $handler->options['summary']); $vars['attributes']['summary'] = $handler->options['summary'];
} }
// If the table has headers and it should react responsively to columns hidden // If the table has headers and it should react responsively to columns hidden
// with the classes represented by the constants RESPONSIVE_PRIORITY_MEDIUM // with the classes represented by the constants RESPONSIVE_PRIORITY_MEDIUM