Jump to:

5513 Posts in 1732 Topics by 1218 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: 4288 Views
  • mccarville
    Avatar
    Community Member
    32 Posts

    allow user to customize page theme Link to this post

    I'm by no means any sort of expert when it come to coding or php but I am trying to implement a drop down box in the cms on Root.content.main that will allow the user to set the theme for the page.

    I could but completely off but this is the code I have come up with so far... as now I getting a parse error on line "SSViewer::set_theme('$Theme');"

    ---------------------------
    <?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()
    ));
    return $fields;}
    }

    class Page_Controller extends ContentController {

    function init() {

    switch($this->URLSegment) {

    SSViewer::set_theme('$Theme');
    break;
    }

    parent::init();

    Requirements::themedCSS("layout");
    Requirements::themedCSS("typography");
    Requirements::themedCSS("form");

    }

    }

    ?>

    P.S. LOVE SilveStripe!!!

  • Hamish
    Avatar
    Community Member
    712 Posts

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

    Change:

    SSViewer::set_theme('$Theme');

    to:

    SSViewer::set_theme("$Theme");

    A string encapsulated in single quote marks will not evaluation php variables, so your script was trying to set it to a theme called $Theme

  • mccarville
    Avatar
    Community Member
    32 Posts

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

    I tried what you suggested and I still got a parse error on the same line. Just for kicks I ran the code below and it worked as anticipated. Which would lead me to believe it is definitely in the commented function... but I don't have any idea how to fix it...

    <?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() {

    /*switch($this->URLSegment) {

    SSViewer::set_theme("$Theme");
    break;
    }*/

    parent::init();

    Requirements::themedCSS("layout");
    Requirements::themedCSS("typography");
    Requirements::themedCSS("form");

    }

    }

    ?>

  • dio5
    Avatar
    Community Member
    501 Posts

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

    Maybe I'm missing something, but why even put quotes around $Theme?

    SSViewer::set_theme($Theme); should work as well.

    Also,

    why have a switch when you don't have 'cases'?

    Just leave the switch out I would think. If $theme is set per page it will automatically set this, no switch $urlsegment needed...

  • mccarville
    Avatar
    Community Member
    32 Posts

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

    Tried what you suggested with no luck... I tried it with no quot, single quote, double quotes, with and without the swith $URLsegment... nothing

    I also tried removing the theme call in the _config file but that did not help

    I was able to build and seemingly select the theme in the CMS but the pages would not render stating an error in SSVeiwer.php on line 151

  • ajshort
    Avatar
    Community Member
    244 Posts

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

    Hey there,

    remove the switch statement, all you need is to put "SSViewer::set_theme($this->Theme);" where you have the commented out switch.

  • mccarville
    Avatar
    Community Member
    32 Posts

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

    Tried what you suggested and I am still getting errors. Again the build is going well but when the page tries to render i get errors... I also see the same errors if I try to access the admin page ... The error is saying that it can not find the template Page.ss... Almost seems like it is looking for the for the .ss files before the theme is set??

  • dio5
    Avatar
    Community Member
    501 Posts

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

    Could you check a few things:

    - try a flush first (add ?flush=1 to your url)
    - check if there's really a value saved in the database?
    - do a Debug::show($Theme); right before SSViewer::set_theme... to see what the value is.

    Perhaps this call to set the theme is indeed too late and should happen earlier?

    4288 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.