Skip to content

Instantly share code, notes, and snippets.

@adrexia
Last active July 15, 2020 22:27
Show Gist options
  • Save adrexia/2531ca06b04c7b2f08a02ad0352131f8 to your computer and use it in GitHub Desktop.
Save adrexia/2531ca06b04c7b2f08a02ad0352131f8 to your computer and use it in GitHub Desktop.
Set up various TinyMCE Editors
<?php
use App\Config\EditorConfig;
EditorConfig::setAllEditors();
<?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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment