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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Widgets /

Discuss SilverStripe Widgets.

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

Can Widgets use $SiteConfig


Go to End


4 Posts   2344 Views

Avatar
Matty Balaam

Community Member, 74 Posts

2 November 2011 at 5:04am

Edited: 02/11/2011 5:06am

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

Forum Moderator, 5523 Posts

4 November 2011 at 6:49pm

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

Community Member, 74 Posts

4 November 2011 at 8:14pm

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

Forum Moderator, 5523 Posts

5 November 2011 at 11:39am

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