Skip to content

Instantly share code, notes, and snippets.

@clrockwell
Created July 11, 2016 16:57
Show Gist options
  • Save clrockwell/bba02177bd5a5e490dd7ab4150948911 to your computer and use it in GitHub Desktop.
Save clrockwell/bba02177bd5a5e490dd7ab4150948911 to your computer and use it in GitHub Desktop.
/**
* Use
*
* $header['row1'][] => ['data' => 'header-1-1', 'colspan' => 2];
* $header['row1'][] => ['data' => 'header-1-2', 'colspan' => 2];
* $header['row2'] => ['Total', 'Avg', 'Total', 'Avg'];
* // add in some colgroups for targeted styling
* $colgroups = array_fill(0, 2, ['attributes' => ['span' => 2]]);
* $rows = array_fill(0, 4, '10');
*
* return theme('multi_head_table', ['header' => $header, 'rows' => $rows, 'colgroups' => $colgroups]);
/**
* Implements hook_theme().
*/
function module_theme($existing, $type, $theme, $path) {
$items['multi_head_table'] = [
'variables' => [
'header' => [],
'rows' => [],
'attributes' => [],
'caption' => NULL,
'colgroups' => [],
'sticky' => NULL,
'empty' => NULL,
],
];
return $items;
}
/**
* Theme a table with multiple headers
*/
function theme_multi_head_table($variables) {
$header = $variables['header'];
$multi_header = FALSE;
if (!empty($header) && isset($header['row2'])) {
$multi_header = TRUE;
}
$rows = $variables['rows'];
$attributes = $variables['attributes'];
$caption = $variables['caption'];
$colgroups = $variables['colgroups'];
$sticky = $variables['sticky'];
$empty = $variables['empty'];
// Add sticky headers, if applicable.
if (count($header) && $sticky) {
drupal_add_js('misc/tableheader.js');
// Add 'sticky-enabled' class to the table to identify it for JS.
// This is needed to target tables constructed by this function.
$attributes['class'][] = 'sticky-enabled';
}
$output = '<table' . drupal_attributes($attributes) . ">\n";
if (isset($caption)) {
$output .= '<caption>' . $caption . "</caption>\n";
}
// Format the table columns:
if (count($colgroups)) {
foreach ($colgroups as $number => $colgroup) {
$attributes = array();
// Check if we're dealing with a simple or complex column
if (isset($colgroup['data'])) {
foreach ($colgroup as $key => $value) {
if ($key == 'data') {
$cols = $value;
}
else {
$attributes[$key] = $value;
}
}
}
else {
$cols = $colgroup;
}
// Build colgroup
if (is_array($cols) && count($cols)) {
$output .= ' <colgroup' . drupal_attributes($attributes) . '>';
$i = 0;
foreach ($cols as $col) {
$output .= ' <col' . drupal_attributes($col) . ' />';
}
$output .= " </colgroup>\n";
}
else {
$output .= ' <colgroup' . drupal_attributes($attributes) . " />\n";
}
}
}
// Add the 'empty' row message if available.
if (!count($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++;
}
}
$rows[] = array(array('data' => $empty, 'colspan' => $header_count, 'class' => array('empty', 'message')));
}
// Format the table header:
if (count($header)) {
$ts = tablesort_init($header);
// HTML requires that the thead tag has tr tags in it followed by tbody
// tags. Using ternary operator to check and see if we have any rows.
if ($multi_header) {
foreach ($header as $header_row) {
$output .= (count($rows) ? ' <thead><tr>' : ' <tr>');
foreach ($header_row as $cell) {
$cell = tablesort_header($cell, $header_row, $ts);
$output .= _theme_table_cell($cell, TRUE);
}
$output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
}
} else {
$output .= (count($rows) ? ' <thead><tr>' : ' <tr>');
foreach ($header as $cell) {
$cell = tablesort_header($cell, $header, $ts);
$output .= _theme_table_cell($cell, TRUE);
}
$output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
}
}
else {
$ts = array();
}
// Format the table rows:
if (count($rows)) {
$output .= "<tbody>\n";
$flip = array('even' => 'odd', 'odd' => 'even');
$class = 'even';
foreach ($rows as $number => $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;
// Set the attributes array and exclude 'data' and 'no_striping'.
$attributes = $row;
unset($attributes['data']);
unset($attributes['no_striping']);
}
else {
$cells = $row;
$attributes = array();
$no_striping = FALSE;
}
if (count($cells)) {
// Add odd/even class
if (!$no_striping) {
$class = $flip[$class];
$attributes['class'][] = $class;
}
// Build row
$output .= ' <tr' . drupal_attributes($attributes) . '>';
$i = 0;
foreach ($cells as $cell) {
$cell = tablesort_cell($cell, $header, $ts, $i++);
$output .= _theme_table_cell($cell);
}
$output .= " </tr>\n";
}
}
$output .= "</tbody>\n";
}
$output .= "</table>\n";
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment