diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 37f93de..7ffb0d5 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -2080,48 +2080,23 @@ function template_preprocess_table(&$variables) { // Table header. if (!empty($variables['header']) && is_array($variables['header'])) { - $variables['header'] = array_values($variables['header']); + $variables['header'] = array_values($variables['header']); } - $headers = $variables['header']; - // Empty message. - // Set to new variable for convenient checks later. $empty_message = (isset($variables['empty'])) ? $variables['empty'] : ''; + // Remove 'empty' from template variables and set message to its own variable // to make sure we don't conflict with Twig's reserved keyword of the same // name. unset($variables['empty']); - // @todo API change, rename this variable from 'empty' to 'empty_message'. - $variables['empty_message'] = $empty_message; - - // Preprocess header. - 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 JS. - // This is needed to target tables constructed by this function. - $variables['attributes']['class'][] = 'sticky-enabled'; - } - // If the table has headers and it should react responsively to columns - // hidden with the classes represented by the constants - // RESPONSIVE_PRIORITY_MEDIUM and RESPONSIVE_PRIORITY_LOW, add the - // tableresponsive behaviors. - if ($is_responsive) { - drupal_add_library('system', 'drupal.tableresponsive'); - // Add 'responsive-enabled' class to the table to identify it for JS. - // This is needed to target tables constructed by this function. - $variables['attributes']['class'][] = 'responsive-enabled'; - } - } + $variables['empty_message'] = $empty_message; - // Preprocess colgroups. if (!empty($variables['colgroups'])) { foreach ($variables['colgroups'] as $colgroup_key => $colgroup) { $colgroup_attributes = new Attribute(array()); - // Support simple or complex colgroups. + // Support simple and complex colgroups. if (isset($colgroup['data'])) { foreach ($colgroup as $key => $value) { if ($key == 'data') { @@ -2136,10 +2111,9 @@ function template_preprocess_table(&$variables) { $cols = $colgroup; } - // Set colgroup attributes. $variables['colgroups'][$colgroup_key]['attributes'] = $colgroup_attributes; - // Set cols. + // Build colgroups and assign attributes if (is_array($cols) && !empty($cols)) { $variables['colgroups'][$colgroup_key]['cols'] = array(); foreach ($cols as $col_key => $col) { @@ -2150,9 +2124,8 @@ function template_preprocess_table(&$variables) { } } - // Preprocess Empty message. // Add one row spanning all columns for the 'empty' message if there are no - // data rows and the empty message is set. + // data rows and the empty message is not empty. if (empty($variables['rows']) && !empty($empty_message)) { $header_count = 0; foreach ($variables['header'] as $header_cell) { @@ -2164,9 +2137,6 @@ function template_preprocess_table(&$variables) { } } // Add one row for the empty message spanning all columns. - // @todo Leave rows empty, let template decide how to handle no rows, using - // empty_message variable and optionally counting the columns to generate - // an "empty" row spanning all columns like this. $variables['rows'][] = array(array( 'data' => $empty_message, 'attributes' => new Attribute(array( @@ -2176,17 +2146,30 @@ function template_preprocess_table(&$variables) { )); } - // Build up an associative array of responsive classes keyed by column. + // Build an associative array of responsive classes keyed by column. $responsive_classes = array(); // Preprocess table header. 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 the cell is not an array, make it one. if (!is_array($cell)) { $cell = array( @@ -2195,7 +2178,6 @@ function template_preprocess_table(&$variables) { ); } - // Create cell attributes. if (!isset($cell['attributes'])) { $cell['attributes'] = array(); } @@ -2213,8 +2195,6 @@ function template_preprocess_table(&$variables) { } } - // @todo Find out why we can use $variables['header'] here but must use - // $headers for tablesort_cell() below. $cell = tablesort_header($cell, $variables['header'], $ts); // Move cell properties that are not data or attributes into attributes. @@ -2226,15 +2206,12 @@ function template_preprocess_table(&$variables) { } } - // Conversion to Attribute() happens last. $cell['attributes'] = new Attribute($cell['attributes']); // Flag the cell as a header. $cell['header'] = TRUE; // Update the header cell with attributes and properties. - // @todo Make sure we're not overwriting or losing any header element - // variables. $variables['header'][$col_key] = $cell; } } @@ -2242,7 +2219,6 @@ function template_preprocess_table(&$variables) { $ts = array(); } - // Preprocess table rows. if (!empty($variables['rows'])) { // Add odd/even class. @@ -2270,20 +2246,19 @@ function template_preprocess_table(&$variables) { $no_striping = FALSE; } - // Add odd/even class + // Add odd/even class. if (!$no_striping) { $class = $flip[$class]; $row_attributes['class'][] = $class; } + // Build row. if (!empty($cells)) { - // Build row. $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) { - // If the cell is not an array, make it one. if (!is_array($cell)) { $cell = array( @@ -2292,15 +2267,12 @@ function template_preprocess_table(&$variables) { ); } - // Create cell attributes. if (!isset($cell['attributes'])) { $cell['attributes'] = array(); } // Add active class if needed for sortable tables. - // @todo Find out why we can't use $variables['header'] instead of - // $headers here while we can in tablesort_header() above. - $cell = tablesort_cell($cell, $headers, $ts, $i); + $cell = tablesort_cell($cell, $variables['header'], $ts, $i); // Move cell properties that are not data, attributes or header property // into attributes. @@ -2316,7 +2288,6 @@ function template_preprocess_table(&$variables) { $cell['attributes']['class'][] = $responsive_classes[$i]; } - // Conversion to Attribute() happens last. $cell['attributes'] = new Attribute($cell['attributes']); // Make sure that zeros get printed as '0'. 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 995e93a..a235849 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Theme/TableTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/TableTest.php @@ -83,7 +83,7 @@ function testThemeTableWithNoStriping() { } /** - * Tests that the 'header' option in cells works correctly + * Tests that the 'header' option in cells works correctly. */ function testThemeTableHeaderCellOption() { $rows = array(