Jump to:

5514 Posts in 1733 Topics by 1219 members

Customising the CMS

SilverStripe Forums » Customising the CMS » allow user to customize page theme

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

Page: 1 2 3
Go to End
Author Topic: 4291 Views
  • mccarville
    Avatar
    Community Member
    32 Posts

    Re: allow user to customize page theme Link to this post

    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!"

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: allow user to customize page theme Link to this post

    of course $Theme should be $this->Theme

  • mccarville
    Avatar
    Community Member
    32 Posts

    Re: allow user to customize page theme Link to this post

    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

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: allow user to customize page theme Link to this post

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

  • mccarville
    Avatar
    Community Member
    32 Posts

    Re: allow user to customize page theme Link to this post

    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

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: allow user to customize page theme Link to this post

    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?

  • mccarville
    Avatar
    Community Member
    32 Posts

    Re: allow user to customize page theme Link to this post

    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
  • mccarville
    Avatar
    Community Member
    32 Posts

    Re: allow user to customize page theme Link to this post

    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!!!!!!!

    4291 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.