0

By default Drupal provides a form under text formats to allow some html tags, then it will disallow anything not in this allow list. I would like to do the opposite of this. Allow all html tags then disallow just a few tags.

I am using this custom hook to disallow html tags

mymodule_editor_js_settings_alter

then I am placing the tags I want to disallow like all h1 class names

$settings['editor']['formats']['rich_text']['editorSettings']['disallowedContent'] = 'h1(*)';

But I have to supply a list of allowed html tags in the drupal GUI under text formats. So I need to allow h1 id and styles. What I want to do is to allow all h1 tags by example and then disallow 1 thing. I read this is possible here under the section "How to Allow Everything Except…" https://ckeditor.com/docs/ckeditor4/latest/guide/dev_disallowed_content.html#how-to-allow-everything-except This page offers an example array. I tried to add this to my hook but I get a syntax error.

I tried this and it seems to be working. In the hook below I am allowing h1 and h2 and any attribute, style and classnames, but then I disallow the h1 class name of "myHeader2"

use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;

/**
 * Implements hook_form_alter().
 */
function blank_moda_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id == 'node_page_form') {

  }
}

function mymodule_editor_js_settings_alter(array &$settings) {
  foreach ($settings['editor']['formats'] as $name => $value) {

    $settings['editor']['formats']['rich_text']['editorSettings']['allowedContent'] = array(
      '$1' => array(
        'elements' => array('h1'=> TRUE, 'h2'=> TRUE),
        'attributes' => TRUE,
        'styles' => TRUE,
        'classes' => TRUE,
      ),
    );

    $settings['editor']['formats']['rich_text']['editorSettings']['disallowedContent'] = 'h1(myHeader2)';
  }
}

Now I need to prepare a full list of all the allowed elements I want. Then I can start disallowing specific attributes, styles and class names.

  • I found that this hook does work, but it does not work in addition to the Allowed html tags in the GUI in Drupal /admin/config/content/formats/manage/rich_text?destination=/admin/config/content/formats. I had to copy all of the elements from the GUI over to the hook. – paulcap1 Aug 28 '18 at 11:20
  • I have found that It is only working in node edit form and the ckeditor is in edit mode. Wen I preview the published page it allows the tags in my disallow custom hook. – paulcap1 Aug 28 '18 at 16:05

Your Answer

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

Browse other questions tagged or ask your own question.