Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[SOLVED] DropdownField in SiteConfig


Go to End


3 Posts   2325 Views

Avatar
Sticks

Community Member, 31 Posts

17 June 2011 at 12:26pm

Edited: 17/06/2011 12:27pm

I'm trying to add a DropdownField to the main site options just below the 'Themes' field. The idea is to have multiple colour schemes for a particular theme, and the DropdownField will select a different .css file to use to style the site.

I had this working fine on a page by page basis (thanks to this tutorial: http://goo.gl/cuQuh ) by adding the following code to the Page class:

public static $db = array(
		..
		"ColourSchemes" => "Enum('Red, Green, Blue', 'Red')",
	);

..

function getCMSFields() {
		$fields = parent::getCMSFields();
		..
		$fields->addFieldToTab(
		    "Root.Content.Main",
		    new DropdownField(
	        'ColourSchemes',
	        'Choose a colour scheme',
	        $this->dbObject('ColourSchemes')->enumValues())
		);
			return $fields;
	}

This adds the DropdownField and populates it just fine. I then add to the Page_Controller this line:

Requirements::themedCSS($this->owner->ColourSchemes); 

This successfully links the page to the style sheet Red.css (or Green.css etc.).

Note: I tried the following code in the Page.ss file to achieve this but it doesn't quite work for me:

<% require themedCSS($ColourSchemes) %> 

My problem is with using this method in a SiteConfig file. Trying to add the DropdownField with the above method yields a fatal error 'Fatal error: Call to undefined method SiteConfig::dbObject() in ... mysite\code\siteconfig.php.

The database builds successfully with dev/build/?flush=1 but then when trying to view the DropdownField in the backend the above error is thrown.

My first question is: Am I trying to do this colour scheme idea in a reasonable way? Is there a better and/or easier way to change the colour scheme of a SilverStripe theme?

And secondly: Why is the code not working in the SiteConfig? I understand it's different to page classes, but I haven't wrapped my head around exactly how it's different and what I need to change to get things to work.

Avatar
Willr

Forum Moderator, 5523 Posts

18 June 2011 at 3:56pm

<% require themedCSS($ColourSchemes) %>

You cannot use variables in template class like this as of any of the 2.* releases. You would have to require it via the PHP Requirements api.

As for the error with siteconfig, your custom siteconfig would be an extension (Decorator) on the built in class so you would need to refer to the original siteconfig object using $this->owner ie - $this->owner->dbObject(..).

Also your $db = array() would be an extraStatics function for SiteConfig. See http://doc.silverstripe.org/sapphire/en/reference/siteconfig for reference.

Avatar
Sticks

Community Member, 31 Posts

30 August 2011 at 5:19pm

Thanks Willr. Works perfectly. Adding '->owner' to the end of $this when dealing with the SiteConfig has cropped up a few times now, so that's a huge help. Cheers.