user742736
user742736

Reputation: 2729

Silverstripe add dataobject to Siteconfig

I am trying to add a dataobject to the site config (Settings). I have managed to add a logo field and this works now I would like to add some repeater fields eg (social media fields). This doesn't work. I am getting an error.

Code and error below. I have looked in the database and all the fields have been created after the dev/build. I have also Flush=all.

mysite/_config/app.yml

SiteConfig:
 extensions:
  - CustomSiteConfig

mysite/code/extensions/CustomSiteConfig.php

class CustomSiteConfig extends DataExtension {

private static $has_one = array(
    'Logo' => 'Image'
);

private static $has_many = array(
    'SocialMedia' => 'SocialMedia'
);


public function updateCMSFields(FieldList $fields) {

    $fields->addFieldToTab("Root.Main", new UploadField('Logo', 'Logo'));
    $fields->addFieldToTab('Root.Main', $this->getSocialMedia());

}

protected function getSocialMedia() {
    $gridFieldConfig = GridFieldConfig::create()->addComponents(
        new GridFieldToolbarHeader(),
        new GridFieldAddNewButton('toolbar-header-right'),
        new GridFieldSortableHeader(),
        new GridFieldDataColumns(),
        new GridFieldPaginator(10),
        new GridFieldEditButton(),
        new GridFieldDeleteAction(),
        new GridFieldDetailForm(),
        new GridFieldSortableRows('SortID')
    );

    $gridField = new GridField("SocialMedia", "SocialMedia", $this->SocialMedia(), $gridFieldConfig);

    return $gridField;
}

}

mysite/code/extensions/SocialMedia.php

class SocialMedia extends DataObject { 

    public static $db = array( 
        'SortID' => 'Int',
        'Social' => 'Text',
        'Icon' => 'Text',
        'URL' => 'Text'
    );

    public static $has_one = array(
        'CustomSiteConfig' => 'CustomSiteConfig'
    );

    public function getCMSFields() {
        return new FieldList(
            new TextField('Social'),
            new TextField('Icon'),
            new TextField('URL')

        );

    }
}

Error Message

Fatal error: Call to undefined method CustomSiteConfig::SocialMedia() in file/path/mysite/code/extensions/CustomSiteConfig.php on line 34

Line 34 is

$gridField = new GridField("SocialMedia", "SocialMedia", $this->SocialMedia(), $gridFieldConfig);

Upvotes: 3

Views: 901

Answers (1)

munomono
munomono

Reputation: 1245

On a decorator/DataExtension one should use $this->owner instead of just $this. Line 34 should look like this:

$gridField = new GridField("SocialMedia", "SocialMedia", $this->owner->SocialMedia(), $gridFieldConfig);

See also: http://doc.silverstripe.org/en/developer_guides/extending/extensions#owner

Upvotes: 4

Related Questions