public function TableFilter::process in Bootstrap Utilities - Text editor filters 8
Performs the filter processing.
Parameters
string $text: The text string to be filtered.
string $langcode: The language code of the text to be filtered.
Return value
\Drupal\filter\FilterProcessResult The filtered text, wrapped in a FilterProcessResult object, and possibly with associated assets, cacheability metadata and placeholders.
Overrides FilterInterface::process
See also
\Drupal\filter\FilterProcessResult
File
- src/
Plugin/ Filter/ TableFilter.php, line 68
Class
- TableFilter
- Add Bootstrap class to any tables.
Namespace
Drupal\bootstrap_utilities\Plugin\FilterCode
public function process($text, $langcode) {
$result = new FilterProcessResult($text);
if (stristr($text, 'table') !== FALSE) {
$setting_classes = [];
$setting_classes[] = 'table';
if ($this->settings['table_row_striping']) {
$setting_classes[] = 'table-striped';
}
if ($this->settings['table_bordered']) {
$setting_classes[] = 'table-bordered';
}
if ($this->settings['table_row_hover']) {
$setting_classes[] = 'table-hover';
}
if ($this->settings['table_small']) {
$setting_classes[] = 'table-sm';
}
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
$table_elements = $xpath
->query('//table');
if (!is_null($table_elements)) {
foreach ($table_elements as $element) {
$existing_classes = [];
$with_setting_classes = [];
if ($element
->getAttribute('class')) {
$existing_classes[] = $element
->getAttribute('class');
}
$with_setting_classes = array_unique(array_merge($existing_classes, $setting_classes), SORT_REGULAR);
$all_classes = implode(' ', $with_setting_classes);
$element
->setAttribute('class', $all_classes);
}
}
if ($this->settings['table_remove_width_height']) {
$tbody_elements = $xpath
->query('//tbody');
if (!is_null($tbody_elements)) {
foreach ($tbody_elements as $element) {
$element
->removeAttribute('width');
$element
->removeAttribute('height');
}
}
}
$result
->setProcessedText(Html::serialize($dom));
}
return $result;
}