diff --git a/src/Handling/SpellCheckMiddleware.php b/src/Handling/SpellCheckMiddleware.php index 00532c9..4ab5ed0 100644 --- a/src/Handling/SpellCheckMiddleware.php +++ b/src/Handling/SpellCheckMiddleware.php @@ -7,6 +7,7 @@ use SilverStripe\Control\Middleware\HTTPMiddleware; use SilverStripe\Core\Config\Configurable; use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig; +use SilverStripe\Forms\HTMLEditor\TinyMCEConfig; use SilverStripe\i18n\i18n; use SilverStripe\Security\SecurityToken; @@ -25,17 +26,25 @@ class SpellCheckMiddleware implements HTTPMiddleware public function process(HTTPRequest $request, callable $delegate) { // Set settings - $editor = static::config()->get('editor'); - HTMLEditorConfig::get($editor)->enablePlugins('spellchecker'); - HTMLEditorConfig::get($editor)->addButtonsToLine(2, 'spellchecker'); + $editor = $this->config()->get('editor'); + + /** @var TinyMCEConfig $editorConfig */ + $editorConfig = TinyMCEConfig::get($editor); + + $editorConfig->enablePlugins('spellchecker'); + $editorConfig->addButtonsToLine(2, 'spellchecker'); + $token = SecurityToken::inst(); - HTMLEditorConfig::get($editor) + + $editorConfig ->setOption('spellchecker_rpc_url', Director::absoluteURL($token->addToUrl('spellcheck/'))) ->setOption('browser_spellcheck', false) - ->setOption( - 'spellchecker_languages', - implode(',', $this->getLanguages()) - ); + ->setOption('spellchecker_languages', implode(',', $this->getLanguages())); + + $defaultLocale = $this->getDefaultLocale(); + if ($defaultLocale) { + $editorConfig->setOption('spellchecker_language', $defaultLocale); + } return $delegate($request); } @@ -53,4 +62,25 @@ public function getLanguages() } return $languages; } + + /** + * Returns the default locale for TinyMCE. Either via configuration or the first in the list of locales. + * + * @return string|false + */ + public function getDefaultLocale() + { + // Check configuration first + $defaultLocale = SpellController::config()->get('default_locale'); + if ($defaultLocale) { + return $defaultLocale; + } + + // Grab the first one in the list + $locales = SpellController::get_locales(); + if (empty($locales)) { + return false; + } + return reset($locales); + } } diff --git a/src/Handling/SpellController.php b/src/Handling/SpellController.php index e07deac..3169b57 100644 --- a/src/Handling/SpellController.php +++ b/src/Handling/SpellController.php @@ -21,7 +21,16 @@ class SpellController extends Controller * @var array * @config */ - private static $locales = array(); + private static $locales = []; + + /** + * Optional: define the default locale for TinyMCE instances. If not defined, the first locale in the list of + * available locales will be used. + * + * @var string|bool + * @config + */ + private static $default_locale = false; /** * Necessary permission required to spellcheck. Set to empty or null to disable restrictions. diff --git a/tests/Handling/SpellCheckMiddlewareTest.php b/tests/Handling/SpellCheckMiddlewareTest.php new file mode 100644 index 0000000..5dc4207 --- /dev/null +++ b/tests/Handling/SpellCheckMiddlewareTest.php @@ -0,0 +1,24 @@ +set(SpellController::class, 'default_locale', 'foo'); + $this->assertSame('foo', $middleware->getDefaultLocale(), 'Returns configured default'); + + Config::modify() + ->set(SpellController::class, 'default_locale', false) + ->set(SpellController::class, 'locales', ['foo_BAR', 'bar_BAZ']); + $this->assertSame('foo_BAR', $middleware->getDefaultLocale(), 'Returns first in `locales`'); + } +}