0

I've created a Drupal 8 module that adds a custom plugin to the core's CKEditor. This plugin allows users to add tabbed content to their pages.

The problem is that the plugin adds a class to the element it creates and CKEditor by default removes classes from most elements once you save the node.

I can change CKEditor config for Basic HTML editor (admin/config/content/formats/manage/basic_html) to allow classes in elements by changing to in "Allowed HTML tags" input.

Now I would love to be able to do this programmatically somehow. How can I change the configuration for CKEditor in a Drupal module? Especially the "extraAllowedContent" config value.

The project for reference purposes can be found at https://github.com/j-vee/ckeditor_tabber

  • The only thing that I can think of would be to find the DB Table that contains those settings values and modify it with db_merge – No Sssweat Jun 18 '16 at 22:14
  • Thanks! That got a me a bit closer. I found the settings for all filters in the config table. The problem is that each editor has a settings row there so for example filter.format.basic_html has one. But might be that the user has changed / deleted that Basic HTML format so I wouldn't know for sure which row to update. Might be better just to leave it up to the users to update the Allowed HTML tags setting themselves – Juhani Jun 21 '16 at 22:40
0

I had a similar case, i had to add a few attributes to the basic_html format in the Allowed HTML tags so it could work with the module editor_advance_link which allows the users to add title, class, id and target attributes to the a tag when using the core's CKEditor.

So this is what i did with some code i found here ( https://www.drupal.org/files/issues/allow_image_style_to_be-2061377-224.patch)

if ($filter = FilterFormat::load('basic_html')) {
    if (($filter_html = $filter->filters('filter_html')) && $filter_html->status) {
      $config = $filter_html->getConfiguration();
      $allowed_html = !empty($config['settings']['allowed_html']) ? $config['settings']['allowed_html'] : NULL;
      $matches = [];
      if ($allowed_html && preg_match('/<a ([^>]*)>/', $allowed_html, $matches)) {
        $new_attributes = array_filter(explode(' ', $matches[1]));
        $new_attributes[] = 'target';
        $new_attributes[] = 'title';
        $new_attributes[] = 'class';
        $config['settings']['allowed_html'] = preg_replace('/<a ([^>]*)>/', '<a ' . implode(' ', array_unique($new_attributes)) . '>', $allowed_html);
        $filter->setFilterConfig('filter_html', $config);
        $filter->save();
      }
    }
  }

Don't forget to include: use Drupal\filter\Entity\FilterFormat;

This code is needed to be run only once, so i had to include it in a .install file in a custom module.

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.