commit 3d62311476ab86a16130a2c68942f54229ec7267 Author: gnuget Date: Mon May 26 20:53:16 2014 -0500 adding a test and docs for the footer in tables diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 84e5235..cf2f1f2 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1463,6 +1463,33 @@ function drupal_pre_render_table(array $element) { * ) * ); * @endcode + * - footer: An array of table rows which will be printed within a + * tag. + * Every row is an array of cells, or an associative array with the + * following keys: + * - "data": an array of cells + * - Any HTML attributes, such as "class", to apply to the table row. + * - "no_striping": a boolean indicating that the row should receive no + * 'even / odd' styling. Defaults to TRUE. + * Each cell can be either a string or an associative array with the + * following keys: + * - "data": The string to display in the table cell. + * - "header": Indicates this cell is a header. + * - Any HTML attributes, such as "colspan", to apply to the table cell. + * Here's an example for $rows: + * @code + * $footer = array( + * // Simple row + * array( + * 'Cell 1', 'Cell 2', 'Cell 3' + * ), + * // Row with attributes on the row and some of its cells. + * array( + * 'data' => array('Cell 1', array('data' => 'Cell 2', 'colspan' => 2)), 'class' => array('funky') + * ) + * ); + * @endcode + * - attributes: An array of HTML attributes to apply to the table tag. * - caption: A localized string to use for the tag. * - colgroups: An array of column groups. Each element of the array can be @@ -1602,27 +1629,26 @@ function template_preprocess_table(&$variables) { } // Rows and footer have the same structure. - $table_parts = array('rows' , 'footer'); - foreach ($table_parts as $table_part) { - if (!empty($variables[$table_part])) { + $sections = array('rows' , 'footer'); + foreach ($sections as $section) { + if (!empty($variables[$section])) { $flip = array('even' => 'odd', 'odd' => 'even'); $class = 'even'; - foreach ($variables[$table_part] as $row_key => $row) { + foreach ($variables[$section] as $row_key => $row) { + $cells = $row; + $row_attributes = array(); + $no_striping = ($section === 'rows') ? FALSE : TRUE; + // 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; + $no_striping = isset($row['no_striping']) ? $row['no_striping'] : $no_striping; // Set the attributes array and exclude 'data' and 'no_striping'. $row_attributes = $row; unset($row_attributes['data']); unset($row_attributes['no_striping']); } - else { - $cells = $row; - $row_attributes = array(); - $no_striping = FALSE; - } // Add odd/even class. if (!$no_striping) { @@ -1631,9 +1657,9 @@ function template_preprocess_table(&$variables) { } // Build row. - $variables[$table_part][$row_key] = array(); - $variables[$table_part][$row_key]['attributes'] = new Attribute($row_attributes); - $variables[$table_part][$row_key]['cells'] = array(); + $variables[$section][$row_key] = array(); + $variables[$section][$row_key]['attributes'] = new Attribute($row_attributes); + $variables[$section][$row_key]['cells'] = array(); if (!empty($cells)) { foreach ($cells as $col_key => $cell) { if (!is_array($cell)) { @@ -1667,9 +1693,9 @@ function template_preprocess_table(&$variables) { $cell_attributes['class'][] = $responsive_classes[$col_key]; } - $variables[$table_part][$row_key]['cells'][$col_key]['tag'] = $is_header ? 'th' : 'td'; - $variables[$table_part][$row_key]['cells'][$col_key]['attributes'] = new Attribute($cell_attributes); - $variables[$table_part][$row_key]['cells'][$col_key]['content'] = $cell_content; + $variables[$section][$row_key]['cells'][$col_key]['tag'] = $is_header ? 'th' : 'td'; + $variables[$section][$row_key]['cells'][$col_key]['attributes'] = new Attribute($cell_attributes); + $variables[$section][$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 6b9c5ef..98d98b0 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Theme/TableTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/TableTest.php @@ -118,6 +118,28 @@ function testThemeTableWithNoStriping() { } /** + * Test that the 'footer' option works correctly. + */ + function testThemeTableFooter() { + $footer = array( + array( + 'data' => array(1), + ), + array('1'), + ); + + $table = array( + '#type' => 'table', + '#rows' => array(), + '#footer' => $footer, + ); + + $this->render($table); + $this->removeWhiteSpace(); + $this->assertRaw('11', 'Table footer found.'); + } + + /** * Tests that the 'header' option in cells works correctly. */ function testThemeTableHeaderCellOption() {