I want to put a div around tables in general and give them the class table-responsible. Therefore I changed the table.html.twig in my theme like this:

<div {{attributes.addClass('table-responsive') }}>
  <table{{ attributes.addClass('table') }}>

But if I create a table in CKEditor nothing happend. It's only an ordinary table and nothing changed.

What did I do wrong?


CKEditor generates the table on its own. You can see this, if you switch to source. So the changes in twig will have no effekt, because the theme layer doesn't touch the source code that is coming out of the ckeditor. Only the filter layer is checking the source. More for security reasons. But it would be possible to change the table here, if you want to do it in drupal. Or you can modify the configuration of ckeditor itself.

  • Thanks, for your informations. I didn't solve the problem yet, but made a lot of tests. What is a table.html.twig good for, if I can't use an editor? Or is it only the ckeditor? Does it work in other editors?
    – lesley n.
    Jan 7 '16 at 14:45
  • See here table.html.twig - CKeditor does not use this twig, it has its own table template and how to configure this is not a drupal topic. If you want to do this in drupal, your best chance is a text filter. It is easy to implement this yourself.
    – 4k4
    Jan 7 '16 at 16:58
  • I try to change the text filter but it did not work. I add <div class> <table class> and <div><table></table></div> to ALLOWED HTML TAGS but I can not put a div around table. I can put it around <h1> or around text in general. So can you give me an example for the syntax, please?
    – lesley n.
    Jan 14 '16 at 14:01

To solve this you can implement a custom filter to surround the table with the needed div.

In Drupal 8 you can write a filter this way: https://www.lullabot.com/articles/creating-a-custom-filter-in-drupal-8

Or use hook_filter_info() for Drupal 7: https://api.drupal.org/api/drupal/modules%21filter%21filter.api.php/function/hook_filter_info/7.x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.