I've recently learned to create custom CMS sections in the back-end using class ModelAdmin
and linking them using private static $managed_models = array('Contact');
to a DataObject named Contact
.
My goal is to create a new section named "Contact" which holds a few contact details (phone, email etc). These are singular fields, not multiple records.
Unfortunately this setup (linking to dataobject) creates a sort of GridField in the back-end that creates multiple "Contact" records with the fields Phone, Email etc.
How can I create a custom CMS section that holds a singular entity of given Fields?
Instead of adding a single Client
DataObject
we can store these variables in the site settings using a SiteConfig
extension.
To do this we create a SiteConfig
extension with the variables and fields we would like:
mysite/code/extensions/CustomSiteConfig.php
class CustomSiteConfig extends DataExtension {
private static $db = array(
'Phone' => 'Varchar(255)',
'Email' => 'Varchar(255)',
'Address' => 'Varchar(255)'
);
public function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab('Root.Contact', TextField::create('Phone'));
$fields->addFieldToTab('Root.Contact', TextField::create('Email'));
$fields->addFieldToTab('Root.Contact', TextField::create('Address'));
}
}
We activate the extension:
mysite/_config/config.yml
SiteConfig:
extensions:
- CustomSiteConfig
We can now use the the variables in any of our templates:
<% if $SiteConfig.Phone %>$SiteConfig.Phone<% end_if %>
<% if $SiteConfig.Email %>$SiteConfig.Email<% end_if %>
<% if $SiteConfig.Address %>$SiteConfig.Address<% end_if %>