function table_altrow_filter in Table Alternate Rows 6
Same name and namespace in other branches
- 5 table_altrow.module \table_altrow_filter()
Implementation of hook_filter
File
- ./
table_altrow.module, line 26
Code
function table_altrow_filter($op, $delta = 0, $format = -1, $text = '') {
// The "list" operation provides the module an opportunity to declare both how
// many filters it defines and a human-readable name for each filter. Note that
// the returned name should be passed through t() for translation.
if ($op == 'list') {
return array(
0 => t('Table alternate row filter'),
);
}
// All operations besides "list" provide a $delta argument so we know which
// filter they refer to. We'll switch on that argument now so that we can
// discuss each filter in turn.
switch ($delta) {
// First we define the simple string substitution filter.
case 0:
switch ($op) {
// This description is shown in the administrative interface, unlike the
// filter tips which are shown in the content editing interface.
case 'description':
return t('Adds required attributes to an HTML tag.');
// We don't need the "prepare" operation for this filter, but it's required
// to at least return the input text as-is.
case 'prepare':
return $text;
// The actual filtering is performed here. The supplied text should be
// returned, once any necessary substitutions have taken place.
case 'process':
// 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;
}
// Don't replace existing classes. Perhaps this should append a class instead?
if (!strstr($matches[2][0], 'class=')) {
if ($count % 2 == 0) {
$new_tag = '<tr class="even"' . $matches[2][0] . '>';
$text = str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
}
else {
$new_tag = '<tr class="odd"' . $matches[2][0] . '>';
$text = str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
}
}
$count++;
}
}
return $text;
}
break;
}
}