Can I dynamically change SilverStripe theme?

Viewed 370

I have made for my site additional theme for cecutients (people with low eyesight). Can I dynamically change site theme pushing some button on the main page?

2 Answers

An update for Silverstripe 4.x version:

use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\Control\Session;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\View\SSViewer;
use SilverStripe\Core\Config\Config;

class PageController extends ContentController
{

    private static $allowed_actions = ['changeTheme'];

    protected function init()
    {
        parent::init();
        if ($theme = $this->getRequest()->getSession()->get('theme')) {
            SSViewer::config()->update('theme_enabled', true);
            SSViewer::set_themes([$theme]);
        }            

    }

    public function changeTheme()
    {
        $theme = $this->request->param('ID');
        $existingThemes = Config::inst()->get('SilverStripe\View\SSViewer', 'themes');
        if (in_array($theme, $existingThemes)) {
            SSViewer::config()->update('theme_enabled', true);
            SSViewer::set_themes([$theme]);
            $this->getRequest()->getSession()->set('theme', $theme);
        }

        return $this->redirectBack();
    }        
}
Related