1
votes

I'm trying to access a siteconfig value in my widget class with:

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

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

    static $defaults = array(
        "FacebookURL" => $doSiteConfig->FacebookURL
    );

    function getCMSFields(){
        return new FieldList(
            new TextField("FacebookURL", "Facebook URL")
        );
    }

    function getFacebookURL(){
        $output = new ArrayList();
        $output->push(
            new ArrayData(
                array(
                    "FacebookURL" => $this->FacebookURL
                )
            )
        );
        return $output;
    }
}

But am getting an error: Parse error: syntax error, unexpected '(', expecting ',' or ';' in /....../widgets_facebookFeed/FacebookFeedWidget.php on line 8

What am I doing wrong here?

1

1 Answers

4
votes

you can NOT do something like this:

static $defaults = array(
    "FacebookURL" => $doSiteConfig->FacebookURL
);

but luckily there is the function polulateDefaults, which takes $defaults and sets it to the object, so we can hook into that:

public function populateDefaults() {
    parent::populateDefaults();
    $this->FacebookURL = SiteConfig::current_site_config()->FacebookURL;
}