Extending SilverStripe 3 Site Settings

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:

<?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>

Comments

3 responses to “Extending SilverStripe 3 Site Settings”

  1. Mike Avatar
    Mike

    Ah, if it only worked…

    I’m getting the following error message:

    Fatal error: Object::add_extension() – Can’t find extension class for “MySiteConfig”

    Deprecated for SS 3.1.6 perhaps?

    1. Clone Avatar
      Clone

      Mike,
      I have just tested it with SS 3.1.5, works like a charm.

  2. Valentijn Avatar

    Thank you very much for the code. unfortunately the header images don’t scale on smaller screen sizes.

    any suggestions where to find tis error?

Leave a Reply

Your email address will not be published. Required fields are marked *