Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@benclark
Created July 19, 2012 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benclark/3145218 to your computer and use it in GitHub Desktop.
Save benclark/3145218 to your computer and use it in GitHub Desktop.
Render the node teaser field in a row below the other fields in a table view
<?php
/**
* Implementation of theme_preprocess_views_view_table().
*/
function demcctheme_preprocess_views_view_table(&$vars) {
$view = &$vars['view'];
switch ($view->name) {
case 'og_topics':
if ($view->current_display == 'block_1') {
// Render the node teaser field in a row *below* the other fields.
// This code inserts new rows into the $vars['rows'] array, and
// adjusts the $vars['row_classes'] array to match. It also creates
// a new $vars['colspans'] array that is used in the
// views_view_table.tpl.php override to handle the colspans.
$rows = &$vars['rows'];
$result = &$vars['result'];
$row_classes = &$vars['row_classes'];
// Calculate the colspan value of a row with only one column.
$colspan = count($vars['header']);
// Add the node_teaser CSS class here since it was excluded in the view.
$vars['fields']['node_teaser'] = 'node-teaser';
// Create a new array to store the colspan value.
// Indexed by row #, so it only outputs colspan for the teaser
field/row.
$vars['colspans'] = array();
// $j is the offset created by the new rows being inserted.
$j = 0;
// Since the count of $rows changes after each iteration, cache
// the count() value here to prevent infinite loops.
$rows_static_count = count($rows);
for ($i = 0; $i < $rows_static_count; $i++) {
$teaser = $result[$i]->node_revisions_teaser;
if (empty($teaser)) {
$teaser = t('(no content)');
}
// Create the row to be inserted.
$teaser_row[0] = array(
'node_teaser' => check_markup(
$teaser,
$result[$i]->node_revisions_format
),
);
// Create the row class.
$teaser_row_class[0] = array(
$row_classes[$i+$j][0], // 'even' or 'odd' striping class.
'demcctheme-colspan-all', // to help manipulate the borders, etc.
);
// Added to help with themeing.
$row_classes[$i+$j][] = 'demcctheme-not-colspan-all';
// Each insert adds a row to the original $rows array, so while we're
// iterating on the original $rows count, we need to keep track of the
// number of new rows we've added. the +1 is to refer to the new row,
// not the "parent" row with the other fields.
$i_offset = $i+$j+1;
array_splice($rows, $i_offset, 0, $teaser_row);
array_splice($row_classes, $i_offset, 0, $teaser_row_class);
$vars['colspans'][$i_offset] = $colspan;
// Increment the offset. Put here for readability.
$j++;
}
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment