table_altrow.module in Table Alternate Rows 7
Same filename and directory in other branches
Insert even and odd classes for tables via input filters to allow for proper zebra-style striping.
File
table_altrow.moduleView source
<?php
/**
* @file
* Insert even and odd classes for tables via input filters to allow for proper
* zebra-style striping.
*/
/**
* Implements hook_filter_info().
*/
function table_altrow_filter_info() {
$filters = array();
$filters['table_altrow'] = array(
'title' => t('Add even and odd classes to table rows'),
'process callback' => '_table_altrow_process',
'tips callback' => 'table_altrow_tips',
);
return $filters;
}
/**
* Filter process callback.
*/
function _table_altrow_process($text, $filter) {
// The actual filtering is performed here. The supplied text should be
// returned, once any necessary substitutions have taken place.
// First, we have to parse the variable.
$matches = array();
$offset = 0;
// Find a tbody.
while (preg_match('!(<tbody ?.*>)!i', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
$offset = $matches[0][1];
$count = 1;
// While the tbody is still open.
while (preg_match('!(<tr( ?.*)>)|(</tbody>)!i', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
// +1 so we don't match the same string.
$offset = $matches[0][1] + 1;
// Don't process tr's until we find a tbody.
if ($matches[0][0] == '</tbody>') {
break;
}
$attributes = substr($matches[2][0], 0, strpos($matches[2][0], '>'));
$found_class = preg_match('/\\bclass\\b="([\\- a-z0-9]*?)"/u', $attributes, $class_body);
if ($count % 2 == 0) {
$class = 'even';
}
else {
$class = 'odd';
}
if ($found_class) {
if (strstr($class_body[1], 'even') === FALSE || strstr($class_body[1], 'odd') === FALSE) {
$class .= " {$class_body[1]}";
}
else {
$class = $class_body[1];
}
}
$new_tag = '<tr class="' . $class . '"' . $matches[2][0] . '>';
$text = table_altrow_str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
$count++;
}
}
return $text;
}
/**
* Filter tips callback.
*/
function _table_altrow_tips($filter, $format, $long = FALSE) {
if ($long) {
return t('Tables will be rendered with different styles for even and odd rows if supported.');
}
}
/**
* Replace every instance of a string with a count parameter like PHP5.
* This can probably be removed with Drupal goes to PHP5 only.
* Shamelessly stolen and modified from
* http://ca.php.net/manual/en/function.str-replace.php#76180
*
* @param $needle
* The string to search for.
* @param $replace
* The string to replace.
* @param $haystack
* The text to search within.
* @param $offset
* Optional parameter to indicate the character position to start the search
* and replace at.
* @param integer $count
* Optional parameter to indicate the number of times to execute replacements.
*
* @return
* The modified string.
*/
function table_altrow_str_replace_count($needle, $replace, $haystack, $offset = NULL, $count = NULL) {
if ($count == null) {
$count = 0;
$offset = strpos($haystack, $needle);
}
$rpl_count = 0;
while ($offset !== false && $rpl_count < $count) {
$haystack = substr_replace($haystack, $replace, $offset, strlen($needle));
$offset += strlen($replace);
$offset = strpos($haystack, $needle, $offset);
$rpl_count++;
}
return $haystack;
}
Functions
Name | Description |
---|---|
table_altrow_filter_info | Implements hook_filter_info(). |
table_altrow_str_replace_count | Replace every instance of a string with a count parameter like PHP5. This can probably be removed with Drupal goes to PHP5 only. Shamelessly stolen and modified from http://ca.php.net/manual/en/function.str-replace.php#76180 |
_table_altrow_process | Filter process callback. |
_table_altrow_tips | Filter tips callback. |