<tableclass="table-responsive"> <caption>Tableau de test (Ceci est un tableau de test)</caption> <thead> <tr> <thscope="col"></th> <thscope="col">reprehenderit</th> <thscope="col">dolor</th> <thscope="col">culpa</th>
......................
Подскажите есть ли способ - в тег таблицы добавлять по умолчанию класс class="table-responsive", но только в добавленные при помощи CKEditor
Drupal\filter\FilterProcessResult; use Drupal\filter\Plugin\FilterBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Component\Utility\Html;
/** * Provides a filter to help celebrate good times! * * @Filter( * id = "table_class", * title = @Translation("Table Class responsive"), * description = @Translation("Filter to automatically add responsive class for table"), * type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE, * ) */ class TableClass extends FilterBase {
/** * {@inheritdoc} */ public function process($text, $langcode) { // Create DOM document. $dom = Html::load($text);
// Get all UL tags. $lists_query = '//table';
// Get DOM nodes from XPath query. $xpath = new \DOMXPath($dom); $nodes = $xpath->query($lists_query); // Whether we have results. if ($nodes->length > 0) { // Iterate over list elements and add our class. foreach ($nodes as $node) { $classes = explode(' ', $node->getAttribute('class')); $classes[] = 'table-responsive'; $node->setAttribute('class', join(' ', array_unique($classes))); } }
Комментарии
Можно. Надо писать плагин для CKeditor
А что если немного упростить логику:
Таблицы не
добавленные с помощью CKEditor
а
присутствующие в поле body (возможно определенных типов материалов)
Тогда можно JS скриптом добавить класс этим таблицам.
Так и сделал
Кому понадобится вот код:
<?php
/**
// Get all UL tags.
// Get DOM nodes from XPath query.
/**
* @file
* Contains Drupal\tableclass\Plugin\Filter\TableClass
*/ namespace Drupal\tableclass\Plugin\Filter;
use
Drupal\filter\FilterProcessResult;use Drupal\filter\Plugin\FilterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html; /**
* Provides a filter to help celebrate good times!
*
* @Filter(
* id = "table_class",
* title = @Translation("Table Class responsive"),
* description = @Translation("Filter to automatically add responsive class for table"),
* type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE,
* )
*/
class TableClass extends FilterBase {
* {@inheritdoc}
*/
public function process($text, $langcode) {
// Create DOM document.
$dom = Html::load($text);
$lists_query = '//table';
$xpath = new \DOMXPath($dom);
$nodes = $xpath->query($lists_query);
// Whether we have results.
if ($nodes->length > 0) {
// Iterate over list elements and add our class.
foreach ($nodes as $node) {
$classes = explode(' ', $node->getAttribute('class'));
$classes[] = 'table-responsive';
$node->setAttribute('class', join(' ', array_unique($classes)));
}
}
return new
FilterProcessResult(Html::serialize($dom));}
}?>