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.

Widgets

Discuss SilverStripe Widgets.

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

Can Widgets use $SiteConfig


Reply

4 Posts   691 Views

Avatar
Matty Balaam

2 November 2011 at 5:04am (Last edited: 2 November 2011 5:06am), Community Member, 69 Posts

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.

Avatar
Willr

4 November 2011 at 6:49pm Forum Moderator, 5511 Posts

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();
}

Avatar
Matty Balaam

4 November 2011 at 8:14pm Community Member, 69 Posts

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?

Avatar
Willr

5 November 2011 at 11:39am Forum Moderator, 5511 Posts

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