Jump to:

794 Posts in 409 Topics by 304 members

Widgets

SilverStripe Forums » Widgets » Can Widgets use $SiteConfig

Discuss SilverStripe Widgets.

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

Page: 1
Go to End
Author Topic: 679 Views
  • Matty Balaam
    Avatar
    Community Member
    69 Posts

    Can Widgets use $SiteConfig Link to this post

    Hi,

    I'm making a simple widget to allow me to link to a page, with a simple description. This will only be on some pages.

    To keep this in one area of the CMS so I can alter the text globally I have put the fields into CustomSiteConfig.

    However, I can't get widgets to access the data using $SiteConfig.XX

    If I put the same code directly into page.ss it works fine.

    <?php

    class CustomSiteConfig extends DataObjectDecorator {

    function extraStatics() {
    return array(

    'db' => array(
    'WidgetDescription => 'Text'
    )
    ,
    'has_one' => array(
    'WidgetLink' => 'SiteTree'
    )
    );
    }

    public function updateCMSFields(FieldSet &$fields) {

    $fields->addFieldToTab("Root.Main", new TextField('WidgetDescription', 'Widget Description'));
    $fields->addFieldToTab('Root.Main', new TreeDropdownField("WidgetLinkID", "Widget Link", "SiteTree"));
    }

    }

    Thank you for reading.

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Can Widgets use $SiteConfig Link to this post

    No I don't believe widgets can. They're encapsulated from the site from the looks of it.

    If you want to access the SiteConfig you can add a function on your widget class that pulled it down

    function SiteConfig() {
    return SiteConfig::current_site_config();
    }

  • Matty Balaam
    Avatar
    Community Member
    69 Posts

    Re: Can Widgets use $SiteConfig Link to this post

    Thanks Willr, I had got around this by using DataObject::get_one("SiteConfig"). I like to do things the "right" way if possible, so are there advantages to using the code you've posted?

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Can Widgets use $SiteConfig Link to this post

    Checkout the code and you'll get a sense of what extra things it does (why you you should use it!)

    https://github.com/silverstripe/sapphire/blob/2.4/core/model/SiteConfig.php#L153

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