44_
44_

Reputation: 72

How can I allow the user to set a custom global value in the silverstripe CMS?

I would like to be able to set custom values in the CMS, such as with the site name and tagline. I can't currently find any way of doing this other than on individual pages.

Upvotes: 1

Views: 158

Answers (1)

bummzack
bummzack

Reputation: 5875

You can do so by extending SiteConfig. Your Extension might look like this:

class CustomSiteConfig extends DataExtension
{
    private static $db = array(
        'CustomContent' => 'Varchar(255)'
    );

    public function updateCMSFields(FieldList $fields)
    {
        $fields->addFieldToTab('Root.Main',
            TextField::create('CustomContent', 'Custom content')
        );
    }
}

Then you need to apply the extension to SiteConfig. Add the following to mysite/_config/config.yml

SiteConfig:
  extensions:
    - CustomSiteConfig

And that's it. Run dev/build and your new field should be editable in the CMS as well as accessible in the Template using: $SiteConfig.CustomContent

Upvotes: 2

Related Questions