silverstripe / silverstripe-admin

Silverstripe Admin Component
BSD 3-Clause "New" or "Revised" License
25 stars 91 forks source link

Multiple TinyMCE wysiwyg configs share the same config #1726

Closed emteknetnz closed 2 months ago

emteknetnz commented 2 months ago

When using two HTMLEditorFields with different configs, they will both use the same config as the last one that was set

You can test this by using the code below and attempting to insert a table via the table button in the wysiwyg editor, or by adding bullet points

I feel like I've come across the before and the issue was that only a single TinyMCE.dynamic.js style file is dynamically generated

<?php

use SilverStripe\Assets\File;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
use SilverStripe\Forms\TextareaField;
use SilverStripe\LinkField\Form\LinkField;
use SilverStripe\LinkField\Form\MultiLinkField;
use SilverStripe\LinkField\Models\Link;

class Page extends SiteTree
{
    private static $db = [
        'SomeContent' => 'HTMLText'
    ];

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        # doesn't allow any tags except p,ul,li
        $h = $fields->dataFieldByName('Content');
        $c = $h->getEditorConfig();
        $c->setOption('valid_elements', "@[id|class|style|title],a[id|rel|rev|dir|tabindex|accesskey|type|name|href|target|title"
        . "|class],-strong/-b[class],-em/-i[class],-strike[class],-u[class],#p[id|dir|class|align|style],-ol[class],"
        . "-ul[class],-li[class],br,img[id|dir|longdesc|usemap|class|src|border|alt=|title|width|height|align|data*],"
        . "-sub[class],-sup[class],-blockquote[dir|class],-cite[dir|class|id|title],"
        . "-table[cellspacing|cellpadding|width|height|class|align|summary|dir|id|style],"
        . "-tr[id|dir|class|rowspan|width|height|align|valign|bgcolor|background|bordercolor|style],"
        . "tbody[id|class|style],thead[id|class|style],tfoot[id|class|style],"
        . "#td[id|dir|class|colspan|rowspan|width|height|align|valign|scope|style],"
        . "-th[id|dir|class|colspan|rowspan|width|height|align|valign|scope|style],caption[id|dir|class],"
        . "-div[id|dir|class|align|style],-span[class|align|style],-pre[class|align],address[class|align],"
        . "-h1[id|dir|class|align|style],-h2[id|dir|class|align|style],-h3[id|dir|class|align|style],"
        . "-h4[id|dir|class|align|style],-h5[id|dir|class|align|style],-h6[id|dir|class|align|style],hr[class],"
        . "dd[id|class|title|dir],dl[id|class|title|dir],dt[id|class|title|dir]");
        $h->setEditorConfig($c);

        # works correctly
        $h2 = HTMLEditorField::create('SomeContent');
        $c2 = $h2->getEditorConfig();
        $c2->setOption('valid_elements', 'p,ul,li');
        $h2->setEditorConfig($c2);

        $fields->addFieldsToTab('Root.Main', [$h2]);
        return $fields;
    }
}
GuySartorelli commented 2 months ago

The $c and $h set are irrelevant - you can reproduce this with just the $c2 and $h2 set.

The problem is that you're just modifying the config that is pre-defined for that field. That means you're actually modifying the configuration instead of creating a new one, and that configuration is modified for all fields that use it.

This change fixes the above scenario:

+ use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
 // ....
- $c2 = $h2->getEditorConfig();
+ $c2 = HTMLEditorConfig::get('a-new-config');

Working as expected. Closing.