From: Richard Burford
Date: Sun, 7 Oct 2012 02:57:03 +0000 (+0100)
Subject: Converted template_preprocess_table to the new Attribute system and added checks... 
X-Git-Url: http://drupalcode.org/sandbox/pixelmord/1750250.git/commitdiff_plain/728ff19d3908b868a3fe2a1cce92cd6a359a91fd?hp=c64567f79212ce1463c35a58022b99692744cd3b

Converted template_preprocess_table to the new Attribute system and added checks to see whether $header and $rows are empty or not.
---

diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index 3f18c16..af0f87e 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -9,6 +9,7 @@
  */
 
 use Drupal\Core\Utility\ThemeRegistry;
+use Drupal\Core\Template\Attribute;
 
 /**
  * @defgroup content_flags Content markers
@@ -2886,37 +2887,46 @@ function template_preprocess_table(&$variables) {
   $empty = $variables['empty'];
 
   // Add the 'empty' row message if available.
-  if (!count($rows) && $empty) {
+  if (empty($rows) && $empty) {
     $header_count = 0;
-    foreach ($header as $header_cell) {
-      if (is_array($header_cell)) {
-        $header_count += isset($header_cell['colspan']) ? $header_cell['colspan'] : 1;
-      }
-      else {
-        $header_count++;
+    if (!empty($header)) {
+      foreach ($header as $header_cell) {
+        if (is_array($header_cell)) {
+          $header_count += isset($header_cell['colspan']) ? $header_cell['colspan'] : 1;
+        }
+        else {
+          $header_count++;
+        }
       }
     }
+
     $rows[] = array(array('data' => $empty, 'colspan' => $header_count, 'class' => array('empty', 'message')));
   }
 
   // Generate the table headers.
-  foreach ($header as $cell) {
-    $variables['header']['cells'][] = _theme_table_cell($cell, TRUE);
+  if (!empty($header)) {
+    foreach ($header as $cell) {
+      $variables['header']['cells'][] = _theme_table_cell($cell, TRUE);
+    }
   }
 
-  // Generate the table rows.
-  foreach ($rows as $row_number => $row) {
-    if (!empty($row['data'])) {
-      foreach ($row['data'] as $cell) {
-        $variables['rows'][$row_number]['cells'][] = _theme_table_cell($cell);
+  if (!empty($rows)) {
+    // Generate the table rows.
+    foreach ($rows as $row_number => $row) {
+      if (!empty($row['data'])) {
+        foreach ($row['data'] as $cell) {
+          $variables['rows'][$row_number]['cells'][] = _theme_table_cell($cell);
+        }
       }
-    }
-    else {
-      foreach ($row as $cell) {
-        $variables['rows'][$row_number]['cells'][] = _theme_table_cell($cell);
+      else {
+        foreach ($row as $cell) {
+          $variables['rows'][$row_number]['cells'][] = _theme_table_cell($cell);
+        }
       }
     }
   }
+
+  $variables['attributes'] = new Attribute($variables['attributes']);
 }
 
 /**
diff --git a/core/themes/stark/templates/theme.inc/table.twig b/core/themes/stark/templates/theme.inc/table.twig
index cf35126..3534b3b 100644
--- a/core/themes/stark/templates/theme.inc/table.twig
+++ b/core/themes/stark/templates/theme.inc/table.twig
@@ -36,7 +36,7 @@
  * @ingroup themeable
  */
 #}
-<table {{ attributes }}>
+<table{{ attributes }}>
   {% if caption %}
     <caption>{{ caption }}</caption>
   {% endif %}
@@ -44,13 +44,13 @@
   {# Build the colgroup element. #}
   {% for cols in colgroup %}
     {% if cols|length %}
-      <colgroup {{ colgroup.attributes }}>
+      <colgroup{{ colgroup.attributes }}>
       {% for col in cols %}
         <col {{ col.attributes }}/>
       {% endfor %}
       </colgroup>
     {% else %}
-      <colgroup {{ colgroup.attributes }}/>
+      <colgroup{{ colgroup.attributes }}/>
     {% endif %}
   {% endfor %}
 
@@ -68,7 +68,7 @@
     <tbody>
     {% for row in rows %}
       {% if row.cells|length %}
-        <tr {{ row.attributes }}>
+        <tr{{ row.attributes }}>
         {% for cell in row.cells %}
           {{ cell }}
         {% endfor %}
