I created an account on the recipes page but it seemd like I could only edit content but not create a new article. I amy be missing something... I wrote the how-to...
-------------------------
The goal of this How-To is to show you how to create a drop down list in the CMS that allows the user to select a specific theme for the page. This tutorial assumes that you have a working knowledge of tutorials 1 and 2.
This is the final code:
<?php
class Page extends SiteTree {
static $db = array(
'ThemeX' => "Enum('District,AHS,MAS,HAS','District')"
);
static $has_one = array(
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', new DropdownField(
'ThemeX',
'Select A Theme:',
singleton('Page')->dbObject('ThemeX')->enumValues()
),'Content');
return $fields;
}
}
class Page_Controller extends ContentController {
function init() {
if($this->ThemeX){
SSViewer::set_theme($this->ThemeX);
}
parent::init();
Requirements::themedCSS("layout");
Requirements::themedCSS("typography");
Requirements::themedCSS("form");
}
}
?>
Hopefully most of this looks standard to you, so I will just point out the modifications we made.
MOD 1
static $db = array(
'ThemeX' => "Enum('District,AHS,MAS,HAS','District')"
);
We create a variable called ThemeX. The variable is of type Enum(?) which ends up populating the values for the drop down list. The first set of quotes inside the parentheses contains the values for the drop down list; each value should be separated by a comma. The second set of parentheses contains the default selected value.
MOD 2
$fields->addFieldToTab('Root.Content.Main', new DropdownField(
'ThemeX',
'Select A Theme:',
singleton('Page')->dbObject('ThemeX')->enumValues()
),'Content');
Now we need to add the dropdown list into the CMS. The set of quotes for DropdownField calls the variable we created in MOD 1. The next set of quotes is the text that will appear above the dropdown field in the CMS. In singleton(‘Page’) Page refers to the name of the class [as in class Page extends SiteTree]. In dbObject(‘ThemeX’) the ThemeX is again referring to the variable we declared in MOD 1. We include a ‘Content’ at the end to tell SS to place the drop down list above the Content field in the CMS.
MOD 3
if($this->ThemeX){
SSViewer::set_theme($this->ThemeX);
}
IMPORTANT!! Without the if statement, if you have any pages in your web site that do not already have ThemeX declared, your CMS will crash. Since we have an if statement the page will look in –config.php for a default theme if no variable is declared.
Looking at the code, we call $this->ThemeX which means ThemeX from this page. Are if statements tells the system not to process the theme swap if ThemeX has not been declared. Finally our SSViewer::set_theme call, sets the theme to the users selection.
At this point you are able to log in to the CMS and set the theme of a page by selecting the theme from a drop down list and clicking save and publish.
Hope this helps!! SilverStripe is the best, keep up the good work!!