Home » Blog » Extending SilverStripe's SiteConfig

kiveo Portfolio

kiveo Services

Web Applications

your business can have the custom programming required to run your business as efficiently as possible

Website Design

your business, no matter the size, deserves a website you are proud of, that does exactly what you need

Agency Solutions

your creative agency shouldn't have to turn away work because you don't have developers on staff, or your developers are busy.

User Support

your customers deserve to have access to excellent technical support and customer service even when you're busy.

Extending SilverStripe's SiteConfig

Posted by on January 22nd, 2010

Tags: ,

So, SilverStripe 2.4 (still in alpha at the time of this post) has introduced a new feature (and class) called "Site Config". This is huge, folks - on the level of discovering XRays huge (except without the radiation poisoning part - unless you're just that sloppy with your code, then, perhaps you deserve it).  My use for the SiteConfig class is to level the playing field with other CMS offerings like Joomla! and WordPress and allow users to change some (semi) static content within their sites, without having to come to me to update their templates.

By default the Site Config class only offers two fields:

 

  • Title ($SiteConfig.Title in the templates)
  • Tagline ($SiteConfig.Tagline in the templates)
I want, nay, need more fields. I first figured out how to hack the sapphire/core/model/SiteConfig.php to allow me to add extra fields. Then my inner (good) programmer said, "Nay. Thou shalt not screw with code that is not upgrade safe." (Voiced by none other than Morgan Freeman.) And, reluctantly I concurred. After much face-palming, head-banging and other self inflicted hurting, I turned to my programmer friends who are smarter than I (specifically, Miles).

Here's what we worked out - and it works great, btw!

In mysite/_config.php, add this line:

DataObject::add_extension('SiteConfig', 'SiteConfigOverride');

In mysite/code/ create a file called "SiteConfigOverride.php" and add this - don't worry, I'll explain it all after:

class SiteConfigOverride extends DataObjectDecorator{
     function extraStatics() {
		return array(
			'db' => array(
				"AddressLn1" => "Varchar(255)",
				"AddressLn2" => "Varchar(255)",
				"City" => "Varchar(255)",
				"State" => "Varchar(255)",
				"ZIP" => "Varchar(10)",
				"Phone1" => "Varchar(12)",
				"Phone2" => "Varchar(12)",
				'LogoURL' => 'Varchar(255)'
			)
		);
	}

	public function updateEditFormFields(FieldSet &$fields) {
		$fields->addFieldToTab("Root.Main", new TextField("AddressLn1", _t('SiteConfig.ADDRESSLN1',"Address Ln 1")));
		$fields->addFieldToTab("Root.Main", new TextField("AddressLn2", _t('SiteConfig.ADDRESSLN2',"Address Ln 2")));
		$fields->addFieldToTab("Root.Main", new TextField("City", _t('SiteConfig.CITY',"City")));
		$fields->addFieldToTab("Root.Main", new TextField("State", _t('SiteConfig.STATE',"State")));
		$fields->addFieldToTab("Root.Main", new TextField("ZIP", _t('SiteConfig.ZIP',"ZIP")));
		$fields->addFieldToTab("Root.Main", new TextField("Phone1", _t('SiteConfig.PHONE1',"Main Phone")));
		$fields->addFieldToTab("Root.Main", new TextField("Phone2", _t('SiteConfig.PHONE2',"Secondary Phone or Fax")));
	    $fields->addFieldToTab("Root.Main", new TextField("LogoURL", _t('SiteConfig.LOGOURL', "Logo URL")));
	}
}

Note: (You know it's important because I bolded AND underlined it) - You must close your PHP tag in the SiteConfigOverride.php file - otherwise SilverStripe DIES a horrible white screen death without any errors (they're not included above because SilverStripe's default editor conks out when you try add them...more investigating...).

 

  • Despite the current (2.3 branch) documentation, SiteConfigOverride cannot extend just any class, when called from DataObject via the add_extension method, it must (at least as of 2.4.0a) extend DataObjectDecorator. 
  • The extraStatics method is how you add new fields to the DB.
  • The updateEditFormFields method is how you tell the admin screen to add new fields to the default Site Config screen.
    • Each field must be added in the format shown above:
      • $fields->addFieldToTab("tabName",$tabType,insert_before);
      • $tabType (shortcutted in the example) needs to be in the format $tabType = new FieldType('dbColumnName',_t('SiteConfig.UPPERCASEDBCOLUMNNAME','Display Name'));
  • Run a /dev/build/?flush=all after creating the files and updating your _config.php and log into your admin and click on "Site Content" in your site tree and you should see your new fields.

In the illustrated case above, I've just added a set of text fields to capture address and phone info, as well as a place to drop in a logo URL that the client can define later. This gives the designer the ability to focus more on design elements than content elements. Each of these can be accessed from their properly cased DB column name. If you need to figure out what they are later, from a template, you can simply call $SiteConfig.Debug in the template and all of the database columns, properly cased, will be laid out for you. ENJOY!

 

PS - Thanks to Lloyd Emelle of emelle.me for catching a typo in the extraStatics method.