FilterResponsiveTable.php in Responsive Table Filter 8
File
src/Plugin/Filter/FilterResponsiveTable.php
View source
<?php
namespace Drupal\responsive_table_filter\Plugin\Filter;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Form\FormStateInterface;
class FilterResponsiveTable extends FilterBase {
public function settingsForm(array $form, FormStateInterface $form_state) {
$form['wrapper_element'] = [
'#type' => 'textfield',
'#required' => TRUE,
'#title' => $this
->t('Wrapper element'),
'#default_value' => $this->settings['wrapper_element'],
'#description' => $this
->t('The element to wrap the responsive table (e.g. figure)'),
];
$form['wrapper_classes'] = [
'#type' => 'textfield',
'#title' => $this
->t('Wrapper class(es)'),
'#default_value' => $this->settings['wrapper_classes'],
'#description' => $this
->t("Any wrapper class(es) separated by spaces (e.g. responsive-figure-table)"),
];
return $form;
}
public function process($text, $langcode) {
$result = new FilterProcessResult($text);
$text = preg_replace_callback('@<table([^>]*)>(.+?)</table>@s', [
$this,
'processTableCallback',
], $text);
$result
->setProcessedText($text);
return $result;
}
public function tips($long = FALSE, $context = []) {
return $this
->t('Wraps a %table tags with a %figure tag.', [
'%table' => '<table>',
'%figure' => '<' . $this
->getWrapperElement() . '>',
]);
}
private function processTableCallback(array $matches) {
$attributes = $matches[1];
$text = $matches[2];
$text = '<' . $this
->getWrapperElement() . $this
->getWrapperAttributes() . '><table' . $attributes . '>' . $text . '</table></' . $this
->getWrapperElement() . '>';
return $text;
}
private function getWrapperElement() {
return Xss::filter($this->settings['wrapper_element']);
}
private function getWrapperAttributes() {
return new Attribute([
'class' => [
$this->settings['wrapper_classes'],
],
]);
}
}