Skip to content

Commit

Permalink
FIX Allow configurable default locale, or use the first defined locale
Browse files Browse the repository at this point in the history
  • Loading branch information
robbieaverill committed May 3, 2018
1 parent c0bd59c commit a3b586a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
46 changes: 38 additions & 8 deletions src/Handling/SpellCheckMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand All @@ -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);
}
}
11 changes: 10 additions & 1 deletion src/Handling/SpellController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions tests/Handling/SpellCheckMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace SilverStripe\Spellcheck\Tests\Handling;

use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\SpellCheck\Handling\SpellCheckMiddleware;
use SilverStripe\SpellCheck\Handling\SpellController;

class SpellCheckMiddlewareTest extends SapphireTest
{
public function testGetDefaultLocale()
{
$middleware = new SpellCheckMiddleware();

Config::modify()->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`');
}
}

0 comments on commit a3b586a

Please sign in to comment.