TableAltrow.php in Table Alternate Rows 8
File
src/Plugin/Filter/TableAltrow.php
View source
<?php
namespace Drupal\table_altrow\Plugin\Filter;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
class TableAltrow extends FilterBase {
public function process($text, $langcode) {
$matches = [];
$offset = 0;
while (preg_match('!(<tbody ?.*>)!i', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
$offset = $matches[0][1];
$count = 1;
while (preg_match('!(<tr( ?.*)>)|(</tbody>)!i', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
$offset = $matches[0][1] + 1;
if ($matches[0][0] == '</tbody>') {
break;
}
if (!strstr($matches[2][0], 'class=')) {
if ($count % 2 == 0) {
$new_tag = '<tr class="even"' . $matches[2][0] . '>';
$text = table_altrow_str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
}
else {
$new_tag = '<tr class="odd"' . $matches[2][0] . '>';
$text = table_altrow_str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
}
}
$count++;
}
}
return new FilterProcessResult($text);
}
public function tips($long = FALSE) {
if ($long) {
return $this
->t('Tables will be rendered with different styles for even and odd rows if supported.');
}
}
}
Classes
Name |
Description |
TableAltrow |
Insert even and odd classes for tables via input filters to allow for proper
zebra-style striping. |