SilverStripe TInyMCE configuration requires a refresh to take effect

496 views Asked by At

We have built out our own version of the tinyMCE editor in SilverStripe. The only issue is that you need to hit refresh for our custom configuration to be loaded. Once it has been refreshed once, it sticks for the rest of the session.

Our set up is as follows:

BolierplateWYSIWYG.php

class BolierplateWYSIWYG extends Extension {

    protected function defaults() {
        $defaultEditorConfig = HtmlEditorConfig::get('cms');
        $defaultEditorConfig->setOptions(
            array(
                'theme'                        => 'advanced',
                'priority'                     => 1,
                // More config options
            )
        );

        return HtmlEditorConfig::get('cms');
    }

    public function getConfig() {
        return $this->defaults();
    }
}

Then, inside of Page.php we have the following:

... page functions ...

public function getCMSFields() {

    $fields = parent::getCMSFields();

    // Update WYSIWYG
    $digital360Wysiwyg = new Digital360WYSIWYG;
    $digital360Wysiwyg->getConfig();

    ... Page CMS configuration ...

Inside of our boilplate.yml we have:

HtmlEditorField:
  extensions:
    - BolierplateWYSIWYG

How do I get this new configuration to load without requiring a page refresh?

3

There are 3 answers

3
3dgoo On

You can customise your HtmlEditorField by calling setOptions in your _mysite/config.php:

HtmlEditorConfig::get('cms')->setOptions(
    array(
        'theme'                        => 'advanced',
        'priority'                     => 1,
        // More config options
    )
);

This will work without the need to refresh a CMS page.

3
assertchris On
0
colymba On

Like @assertchris mention, my PR https://github.com/silverstripe/silverstripe-framework/pull/4259/files has now been merge so you can easily have multiple TinyMCE configs which should help you with you extension.

Setup your HTMLEditorConfig in _config.php like

HtmlEditorConfig::get('default')->setOptions....
HtmlEditorConfig::get('fancy')->setOptions....

Since you have to have an extension, you could have something like:

class BolierplateWYSIWYG extends Extension {
  public function setEditorConfig($name = 'default')
  {
    HtmlEditorConfig::set_active($name);
  }
}

The you can use it like this when setting up your CMS fields

$digital360Wysiwyg = new Digital360WYSIWYG;
$digital360Wysiwyg->setEditorConfig();

or

$digital360Wysiwyg = new Digital360WYSIWYG;
$digital360Wysiwyg->setEditorConfig('fancy');

This should work fine. Although be careful when changing some of the editor options like mode, as this can cause your refresh issue. an you shouldn't have to change theme or priority?