adrexia of DNA
3/5/2020 - 3:34 AM

Set up various TinyMCE Editors

Set up various TinyMCE Editors

<?php

namespace App\Config;

use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;

class EditorConfig
{
    public static function setAllEditors()
    {
        self::setCMSEditor();
        self::setSimpleEditor();
        self::setBasicEditor();
    }
    /**
     * Sets the main editor configuration. Starts with cms, and adjusts to
     * remove unwanted elements or add things cms is missing
     */
    public static function setCMSEditor()
    {
        $cmsEditor = TinyMCEConfig::get('cms')->setOptions([
            "block_formats" => 'Paragraph=p;Header 1=h2;Header 2=h3;Header 3=h4;Header 4=h5;Header 5=h6;Preformatted=pre; Address=address',
            'extended_valid_elements' =>
                "img[class|src|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|usemap|data*]," . "iframe[src|name|width|height|align|frameborder|marginwidth|marginheight|scrolling],"
                . "object[width|height|data|type],"
                . 'embed[width|height|name|flashvars|src|bgcolor|align|play|loop|quality|allowscriptaccess|type|pluginspage|autoplay],'
                . "param[name|value],"
                . "map[class|name|id],"
                . "area[shape|coords|href|target|alt]",
            'browser_spellcheck' => true
        ]);

        $cmsEditor->removeButtons('underline', 'alignleft', 'alignright', 'alignjustify', 'aligncenter', 'template', 'anchor');
        $cmsEditor->enablePlugins('hr', 'charmap', 'anchor', 'wordcount', 'lists');
        $cmsEditor->insertButtonsAfter('removeformat', '|', 'superscript', 'subscript', 'blockquote', 'hr');
        $cmsEditor->addButtonsToLine(1, '|', 'template');
        $cmsEditor->insertButtonsAfter('unlink', 'anchor', '|');
    }

    /**
     * Sets up a simple editor. For areas of the site where we need to limit the type of html that appears
     * i.e. no images, tables, templates etc
     * Can be used by specifying the final parameter in the field constructor. eg:
     *
     * HTMLEditorField::create('Content', 'Content',  $this->Content, 'simple')
     */
    public static function setSimpleEditor()
    {
        $simple = TinyMCEConfig::get('simple');
        $simple->setOptions([
            'skin' => 'silverstripe',
            "block_formats" => 'Paragraph=p;Header 2=h3;Header 3=h4;Header 4=h5;',
        ]);

        $cmsModule = ModuleLoader::inst()->getManifest()->getModule('silverstripe/cms');
        $assetAdminModule = ModuleLoader::inst()->getManifest()->getModule('silverstripe/asset-admin');
        $adminModule = ModuleLoader::inst()->getManifest()->getModule('silverstripe/admin');
        $simple->enablePlugins([
            'sslink' => $adminModule->getResource('client/dist/js/TinyMCE_sslink.js'),
            'sslinkexternal' => $adminModule->getResource('client/dist/js/TinyMCE_sslink-external.js'),
            'sslinkemail' => $adminModule->getResource('client/dist/js/TinyMCE_sslink-email.js'),
            'sslinkinternal' => $cmsModule->getResource('client/dist/js/TinyMCE_sslink-internal.js'),
            'sslinkanchor' => $cmsModule->getResource('client/dist/js/TinyMCE_sslink-anchor.js'),
            'sslinkfile' => $assetAdminModule->getResource('client/dist/js/TinyMCE_sslink-file.js'),
        ]);

        $simple->removeButtons('underline', 'alignleft', 'alignright', 'alignjustify', 'aligncenter', 'code', 'paste', 'pastetext', 'template', 'anchor', 'hr', 'blockquote', 'table', 'indent', 'outdent', 'sslink', 'formatselect');
        $simple->enablePlugins('charmap', 'anchor', 'wordcount', 'lists');
        $simple->insertButtonsAfter('removeformat', '|', 'superscript', 'subscript');
        $simple->insertButtonsAfter('numlist', '|', 'unlink', 'sslink', 'anchor', '|', 'formatselect', '|', 'paste', 'pastetext', 'code');
        $simple->setButtonsForLine(2, ''); // no buttons
    }

    /**
     * Sets up a basic editor. Like the simple editor but even fewer options
     * Can be used by specifying the final parameter in the field constructor. eg:
     *
     * HTMLEditorField::create('Content', 'Content',  $this->Content, 'basic')
     */
    public static function setBasicEditor()
    {
        $basic = TinyMCEConfig::get('basic');
        $basic->setOptions([
            'skin' => 'silverstripe'
        ]);

        $cmsModule = ModuleLoader::inst()->getManifest()->getModule('silverstripe/cms');
        $assetAdminModule = ModuleLoader::inst()->getManifest()->getModule('silverstripe/asset-admin');
        $adminModule = ModuleLoader::inst()->getManifest()->getModule('silverstripe/admin');
        $basic->enablePlugins([
            'sslink' => $adminModule->getResource('client/dist/js/TinyMCE_sslink.js'),
            'sslinkexternal' => $adminModule->getResource('client/dist/js/TinyMCE_sslink-external.js'),
            'sslinkemail' => $adminModule->getResource('client/dist/js/TinyMCE_sslink-email.js'),
            'sslinkinternal' => $cmsModule->getResource('client/dist/js/TinyMCE_sslink-internal.js'),
            'sslinkanchor' => $cmsModule->getResource('client/dist/js/TinyMCE_sslink-anchor.js'),
            'sslinkfile' => $assetAdminModule->getResource('client/dist/js/TinyMCE_sslink-file.js'),
        ]);
        $basic->removeButtons('underline', 'alignleft', 'numlist', 'bullist', 'alignright', 'alignjustify', 'aligncenter', 'code', 'paste', 'pastetext', 'template', 'anchor', 'hr', 'blockquote', 'table', 'indent', 'outdent', 'sslink', 'formatselect');
        $basic->enablePlugins('charmap', 'anchor', 'wordcount');
        $basic->insertButtonsAfter('removeformat', '|', 'superscript', 'subscript', '|', 'unlink', 'sslink', 'anchor', '|', 'paste', 'pastetext', 'code');
        $basic->setButtonsForLine(2, ''); // no buttons
    }
}
<?php

use App\Config\EditorConfig;

EditorConfig::setAllEditors();