Extending SilverStripe's SiteConfig
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)
DataObject::add_extension('SiteConfig', 'SiteConfigOverride');
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")));
}
}
- 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'));
- For available field types, see the documentation.
- Each field must be added in the format shown above:
- 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.
