Theme switcher

silverstripe-4
Tags: #<Tag:0x00007f0826f98398>

#1

Silverstripe Version: 4.2.2

If I have lets say four different themes and I want to switch the themes with a link on my front-end (like /?theme=theme1), how can i realize that with SilverStripe 4?

In SilverStripe 3.6 I could jut use code in my _config.php like:

$theme="";
if (array_key_exists('theme', $_GET)) {
	$theme=$_GET['theme'];
}
SSViewer::set_theme($theme);

I googled for hours now and found that I should use something like:

Config::inst()->update('SSViewer', 'theme', 'theme1');

I tried to put that code into my _config.php and also Page.php and PageController.php but no matter what I do I get a Servererror on the front-end.


#2

First things first. If it’s not already, make sure your site is in dev mode (SS_ENVIRONMENT_TYPE=dev in your .env file) - that will help with debugging, since it will show you errors.

The error may be something as simple as a missing use statement from the file you’re working on. Since the Config class exists in a specific namespace, you’ll need to tell PHP where to find it:

use SilverStripe\Core\Config\Config;

at the top of the file might cure it.

There’s a theme switcher add-on here: GitHub - andrewandante/silverstripe-theme-picker which might give you some additional hints on what you need to implement.


#3

Thank you very much!