Div tags stripped from editor

Silverstripe Version: 4.11

I am not sure what is going on but

tags are being stripped whenever I click on Publish. I have tried with and without the following code:
HtmlEditorConfig::get('cms')
    ->addButtonsToLine(1, 'styleselect')
    ->setOptions([
        'importcss_append' => true,
        'style_formats' => $formats,
		'extended_valid_elements' => 'div[class|id|style]',
		'block_formats' => 'Paragraph=p;Div=div;Heading 1=h1;Heading 2=h2;Heading 3=h3;Heading 4=h4;Heading 5=h5;Heading 6=h6;Preformatted=pre'
    ]);

This is making it very difficult to insert shortcodes as they get wrapepd in <p> tags instead of div

Also media, e.g. youtube as only the placeholder is inserted rather than the surrounding div tag.

Has anyone experienced this problem and found a solution?

Thanks in advance.

I haven’t used HtmlEditorConfig, but I do use TinyMCEConfig like this and it allows the insertion of div tags:

TinyMCEConfig::get('cms')
    ->setOptions([
        'block_formats' => "Paragraph=p;Heading 2=h2;Heading 3=h3;Heading 4=h4;Div=div",
]);

Hope that’s helpful.

Chris

Actually…After I replied to your question I was curious to figure something similar like this out since I’ve had a need for it before and this is what I came up with :

$formats = [
    [
        'title'             => 'Style 1',
        'selector'          => 'div',
        'classes'           => 'style1',
        'wrapper'           => true,
        'merge_siblings'    => false,
    ]
];


TinyMCEConfig::get('cms')
    ->addButtonsToLine(1, 'styleselect')
    ->setOptions([
        'block_formats'     => "Paragraph=p;Heading 2=h2;Heading 3=h3;Heading 4=h4;Div=div",
        'importcss_append'  => true,
        'style_formats'     => $formats
]);

Thanks Chris.

I have actually already tried both options, TinyMCEConfig & HtmlEditorConfig, but no luck. I have now realised that a fresh install of SS does not display this problem so I now have to isolate all modules, yml files and my own code to see which one is the cause.

Aaargh! I found it. I use the axllent/silverstripe-cms-tweaks module. I didn’t realise it had this in it:

        HtmlEditorConfig::get('cms')->setOptions(array(
            /* Strip out <div> tags */
            'invalid_elements' => 'div',
            /* The "span[!class]" is to address the issue where lists get inline css style.
            See and http://martinsikora.com/how-to-make-tinymce-to-output-clean-html */
            'extended_valid_elements' => 'span[!class|!style],p[class|style]'
        ));

All good now.