Jump to:

5514 Posts in 1733 Topics by 1219 members

Customising the CMS

SilverStripe Forums » Customising the CMS » [SOLVED] DropdownField in SiteConfig

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

Page: 1
Go to End
Author Topic: 1351 Views
  • Sticks
    Avatar
    Community Member
    29 Posts

    [SOLVED] DropdownField in SiteConfig Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: [SOLVED] DropdownField in SiteConfig Link to this post

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

  • Sticks
    Avatar
    Community Member
    29 Posts

    Re: [SOLVED] DropdownField in SiteConfig Link to this post

    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.

    1351 Views
Page: 1
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.