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

allow user to customize page theme


Go to End


21 Posts   7063 Views

Avatar
mccarville

Community Member, 32 Posts

21 January 2009 at 2:11am

Here is the code I ran

<?php

class Page extends SiteTree {
	static $db = array(
	'Theme' => "Enum('higherground,blackcandy','higherground')"
	);
	static $has_one = array(
   );
   
   function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', new DropdownField(
'Theme',
'Select A Theme:',
singleton('Page')->dbObject('Theme')->enumValues()
));

if (!Permission::check("ADMIN")){
   $fields->removeFieldFromTab('Root','Access');
} 
return $fields;}
}

class Page_Controller extends ContentController {
	function init() {
	    Debug::show($Theme);
		SSViewer::set_theme($Theme);
	
		parent::init();
		
		Requirements::themedCSS("layout");
		Requirements::themedCSS("typography");
		Requirements::themedCSS("form");
	}
}

?>

This is the output at the top of the rendered page:

Notice: Undefined variable: Theme in C:\wamp\www\testit\mysite\code\Page.php on line 26
Debug (Page_Controller->init() in line 26 of Page.php)


Notice: Undefined variable: Theme in C:\wamp\www\testit\mysite\code\Page.php on line 27

Thank you soooo much for your efforts with this... it has been driving me nuts...

An old salty sea captain walks into a bar and sits down to have a drink. The bartender walks over and notices that he has a big ship steering wheel hanging out the front of his pants, but chooses not to say anything and serve the man his drink. As the night wears on finally the bartender can no longer help himself... "So whats with the huge steering wheel hanging out of your pants??" The man looks up and replies "Arrrgg... it's drivin me nuts!"

Avatar
dio5

Community Member, 501 Posts

21 January 2009 at 2:17am

of course $Theme should be $this->Theme :)

Avatar
mccarville

Community Member, 32 Posts

21 January 2009 at 7:09am

Edited: 21/01/2009 10:44am

When using code:

.....
Debug::show($this->Theme); //I did use the lowercase t in this... just edited my post
SSViewer::set_theme($this->Theme);
.....

I get the following errors... good catch though...

Notice: Undefined variable: This in C:\wamp\www\testit\mysite\code\Page.php on line 26

Notice: Trying to get property of non-object in C:\wamp\www\testit\mysite\code\Page.php on line 26
Debug (Page_Controller->init() in line 26 of Page.php)

Notice: Undefined variable: This in C:\wamp\www\testit\mysite\code\Page.php on line 27

Notice: Trying to get property of non-object in C:\wamp\www\testit\mysite\code\Page.php on line 27

Avatar
dio5

Community Member, 501 Posts

21 January 2009 at 8:52am

Edited: 21/01/2009 8:53am

$this, not $This, it needs to be lowercase.

Avatar
mccarville

Community Member, 32 Posts

21 January 2009 at 10:45am

With the lower case $this->Theme I am still getting the errors below:

Notice: Undefined variable: This in C:\wamp\www\testit\mysite\code\Page.php on line 26

Notice: Trying to get property of non-object in C:\wamp\www\testit\mysite\code\Page.php on line 26
Debug (Page_Controller->init() in line 26 of Page.php)

Notice: Undefined variable: This in C:\wamp\www\testit\mysite\code\Page.php on line 27

Notice: Trying to get property of non-object in C:\wamp\www\testit\mysite\code\Page.php on line 27

Avatar
dio5

Community Member, 501 Posts

21 January 2009 at 9:13pm

In your error 'This' is still capitalized, are you sure it's not capitalized somewhere anymore?

Also, what happens when you have this:

function init()
{
Debug::show($this);
parent::init();
}

It should render a list of all possible 'tags', is $Theme one of them?

Avatar
mccarville

Community Member, 32 Posts

22 January 2009 at 11:12am

Why must the sweet taste of victory be so quickly soured by defeat???... Though the war is not over and i shall overcome!

So I did what you suggested with debug $this, and I was reading through the variables it listed and I saw one called Theme... then it hit me... Have I been reusing and already declared variable? On a whim I changed the code and replaced Theme with ThemeX. I rebuilt the database and went to my index page... Amazing though my config file calls blackcandy, the page rendered with higherground as I had set it to in the CMS previously.... Ofcourse this was so amazing that I could hardly believe my eyes. I decide to make sure it was really working and change the value in the drop down in the CMS to see if it would change the page theme back to blackcandy... when I visited ss/admin I was greated with the attached page [the red box at the top fatal error page]

Just when I was so close...

I can't wait to nail this and get the right code posted up here somewhere, so no one ever has to suffer through this again

Attached Files
Avatar
mccarville

Community Member, 32 Posts

22 January 2009 at 11:26am

I did it!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I found a typo where I forgot to change a Theme to ThemeX... seems to be working seemsly now!!!!!!!!!!!!!!!!!!!!!!!!!11

I can't tell you how much I appreciate everyone's help... especially you dio5, thanks for sticking in...

Final code -

<?php

class Page extends SiteTree {
	static $db = array(
	'ThemeX' => "Enum('higherground,blackcandy','higherground')"
	);
	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()
));


return $fields;}
}

class Page_Controller extends ContentController {
	function init() {
	   
		SSViewer::set_theme($this->ThemeX);
	
		parent::init();
		
		Requirements::themedCSS("layout");
		Requirements::themedCSS("typography");
		Requirements::themedCSS("form");
	}
}

?>

AMAZING!!!!!!!!!! SilverStripe is the best!!!!!!!