Jump to:

23355 Posts in 17734 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » SS3 - Accessing SiteConfig values in Widget class

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 354 Views
  • Fraser
    Avatar
    Community Member
    46 Posts

    SS3 - Accessing SiteConfig values in Widget class Link to this post

    I'm having issues getting hold of values stored in my SiteConfig from within my widget class. I searched around and found people suggesting I should be able to access the config through SiteConfig::current_site_config() but when I try and implement this, I get an error:

    Parse error: syntax error, unexpected '(', expecting ',' or ';' in /...../widgets_facebookFeed/FacebookFeedWidget.php on line 3

    <?php
    class FacebookFeedWidget extends Widget{
       static $thisConfig = SiteConfig::current_site_config();
       static $title = "";
       static $cmsTitle = "Facebook Feed Widget";
       static $description = "This widget shows the Facebook feed";

       static $db = array(
          "FacebookURL" => "Text"
       );
       
       static $defaults = array(
          "FacebookURL" => $thisConfig->FacebookURL
       );

       function getCMSFields(){
          return new FieldList(
             new TextField("FacebookURL", "Facebook URL")
          );
       }
       
       function getFacebookURL(){
          $output = new ArrayList();
          $output->push(
             new ArrayData(
                array(
                   "FacebookURL" => $this->URL
                )
             )
          );
          return $output;
       }
    }

    What am I doing wrong here?

  • jak
    Avatar
    Community Member
    46 Posts

    Re: SS3 - Accessing SiteConfig values in Widget class Link to this post

    The Problem is this line:

    static $thisConfig = SiteConfig::current_site_config();


    In PHP, it is not possible to initialize a static constant with the result of a function call (i.e. you are not allowed to call a function there). You will have to set the default value in some other way (or at a different point in your code).

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