When you first install your SilverStripe site, the ‘Settings’ menu is pretty limited, offering just Title, Tagline, and a choice of templates.
This is fine, but it’s often useful to add other options, such as logo upload, links to facebook, uploading a banner image that appears on every page.
MySiteConfig.php
In your ‘mysite/code/’ folder, create a new file called ‘MySiteConfig.php’, and insert the following text:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
<?php class MySiteConfig extends DataExtension { public static $has_one = array( 'HeaderImage' => 'Image', 'LogoImage' => 'Image', ); public static $db = array( 'FacebookURL' => "Varchar(250)", 'VimeoURL' => "Varchar(250)", ); public function updateCMSFields(FieldList $fields) { $fields->addFieldToTab("Root.Main", new UploadField("LogoImage", "Choose an image for your site logo")); $fields->addFieldToTab("Root.Main", new UploadField("HeaderImage", "Choose an image for the site header")); $fields->addFieldToTab("Root.Main", new TextField("FacebookURL", "Enter the full URL of your Facebook page")); $fields->addFieldToTab("Root.Main", new TextField("VimeoURL", "Enter the full URL of your Vimeo page")); } } |
This file creates a ‘DataExtension’. A DataExtension, as the name suggests, is used to extend any set of data. You declare the additional databse fields and relations just as you would when creating a new page.
And you use the function ‘public function updateCMSFields {}’ to add the fields to the CMS page, just as you would when creating a new page.
_config.php
In your ‘mysite’ folder, open the ‘_config,php’ file, and add the following line:
|
Object::add_extension('SiteConfig', 'MySiteConfig'); |
This line simple tells SilverStripe to take the ‘SiteConfig’ (the basic settings that already exist) and add ‘MySiteConfig’ to it.
Dev/Build
Go to your site, log in, and add ‘/dev/build’ to the URL – eg ‘mysite.com/dev/build’ to refresh the database.
Now, in the admin area, go to the site settings page and you should see your new options.
Using these fields in the Template
You can now add a logo, banner image, and enter the facebook / vimeo links – the data is stored in the database, but we need to show it on our page.
We do this by adding ‘$SiteConfig.’ in front of the names of the fields. Eg:
|
$SiteConfig.LogoImage.SetRatioSize(250,250) <a href="$SiteConfig.FacebookURL">Visit my facebook page</a> |