diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 2f41684..b1f4d36 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1498,20 +1498,15 @@ function drupal_pre_render_table(array $element) { * rows. */ function template_preprocess_table(&$variables) { - // Flags. $is_sticky = !empty($variables['sticky']); $is_responsive = !empty($variables['responsive']); - // Table header. - if (!empty($variables['header']) && is_array($variables['header'])) { - $variables['header'] = array_values($variables['header']); - } - + // Format the table columns: if (!empty($variables['colgroups'])) { - foreach ($variables['colgroups'] as $colgroup_key => $colgroup) { + foreach ($variables['colgroups'] as $colgroup_key => &$colgroup) { $colgroup_attributes = new Attribute(); - // Support simple and complex colgroups. + // Check if we're dealing with a simple or complex column if (isset($colgroup['data'])) { foreach ($colgroup as $key => $value) { if ($key == 'data') { @@ -1525,70 +1520,49 @@ function template_preprocess_table(&$variables) { else { $cols = $colgroup; } - - $variables['colgroups'][$colgroup_key]['attributes'] = $colgroup_attributes; + $colgroup = array(); + $colgroup['attributes'] = $colgroup_attributes; // Build colgroups and assign attributes if (is_array($cols) && !empty($cols)) { - $variables['colgroups'][$colgroup_key]['cols'] = array(); + $colgroup['cols'] = array(); foreach ($cols as $col_key => $col) { - $col_attributes = new Attribute($col); - $variables['colgroups'][$colgroup_key]['cols'][$col_key]['attributes'] = $col_attributes; + $colgroup['cols'][$col_key]['attributes'] = new Attribute($col); } } } } - // Add one row spanning all columns for the 'empty' message if there are no - // data rows and the empty message is set. + // 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)) { - $header_count += isset($header_cell['colspan']) ? $header_cell['colspan'] : 1; + if (is_array($header_cell) && isset($header_cell['colspan'])) { + $header_count += $header_cell['colspan']; } else { $header_count++; } } - // Add one row for the empty message spanning all columns. $variables['rows'][] = array(array( 'data' => $variables['empty'], - 'attributes' => new Attribute(array( - 'colspan' => $header_count, - 'class' => array('empty', 'message'), - )) + 'colspan' => $header_count, + 'class' => array('empty', 'message'), )); } // Build an associative array of responsive classes keyed by column. $responsive_classes = array(); - // Preprocess table header. + // Format the table header: + $ts = array(); if (!empty($variables['header'])) { - - // Add sticky headers, if applicable. - if ($is_sticky) { - drupal_add_library('system', 'drupal.tableheader'); - // Add 'sticky-enabled' class to the table to identify it for JavaScript. - $variables['attributes']['class'][] = 'sticky-enabled'; - } - - // If the table is responsive, set a class and add the necessary library. - if ($is_responsive) { - drupal_add_library('system', 'drupal.tableresponsive'); - // Add 'responsive-enabled' class to the table to identify it - // for JavaScript. - $variables['attributes']['class'][] = 'responsive-enabled'; - } - - // Initialize table sorting. $ts = tablesort_init($variables['header']); foreach ($variables['header'] as $col_key => $cell) { if (!is_array($cell)) { $cell_content = $cell; - $cell_attributes = ''; + $cell_attributes = new Attribute(); $is_header = TRUE; } else { @@ -1618,29 +1592,23 @@ function template_preprocess_table(&$variables) { $cell_content = drupal_render($cell_content); } - tablesort_header($cell_content, $cell, $header, $ts); + tablesort_header($cell_content, $cell, $variables['header'], $ts); // tablesort_header() removes the 'sort' and 'field' keys. $cell_attributes = new Attribute($cell); } - + $variables['header'][$col_key] = array(); $variables['header'][$col_key]['tag'] = $is_header ? 'th' : 'td'; $variables['header'][$col_key]['attributes'] = $cell_attributes; - $variables['header'][$col_key]['data'] = $cell_content; + $variables['header'][$col_key]['content'] = $cell_content; } } - else { - $ts = array(); - } if (!empty($variables['rows'])) { - $flip = array( - 'even' => 'odd', - 'odd' => 'even', - ); + $flip = array('even' => 'odd', 'odd' => 'even'); $class = 'even'; foreach ($variables['rows'] as $row_key => $row) { - // Check if we're dealing with a simple or complex row. + // 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'] : FALSE; @@ -1663,12 +1631,11 @@ function template_preprocess_table(&$variables) { } // Build row. + $variables['rows'][$row_key] = array(); + $variables['rows'][$row_key]['attributes'] = new Attribute($row_attributes); + $variables['rows'][$row_key]['cells'] = array(); if (!empty($cells)) { - $variables['rows'][$row_key] = array(); - $variables['rows'][$row_key]['attributes'] = new Attribute($row_attributes); - $variables['rows'][$row_key]['cells'] = array(); - $i = 0; - foreach ($cells as $cell) { + foreach ($cells as $col_key => $cell) { if (!is_array($cell)) { $cell_content = $cell; $cell_attributes = array(); @@ -1691,19 +1658,18 @@ function template_preprocess_table(&$variables) { } } // Add active class if needed for sortable tables. - if (isset($variables['header'][$i]['data']) && $variables['header'][$i]['data'] == $ts['name'] && !empty($variables['header'][$i]['field'])) { + if (isset($variables['header'][$col_key]['data']) && $variables['header'][$col_key]['data'] == $ts['name'] && !empty($variables['header'][$col_key]['field'])) { $cell_attributes['class'][] = 'active'; } // Copy RESPONSIVE_PRIORITY_LOW/RESPONSIVE_PRIORITY_MEDIUM // class from header to cell as needed. - if (isset($responsive_classes[$i])) { - $cell['attributes']['class'][] = $responsive_classes[$i]; + if (isset($responsive_classes[$col_key])) { + $cell_attributes['class'][] = $responsive_classes[$col_key]; } - $variables['rows'][$row_key]['cells'][$i]['tag'] = $is_header ? 'th' : 'td'; - $variables['rows'][$row_key]['cells'][$i]['attributes'] = new Attribute($cell_attributes); - $variables['rows'][$row_key]['cells'][$i]['data'] = $cell_content; - $i++; + $variables['rows'][$row_key]['cells'][$col_key]['tag'] = $is_header ? 'th' : 'td'; + $variables['rows'][$row_key]['cells'][$col_key]['attributes'] = new Attribute($cell_attributes); + $variables['rows'][$row_key]['cells'][$col_key]['content'] = $cell_content; } } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/TableTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/TableTest.php index df1541f..f9b8631 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Theme/TableTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/TableTest.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Theme; +use Drupal\Component\Utility\String; use Drupal\simpletest\DrupalUnitTestBase; /** @@ -41,10 +42,10 @@ function testThemeTableStickyHeaders() { '#rows' => $rows, '#sticky' => TRUE, ); - $this->content = drupal_render($table); + $this->render($table); $js = _drupal_add_js(); - $this->assertTrue(isset($js['core/misc/tableheader.js']), 'tableheader.js was included when $sticky = TRUE.'); - $this->assertRaw('sticky-enabled', 'Table has a class of sticky-enabled when $sticky = TRUE.'); + $this->assertTrue(isset($js['core/misc/tableheader.js']), 'tableheader.js found.'); + $this->assertRaw('sticky-enabled'); drupal_static_reset('_drupal_add_js'); } @@ -66,10 +67,10 @@ function testThemeTableNoStickyHeaders() { '#colgroups' => $colgroups, '#sticky' => FALSE, ); - $this->content = drupal_render($table); + $this->render($table); $js = _drupal_add_js(); - $this->assertFalse(isset($js['core/misc/tableheader.js']), 'tableheader.js was not included because $sticky = FALSE.'); - $this->assertNoRaw('sticky-enabled', 'Table does not have a class of sticky-enabled because $sticky = FALSE.'); + $this->assertFalse(isset($js['core/misc/tableheader.js']), 'tableheader.js not found.'); + $this->assertNoRaw('sticky-enabled'); drupal_static_reset('_drupal_add_js'); } @@ -79,9 +80,9 @@ function testThemeTableNoStickyHeaders() { */ function testThemeTableWithEmptyMessage() { $header = array( - t('Header 1'), + 'Header 1', array( - 'data' => t('Header 2'), + 'data' => 'Header 2', 'colspan' => 2, ), ); @@ -89,11 +90,12 @@ function testThemeTableWithEmptyMessage() { '#type' => 'table', '#header' => $header, '#rows' => array(), - '#empty' => t('No strings available.'), + '#empty' => 'Empty row.', ); - $this->content = drupal_render($table); - $this->assertTrue($this->xpath('//tr[@class="odd"]/td[@colspan="3" and @class="empty message" and text()=:text]', array(':text' => t('No strings available.'))), 'Correct colspan was set on empty message.'); - $this->assertTrue($this->xpath('//thead/tr/th[text()=:text]', array(':text' => t('Header 1'))), 'Table header was printed.'); + $this->render($table); + $this->removeWhiteSpace(); + $this->assertRaw('Header 1Header 2', 'Table header found.'); + $this->assertRaw('Empty row.', 'Colspan on #empty row found.'); } /** @@ -110,7 +112,7 @@ function testThemeTableWithNoStriping() { '#type' => 'table', '#rows' => $rows, ); - $this->content = drupal_render($table); + $this->render($table); $this->assertNoRaw('class="odd"', 'Odd/even classes were not added because $no_striping = TRUE.'); $this->assertNoRaw('no_striping', 'No invalid no_striping HTML attribute was printed.'); } @@ -130,11 +132,32 @@ function testThemeTableHeaderCellOption() { '#type' => 'table', '#rows' => $rows, ); - $this->content = drupal_render($table); + $this->render($table); $this->assertRaw('111', 'The th and td tags was printed correctly.'); } /** + * Renders a given render array. + * + * @param array $elements + * The render array elements to render. + * + * @return string + * The rendered HTML. + */ + protected function render(array $elements) { + $this->content = drupal_render($elements); + $this->verbose('
' . String::checkPlain($this->content));
+  }
+
+  /**
+   * Removes all white-space between HTML tags from $this->content.
+   */
+  protected function removeWhiteSpace() {
+    $this->content = preg_replace('@>\s+<@', '><', $this->content);
+  }
+
+  /**
    * Asserts that a raw string appears in $this->content.
    *
    * @param string $value
diff --git a/core/modules/system/templates/table.html.twig b/core/modules/system/templates/table.html.twig
index 0135898..b2161b5 100644
--- a/core/modules/system/templates/table.html.twig
+++ b/core/modules/system/templates/table.html.twig
@@ -12,8 +12,9 @@
  *     http://drupal.org/node/893530 and
  *     http://api.drupal.org/api/drupal/includes!theme.inc/function/theme_table/7#comment-5109.
  * - header: Table header cells. Each cell contains the following properties:
- *   - attributes: HTML attributes to apply to the  tag.
- *   - data: A localized string for the title of the column.
+ *   - tag: The HTML tag name to use; either TH or TD.
+ *   - attributes: HTML attributes to apply to the tag.
+ *   - content: A localized string for the title of the column.
  *   - field: Field name (required for column sorting).
  *   - sort: Default sort order for this column ("asc" or "desc").
  * - sticky: A flag indicating whether to use a "sticky" table header.
@@ -22,11 +23,11 @@
  *   - data: Table cells.
  *   - no_striping: A flag indicating that the row should receive no
  *     'even / odd' styling. Defaults to FALSE.
- *     A cell can be either a string or may contain the following keys:
- *     - data: The string to display in the table cell.
- *     - header: Indicates this cell is a header.
+ *   - cells: Table cells of the row. Each cell contains the following keys:
+ *     - tag: The HTML tag name to use; either TH or TD.
  *     - attributes: Any HTML attributes, such as "colspan", to apply to the
  *       table cell.
+ *     - content: The string to display in the table cell.
  * - empty: The message to display in an extra row if table does not have
  *   any rows.
  *
@@ -36,50 +37,49 @@
  */
 #}
 
-  {# Caption #}
   {% if caption %}
     {{ caption }}
   {% endif %}
-
-  {# Columns #}
-  {% for colgroup in colgroups %}
+  {% for colgroup in colgroups -%}
     {% if colgroup.cols %}
-      
-        {% for col in colgroup.cols %}
-          
-        {% endfor %}
-      
+    
+      {% for col in colgroup.cols -%}
+      
+      {%- endfor %}
+
+    
     {% else %}
-      
+    
     {% endif %}
-  {% endfor %}
+  {%- endfor %}
+  {%- if header %}
 
-  {# Header #}
-  {% if header %}
     
       
         {% for cell in header -%}
-          
-            {{- cell.data -}}
-          
+        <{{ cell.tag }}{{ cell.attributes }}>
+          {{- cell.content -}}
+        
         {%- endfor %}
+
       
     
   {% endif %}
+  {%- if rows %}
 
-  {# Rows #}
-  {% if rows %}
     
-      {% for row in rows %}
-        
-          {% for cell in row.cells -%}
-            {%- set cell_tag = cell.header ? 'th' : 'td' -%}
-            <{{ cell_tag }}{{ cell.attributes }}>
-              {{- cell.data -}}
-            
-          {%- endfor %}
-        
+      {%- for row in rows %}
+
+      
+        {% for cell in row.cells -%}
+        <{{ cell.tag }}{{ cell.attributes }}>
+          {{- cell.content -}}
+        
+        {%- endfor %}
+
+      
       {% endfor %}
+
     
   {% endif %}