From 7604527a860532da0b9698d95d2a7fb635317967 Mon Sep 17 00:00:00 2001
From: Mark Carver <mark.carver@me.com>
Date: Fri, 26 Jul 2013 09:51:47 -0500
Subject: Issue #1968398 by Mark Carver, Les Lim, R.Hendel, joelpittet,
 brunodbo, intergalactic overlords: Convert Views $row_classes to
 $row['attributes']

---
 .../views/templates/views-view-list.html.twig      |  16 +--
 .../views-view-summary-unformatted.html.twig       |   7 +-
 .../views/templates/views-view-summary.html.twig   |   7 +-
 .../views/templates/views-view-table.html.twig     |  34 +++----
 .../templates/views-view-unformatted.html.twig     |   9 +-
 core/modules/views/views.theme.inc                 | 112 +++++++++++----------
 6 files changed, 93 insertions(+), 92 deletions(-)

diff --git a/core/modules/views/templates/views-view-list.html.twig b/core/modules/views/templates/views-view-list.html.twig
index 364ab2e..f07429c 100644
--- a/core/modules/views/templates/views-view-list.html.twig
+++ b/core/modules/views/templates/views-view-list.html.twig
@@ -4,8 +4,10 @@
  * Default theme implementation for a view template to display a list of rows.
  *
  * Available variables:
+ * - attributes: HTML attributes for the container.
  * - rows: A list of rows for this list.
- * - row_classes: The row's HTML attributes correlating with the row's 'id'.
+ *   - attributes: The row's HTML attributes.
+ *   - content: The row's contents.
  * - title: The title of this group of rows. May be empty.
  * - list: @todo.
  *   - type: Starting tag will be either a ul or ol.
@@ -16,8 +18,8 @@
  * @ingroup themeable
  */
 #}
-{% if wrapper_attributes -%}
-  <div{{ wrapper_attributes }}>
+{% if attributes -%}
+  <div{{ attributes }}>
 {% endif %}
   {% if title %}
     <h3>{{ title }}</h3>
@@ -29,15 +31,15 @@
     <ol{{ list.attributes }}>
   {% endif %}

-    {% for id, row in rows %}
-      <li{{ row_classes[id] }}>{{ row }}</li>
-    {% endfor %}
+  {% for row in rows %}
+    <li{{ row.attributes }}>{{ row.content }}</li>
+  {% endfor %}

   {% if list.type == 'ul' %}
     </ul>
   {% else %}
     </ol>
   {% endif %}
-{% if wrapper_attributes -%}
+{% if attributes -%}
   </div>
 {% endif %}
diff --git a/core/modules/views/templates/views-view-summary-unformatted.html.twig b/core/modules/views/templates/views-view-summary-unformatted.html.twig
index 25d48e3..9120dd4 100644
--- a/core/modules/views/templates/views-view-summary-unformatted.html.twig
+++ b/core/modules/views/templates/views-view-summary-unformatted.html.twig
@@ -8,8 +8,7 @@
  *   - url: The URL to this row's content.
  *   - count: The number of items this summary item represents.
  *   - separator: A separator between each row.
- * - row_classes: HTML attributes for a row, either containing an 'active' class
- *   or no attributes, that correlate to each row by ID.
+ *   - attributes: HTML attributes for a row.
  * - options: Flags indicating how each row should be displayed. This contains:
  *   - count: A flag indicating whether the row's 'count' should be displayed.
  *   - inline: A flag indicating whether the item should be wrapped in an inline
@@ -20,12 +19,12 @@
  * @ingroup themeable
  */
 #}
-{% for id, row in rows  %}
+{% for row in rows  %}
   {{ options.inline ? '<span' : '<div' }} class="views-summary views-summary-unformatted">
   {% if row.separator -%}
     {{ row.separator }}
   {%- endif %}
-  <a href="{{ row.url }}"{{ row_classes[id] }}>{{ row.link }}</a>
+  <a href="{{ row.url }}"{{ row.attributes }}>{{ row.link }}</a>
   {% if options.count %}
     ({{ row.count }})
   {% endif %}
diff --git a/core/modules/views/templates/views-view-summary.html.twig b/core/modules/views/templates/views-view-summary.html.twig
index 64f7826..4d8ed8f 100644
--- a/core/modules/views/templates/views-view-summary.html.twig
+++ b/core/modules/views/templates/views-view-summary.html.twig
@@ -9,8 +9,7 @@
  *   - url: The summary link URL.
  *   - link: The summary link text.
  *   - count: The number of items under this grouping.
- * - row_classes: HTML classes to apply to each row, indexed by row ID.
- *   This matches the index in rows.
+ *   - attributes: HTML attributes to apply to each row.
  * - options: Flags indicating how the summary should be displayed.
  *   This contains:
  *   - count: A flag indicating whether the count should be displayed.
@@ -22,8 +21,8 @@
 #}
 <div class="item-list">
   <ul class="views-summary">
-  {% for id, row in rows %}
-    <li><a href="{{ row.url }}"{{ row_classes[id] }}>{{ row.link }}</a>
+  {% for row in rows %}
+    <li><a href="{{ row.url }}"{{ row.attributes }}>{{ row.link }}</a>
       {% if options.count %}
         ({{ row.count }})
       {% endif %}
diff --git a/core/modules/views/templates/views-view-table.html.twig b/core/modules/views/templates/views-view-table.html.twig
index 2242c0b..6b0b26c 100644
--- a/core/modules/views/templates/views-view-table.html.twig
+++ b/core/modules/views/templates/views-view-table.html.twig
@@ -7,21 +7,19 @@
  * - 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
+ * - header: The table header columns.
+ *   - attributes: Remaining HTML attributes for the element.
+ *   - content: HTML classes to apply to each header cell, indexed by
  *   the header's key.
  * - caption_needed: Is the caption tag needed.
  * - caption: The caption for this table.
  * - accessibility_description: Extended description for the table details.
  * - accessibility_summary: Summary for the table details.
- * - 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.
+ * - rows: Table row items. Rows are keyed by row number.
+ *   - attributes: HTML classes to apply to each row.
+ *   - columns: Row column items. Columns are keyed by column number.
+ *     - attributes: HTML classes to apply to each column.
+ *     - content: The column content.
  *
  * @see template_preprocess_views_view_table()
  *
@@ -51,20 +49,20 @@
   {% if header %}
     <thead>
       <tr>
-        {% for key, field in header %}
-          <th{{ header_classes[key] }} scope="col">
-            {{ field }}
+        {% for column in header %}
+          <th{{ column.attributes }} scope="col">
+            {{ column.content }}
           </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 }}
+    {% for row in rows %}
+      <tr{{ row.attributes }}>
+        {% for column in row.columns %}
+          <td{{ column.attributes }}>
+            {{ column.content }}
           </td>
         {% endfor %}
       </tr>
diff --git a/core/modules/views/templates/views-view-unformatted.html.twig b/core/modules/views/templates/views-view-unformatted.html.twig
index e1c820f..e3fb2e3 100644
--- a/core/modules/views/templates/views-view-unformatted.html.twig
+++ b/core/modules/views/templates/views-view-unformatted.html.twig
@@ -6,7 +6,8 @@
  * Available variables:
  * - title: The title of this group of rows. May be empty.
  * - rows: A list of the view's row items.
- * - row_classes: A list of row class attributes keyed by the row's ID.
+ *   - attributes: The row's HTML attributes.
+ *   - content: The row's content.
  *
  * @see template_preprocess_views_view_unformatted()
  *
@@ -16,8 +17,8 @@
 {% if title %}
   <h3>{{ title }}</h3>
 {% endif %}
-{% for id, row in rows %}
-  <div{{ row_classes[id] }}>
-    {{ row }}
+{% for row in rows %}
+  <div{{ row.attributes }}>
+    {{ row.content }}
   </div>
 {% endfor %}
diff --git a/core/modules/views/views.theme.inc b/core/modules/views/views.theme.inc
index f88413d..43c1637 100644
--- a/core/modules/views/views.theme.inc
+++ b/core/modules/views/views.theme.inc
@@ -384,7 +384,6 @@ function template_preprocess_views_view_field(&$variables) {
 function template_preprocess_views_view_summary(&$variables) {
   $view = $variables['view'];
   $argument = $view->argument[$view->build_info['summary_level']];
-  $variables['row_classes'] = array();

   $url_options = array();

@@ -408,6 +407,7 @@ function template_preprocess_views_view_summary(&$variables) {
   $argument->processSummaryArguments($row_args);

   foreach ($variables['rows'] as $id => $row) {
+    $variables['rows'][$id]->attributes = array();
     $variables['rows'][$id]->link = $argument->summaryName($row);
     $args = $view->args;
     $args[$argument->position] = $row_args[$id];
@@ -418,11 +418,10 @@ function template_preprocess_views_view_summary(&$variables) {
     }
     $variables['rows'][$id]->url = url($view->getUrl($args, $base_path), $url_options);
     $variables['rows'][$id]->count = intval($row->{$argument->count_alias});
-    $variables['row_classes'][$id] = array();
     if (isset($active_urls[$variables['rows'][$id]->url])) {
-      $variables['row_classes'][$id]['class'][] = 'active';
+      $variables['rows'][$id]->attributes['class'][] = 'active';
     }
-    $variables['row_classes'][$id] = new Attribute($variables['row_classes'][$id]);
+    $variables['rows'][$id]->attributes = new Attribute($variables['rows'][$id]->attributes);
   }
 }

@@ -442,7 +441,6 @@ function template_preprocess_views_view_summary(&$variables) {
 function template_preprocess_views_view_summary_unformatted(&$variables) {
   $view = $variables['view'];
   $argument = $view->argument[$view->build_info['summary_level']];
-  $variables['row_classes'] = array();

   $url_options = array();

@@ -472,6 +470,7 @@ function template_preprocess_views_view_summary_unformatted(&$variables) {
     if ($count++) {
       $variables['rows'][$id]->separator = filter_xss_admin($variables['options']['separator']);
     }
+    $variables['rows'][$id]->attributes = array();
     $variables['rows'][$id]->link = $argument->summaryName($row);
     $args = $view->args;
     $args[$argument->position] = $row_args[$id];
@@ -482,11 +481,10 @@ function template_preprocess_views_view_summary_unformatted(&$variables) {
     }
     $variables['rows'][$id]->url = url($view->getUrl($args, $base_path), $url_options);
     $variables['rows'][$id]->count = intval($row->{$argument->count_alias});
-    $variables['row_classes'][$id] = array();
     if (isset($active_urls[$variables['rows'][$id]->url])) {
-      $variables['row_classes'][$id]['class'][] = 'active';
+      $variables['rows'][$id]->attributes['class'][] = 'active';
     }
-    $variables['row_classes'][$id] = new Attribute($variables['row_classes'][$id]);
+    $variables['rows'][$id]->attributes = new Attribute($variables['rows'][$id]->attributes);
   }
 }

@@ -511,7 +509,6 @@ function template_preprocess_views_view_table(&$variables) {
   // Store rows so that they may be used by further preprocess functions.
   $result = $variables['result'] = $variables['rows'];
   $variables['rows'] = array();
-  $variables['field_classes'] = array();
   $variables['header'] = array();

   $options = $view->style_plugin->options;
@@ -534,6 +531,8 @@ function template_preprocess_views_view_table(&$variables) {
     $query += $view->exposed_raw_input;
   }

+  // A boolean to store whether the table's header has any labels.
+  $has_header_labels = FALSE;
   foreach ($columns as $field => $column) {
     // Create a second variable so we can easily find what fields we have and
     // what the CSS classes should be.
@@ -542,11 +541,11 @@ function template_preprocess_views_view_table(&$variables) {
       $variables['fields'][$field] .= ' active';
     }

-    // render the header labels
+    // Render the header labels.
     if ($field == $column && empty($fields[$field]->options['exclude'])) {
       $label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : '');
       if (empty($options['info'][$field]['sortable']) || !$fields[$field]->clickSortable()) {
-        $variables['header'][$field] = $label;
+        $variables['header'][$field]['content'] = $label;
       }
       else {
         $initial = !empty($options['info'][$field]['default_sort_order']) ? $options['info'][$field]['default_sort_order'] : 'asc';
@@ -571,36 +570,41 @@ function template_preprocess_views_view_table(&$variables) {
           'attributes' => array('title' => $title),
           'query' => $query,
         );
-        $variables['header'][$field] = l($label, current_path(), $link_options);
+        $variables['header'][$field]['content'] = l($label, current_path(), $link_options);
       }

       // Set up the header label class.
-      $variables['header_classes'][$field] = array();
+      $variables['header'][$field]['attributes'] = array();
       if ($fields[$field]->options['element_default_classes']) {
-        $variables['header_classes'][$field]['class'][] = 'views-field';
-        $variables['header_classes'][$field]['class'][] = 'views-field-' . $variables['fields'][$field];
+        $variables['header'][$field]['attributes']['class'][] = 'views-field';
+        $variables['header'][$field]['attributes']['class'][] = 'views-field-' . $variables['fields'][$field];
       }
-      $variables['header_classes'][$field] = new Attribute($variables['header_classes'][$field]);
       $class = $fields[$field]->elementLabelClasses(0);
       if ($class) {
-        $variables['header_classes'][$field]['class'][] = $class;
+        $variables['header'][$field]['attributes']['class'][] = $class;
       }
       // Add responsive header classes.
       if (!empty($options['info'][$field]['responsive'])) {
-        $variables['header_classes'][$field]['class'][] = $options['info'][$field]['responsive'];
+        $variables['header'][$field]['attributes']['class'][] = $options['info'][$field]['responsive'];
         $responsive = TRUE;
       }
       // Add a CSS align class to each field if one was set.
       if (!empty($options['info'][$field]['align'])) {
-        $variables['header_classes'][$field]['class'][] = drupal_clean_css_identifier($options['info'][$field]['align']);
+        $variables['header'][$field]['attributes']['class'][] = drupal_clean_css_identifier($options['info'][$field]['align']);
       }
       // Add a header label wrapper if one was selected.
-      if ($variables['header'][$field]) {
+      if ($variables['header'][$field]['content']) {
         $element_label_type = $fields[$field]->elementLabelType(TRUE, TRUE);
         if ($element_label_type) {
-          $variables['header'][$field] = '<' . $element_label_type . '>' . $variables['header'][$field] . '</' . $element_label_type . '>';
+          $variables['header'][$field]['content'] = '<' . $element_label_type . '>' . $variables['header'][$field]['content'] . '</' . $element_label_type . '>';
         }
       }
+      // Check if header label is not empty.
+      if (!empty($variables['header'][$field]['content'])) {
+        $has_header_labels = TRUE;
+      }
+
+      $variables['header'][$field]['attributes'] = new Attribute($variables['header'][$field]['attributes']);
     }

     // Add a CSS align class to each field if one was set.
@@ -611,19 +615,18 @@ function template_preprocess_views_view_table(&$variables) {
     // Render each field into its appropriate column.
     foreach ($result as $num => $row) {
       // Add field classes.
-      $variables['field_classes'][$field][$num] = array();
+      $variables['rows'][$num]['columns'][$column]['attributes'] = array();
       if ($fields[$field]->options['element_default_classes']) {
-        $variables['field_classes'][$field][$num]['class'][] = 'views-field';
-        $variables['field_classes'][$field][$num]['class'][] = 'views-field-' . $variables['fields'][$field];
+        $variables['rows'][$num]['columns'][$column]['attributes']['class'][] = 'views-field';
+        $variables['rows'][$num]['columns'][$column]['attributes']['class'][] = 'views-field-' . $variables['fields'][$field];
       }
-      $variables['field_classes'][$field][$num] = new Attribute($variables['field_classes'][$field][$num]);

       if ($classes = $fields[$field]->elementClasses($num)) {
-        $variables['field_classes'][$field][$num]['class'][] = $classes;
+        $variables['rows'][$num]['columns'][$column]['attributes']['class'][] = $classes;
       }
       // Add responsive header classes.
       if (!empty($options['info'][$field]['responsive'])) {
-        $variables['field_classes'][$field][$num]['class'][] = $options['info'][$field]['responsive'];
+        $variables['rows'][$num]['columns'][$column]['attributes']['class'][] = $options['info'][$field]['responsive'];
       }

       if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
@@ -634,21 +637,22 @@ function template_preprocess_views_view_table(&$variables) {
         }

         // Don't bother with separators and stuff if the field does not show up.
-        if (empty($field_output) && !empty($variables['rows'][$num][$column])) {
+        if (empty($field_output) && !empty($variables['rows'][$num]['columns'][$column]['content'])) {
           continue;
         }

         // Place the field into the column, along with an optional separator.
-        if (!empty($variables['rows'][$num][$column])) {
+        if (!empty($variables['rows'][$num][$column]['content'])) {
           if (!empty($options['info'][$column]['separator'])) {
-            $variables['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
+            $variables['rows'][$num]['columns'][$column]['content'] .= filter_xss_admin($options['info'][$column]['separator']);
           }
         }
         else {
-          $variables['rows'][$num][$column] = '';
+          $variables['rows'][$num]['columns'][$column]['content'] = '';
         }
-        $variables['rows'][$num][$column] .= $field_output;
+        $variables['rows'][$num]['columns'][$column]['content'] .= $field_output;
       }
+      $variables['rows'][$num]['columns'][$column]['attributes'] = new Attribute($variables['rows'][$num]['columns'][$column]['attributes']);
     }

     // Remove columns if the option is hide empty column is checked and the
@@ -668,37 +672,36 @@ function template_preprocess_views_view_table(&$variables) {
   }

   // Hide table header if all labels are empty.
-  if (!array_filter($variables['header'])) {
+  if (!$has_header_labels) {
     $variables['header'] = array();
   }

   $count = 0;
-  $variables['row_classes'] = array();
   foreach ($variables['rows'] as $num => $row) {
-    $variables['row_classes'][$num] = array();
+    $variables['rows'][$num]['attributes'] = array();
     if ($row_class_special) {
-      $variables['row_classes'][$num]['class'][] = ($count++ % 2 == 0) ? 'odd' : 'even';
+      $variables['rows'][$num]['attributes']['class'][] = ($count++ % 2 == 0) ? 'odd' : 'even';
       if ($num === 0) {
-        $variables['row_classes'][$num]['class'][] = 'views-row-first';
+        $variables['rows'][$num]['attributes']['class'][] = 'views-row-first';
       }
       elseif ($num === (count($variables['rows']) - 1)) {
-        $variables['row_classes'][$num]['class'][] = 'views-row-last';
+        $variables['rows'][$num]['attributes']['class'][] = 'views-row-last';
       }
     }
     if ($row_class = $handler->getRowClass($num)) {
-      $variables['row_classes'][$num]['class'][] = $row_class;
+      $variables['rows'][$num]['attributes']['class'][] = $row_class;
     }
-    $variables['row_classes'][$num] = new Attribute($variables['row_classes'][$num]);
+    $variables['rows'][$num]['attributes'] = new Attribute($variables['rows'][$num]['attributes']);
   }

   $variables['attributes']['class'][] = 'views-table';
   $variables['attributes']['class'][] = 'views-view-table';
   if (empty($variables['rows']) && !empty($options['empty_table'])) {
     $build = $view->display_handler->renderArea('empty');
-    $variables['rows'][0][0] = drupal_render($build);
-    $variables['row_classes'][0] = new Attribute(array('class' => 'odd'));
+    $variables['rows'][0]['columns'][0]['content'] = drupal_render($build);
+    $variables['rows'][0]['attributes'] = new Attribute(array('class' => 'odd'));
     // Calculate the amounts of rows with output.
-    $variables['field_classes'][0][0] = new Attribute(array(
+    $variables['rows'][0]['columns'][0]['attributes'] = new Attribute(array(
       'colspan' => count($variables['header']),
       'class' => 'views-empty',
     ));
@@ -874,34 +877,33 @@ function template_preprocess_views_view_unformatted(&$variables) {
   $style = $view->style_plugin;
   $options = $style->options;

-  $variables['row_classes'] = array();
-  $variables['classes'] = array();
   $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : FALSE;
   $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : FALSE;
   // Set up striping values.
   $count = 0;
   $max = count($rows);
   foreach ($rows as $id => $row) {
-    $variables['row_classes'][$id] = array();
+    $variables['rows'][$id] = array();
+    $variables['rows'][$id]['content'] = $row;
+    $variables['rows'][$id]['attributes'] = array();
     $count++;
     if ($default_row_class) {
-      $variables['row_classes'][$id]['class'][] = 'views-row';
-      $variables['row_classes'][$id]['class'][] = 'views-row-' . $count;
+      $variables['rows'][$id]['attributes']['class'][] = 'views-row';
+      $variables['rows'][$id]['attributes']['class'][] = 'views-row-' . $count;
     }
     if ($row_class_special) {
-      $variables['row_classes'][$id]['class'][] = 'views-row-' . ($count % 2 ? 'odd' : 'even');
+      $variables['rows'][$id]['attributes']['class'][] = 'views-row-' . ($count % 2 ? 'odd' : 'even');
       if ($count == 1) {
-        $variables['row_classes'][$id]['class'][] = 'views-row-first';
+        $variables['rows'][$id]['attributes']['class'][] = 'views-row-first';
       }
       if ($count == $max) {
-        $variables['row_classes'][$id]['class'][] = 'views-row-last';
+        $variables['rows'][$id]['attributes']['class'][] = 'views-row-last';
       }
     }
-
     if ($row_class = $view->style_plugin->getRowClass($id)) {
-      $variables['row_classes'][$id]['class'][] = $row_class;
+      $variables['rows'][$id]['attributes']['class'][] = $row_class;
     }
-    $variables['row_classes'][$id] = new Attribute($variables['row_classes'][$id]);
+    $variables['rows'][$id]['attributes'] = new Attribute($variables['rows'][$id]['attributes']);
   }
 }

@@ -927,7 +929,7 @@ function template_preprocess_views_view_list(&$variables) {

   // Initialize a new attribute class for $wrapper_class.
   if ($wrapper_class) {
-    $variables['wrapper_attributes'] = new Attribute(array('class' => $wrapper_class));
+    $variables['attributes']['class'] = $wrapper_class;
   }

   // Initialize a new attribute class for $class.
--
1.8.2

