SSbits - Home page
Site by Carbon Crayon
Submit a Post >

Tutorials - Big bits of code to help you do more

Working with Site Config

SSBsiteconfig

One of the great new features of 2.4 is the introduction of a Site Config page. This allows you to put all of those fields which are not page related, such as The sites title, root access permissions and even the current theme. The SiteConfig class is simply a dataobject and so can easily be extended to include fields, relationships and functions which you can then access from anywhere in your site.

Accessing items in your SiteConfig is as easy as preceding each item with $SiteConfig. For example this would display the Title field:

<h1>$SiteConfig.Title</h1>

Or alternatively:

<% control SiteConfig %>
     <h1>$Title</h1>
<% end_control %>

Accessing SiteConfig from the Controller

So accessing it from the template is super simple and so it turns out is accessing it from a Controller:

$this->SiteConfig();

Or alternatively if you are in a class that does not descend from ContentController:

SiteConfig::current_site_config();

Customizing SiteConfig

So of course there's not much point in the SiteConfig unless we can add or own fields, relationships and functions. To do so we use the DataObjectDecorator class which allows us to 'decorate' other classes with fields, functions and relationships. We need to decorate SiteConfig instead of extend it with our own class because the core SiteConfig class is the one that SilverStripe will look for. Here is an example of a CustomSiteConfig class which extends DataObjectDecorator:

CustomSiteConfig.php

<?php
 
class CustomSiteConfig extends DataObjectDecorator {
	
	function extraStatics() {
		return array(
			'db' => array(
				'Spiel' => 'Text'
			),
			'has_one' => array(
				'SiteLogo' => 'Image'
			)
		);
	}
 
	public function updateCMSFields(FieldSet &$fields) {
		
		$fields->addFieldToTab("Root.Main", new TextareaField("Spiel"));
		
		$fields->addFieldToTab("Root.Main", new ImageField("SiteLogo", "Logo"));
	}
	
	public function getPooSpiel(){
		return '<h3>Poo!</h3>' . $this->owner->Spiel;
	}
}

So here we are adding an extra field to $db and a $has_one relationship. We then add the appropriate form fields to the CMS and we also define a new function. The function is not particularly useful in this case and simple adds the header 'poo' to the start of our Spiel field. However, notice that we use $this->owner to refer to the Spiel field. This is the syntax you must use when extending a DataObject using the DataObjectDecorator so that SilverStripe refers to the object that is being extended rather than the Extention class it self.

Now in order to get SilverStripe to register this extention we need to add this line to our _config.php:

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

Now in our template we can use these new fields like so:

$SiteConfig.SiteLogo

<% control SiteConfig.SiteLogo %>
	$CroppedImage(100,100)
<% end_control %>
<p>$SiteConfig.Spiel</p>
<p>$SiteConfig.PooSpiel</p>

So there you have it, the new SiteConfig. Very useful and easy when you know how! :)

Thanks to Johannes Fischer for suggesting this post.

Special Thanks

Special thanks go to Frank Mullenger for their contributions to this post.

Aram Balakjian avatar

Aram Balakjian

Aram is a web developer running London based agency Aab Web. He has a strong passion for developing attractive, usable sites around the SilverStripe CMS.

  • Quinten
    11/10/2010 6:12pm (4 years ago)

    Thanks, this comes in very handy. Didn't know about the SSbits site since today, seems to be very useful for my silverstripe project!!!!

  • Frank Mullenger
    27/10/2010 9:14pm (4 years ago)

    Is there any way for the dataobjectmanager to be used in the site config?

  • MRKDevelopment
    02/12/2010 4:22am (4 years ago)

    This is great, just what I was looking for. AWESOME.

  • Aram Balakjian
    02/12/2010 1:59pm (4 years ago)

    @Framk Mullenger - I was under the impression that DOM was now working in SiteConfig, I know UC applied a patch a while back? There was an old hack to make it work here: http://archive.ssbits.com/2-4-working-with-siteconfig/

    @MRKDevelopment - Glad it was useful for you :)

  • aragonne
    09/12/2010 9:51am (4 years ago)

    Hi Aram, Thanks for the great tutorial. Do you know how to set default values for the site config fields ... similar to the static $defaults array for DataObjects?

    I tried using static $defaults in my SiteConfig extension but the values didn't get set.

    Thanks!

  • Frank Mullenger
    21/12/2010 12:41am (4 years ago)

    @Aram Thanks, btw the line: ContentController::SiteConfig() should probably be ContentController->SiteConfig() or SiteConfig::current_site_config().

  • Aram Balakjian
    23/12/2010 12:36pm (4 years ago)

    Thanks Frank, amended! :)

  • Don St. Troy
    12/01/2011 10:12pm (4 years ago)

    This is a necessary tutorial for those using Silverstripe

  • Allan Mercado
    16/02/2011 12:00am (4 years ago)

    Hi Aram,

    Great tutorial, thanks!
    I'm using staticpublisher for a site where I'm using this idea. And I'd like to be able to regenerate the static pages when the new SiteConfig is saved.
    Is there a callback or anything that could alert me when the user clicks save on this custom Site Config?

    Cheers
    Allan

  • juneallison
    22/07/2011 3:59pm (3 years ago)

    What would I need to change in this example to make the new fields appear in a new tab?

    And out of curiosity why do you use "public function updateCMSFields(FieldSet &$fields)" instead of something like "function getCMSFields() {
    $fields = parent::getCMSFields();" ?

    Thanks for your help!

  • ilovemoon
    17/08/2011 4:02pm (3 years ago)

    Hi Aram,
    I was using the CustomSiteConfig to control content in the footer.

    When I went to test the cms as a 'content author' the footer content was editable but not saveable.
    There was no save button.

    Is the CustomSiteConfig only editable by administrators or is there something i can do to allow the content authors to edit also?

    Thanks, and thanks for ssbits too, it's been a great help :)

  • Aram Balakjian
    17/08/2011 4:04pm (3 years ago)

    Hi ilovemoon,

    There should be a permission in Security 'Edit Site Config' which will allow other groups to edit it :)

    Aram

  • ilovemoon
    17/08/2011 5:03pm (3 years ago)

    ah yes... 'Manage Site Configuration' Thank you so much :)

  • Had
    29/12/2011 11:06pm (3 years ago)

    Hi Aram,
    Thanks for this great tutorial, as ever...
    I've some troubles to have some nested tabs within the $fields parameters.
    How could I do somethings like this :
    <code>
    $fields->addFieldToTab("Root.News" ,
    new TextField("NewsTitle"));

    $fields->addFieldToTab("Root.News.Image" ,
    new ImageField("NewsImage"));
    </code>

    I've tried to add a TabSet like that :
    <code>
    $fields->addFieldToTab("Root.News",
    new TabSet("Image"));
    </code>
    But it faill. I also try to add a TabSet on the Root directly, with no success.

    Thanks for your help,
    Hadrien (CH)

  • Aram Balakjian
    30/12/2011 9:58am (3 years ago)

    Hi Had,

    try this:

    $fields->fieldByName('Root')->push(
    new TabSet('Content')
    );

    That should give you a new tabset on the root, which you can then add sub tabs to using addFieldToTab().

    Aram

  • Had
    30/12/2011 5:27pm (3 years ago)

    Thanks a lot Aram, it's works perfectly !
    :)

  • Tobias Ebert
    21/06/2012 11:22pm (3 years ago)

    Hey Aram,

    I've successfully integrated the DOM in the SiteConfig. My Problem is that I cant loop through the items in the templates. All my entries in the SiteConfig are saved, if I can trust that CMS message :).

    Here is my CustomSiteConfig.php
    =====================================================
    class CustomSiteConfig extends DataObjectDecorator {

    function extraStatics() {
    return array(
    'db' => array(
    ),
    'has_one' => array(
    ),
    'has_many' => array(
    'PromoItems' => 'GlobalPromoItem',
    'InfoItems' => 'GlobalInfoItem'
    )
    );
    }

    public function updateCMSFields(FieldSet &$fields) {

    $fields->findOrMakeTab("Root.GlobalContent", "Globale Contents");

    $fields->addFieldToTab(
    'Root.GlobalContent',
    $globalInfoItems = new ToggleCompositeField ("globalInfoItems","Infoboxes",
    new Fieldset(
    $manager = new DataObjectManager(
    SiteConfig::current_site_config(),
    'InfoItems',
    'GlobalInfoItem',
    GlobalInfoItem::$summary_fields,
    'getCMSFields_forPopup'
    )
    )
    )
    );
    $globalInfoItems->startClosed(true);
    $manager->setAddTitle("Infobox");
    $manager->setSingleTitle("Infobox");
    }

    }


    THIS IS MY DATAOBJECT:
    <?php
    class GlobalInfoItem extends DataObject
    {
    static $db = array (
    'Headline' => 'Text',
    'Description' => 'Text',
    'Title' => 'Text',
    'UrlParam' => 'Text'
    );

    static $has_one = array (
    'Pic' => 'Image',
    'SiteConfig' => 'SiteConfig'
    );

    static $summary_fields = array(
    'Headline' => 'Title',
    'Description' => 'Description',
    'UrlParam' => 'URL',
    'Pic.Url' => 'Image'
    );

    public function getCMSFields_forPopup()
    {
    return new FieldSet(
    new TextField('Headline', 'Überschrift'),
    new TextField('Description', 'Title'),
    new TextField('Title', 'Description'),
    new TextField('UrlParam', 'URL'),
    new ImageField('Pic', 'Image')
    );
    }
    }

    THIS IS IN MY TEMPLATE:
    <% control SiteConfig.InfoItems %>
    TESTOUTPUT
    <% end_control %>

    Also tested:

    <% control SiteConfig %>
    <% control InfoItems %>
    TESTOUTPUT
    <% end_control %>
    <% end_control %>

    but without any success! Maybe and hopefully you guys have an idea where the problem is, thanks for any help in advance.

  • Tobias Ebert
    21/06/2012 11:24pm (3 years ago)

    i dont get any error msg, neither any result :(

  • Tobias Ebert
    21/06/2012 11:36pm (3 years ago)

    mhm i tried a bit more and found out that

    $manager->setParentClass("SiteConfig");
    $manager->setSourceID($this->owner->ID);

    is necessary, now i can retrieve the items in the control loop :D

  • ilovemoon
    09/07/2012 4:28pm (2 years ago)

    Hi Aram,
    I recently returned to a site I hadn't touched in a few months and it seems I can no longer save the site config area. I can make changes to the fields but when I click save the save icon just keeps spinning. Firebug gives me "NetworkError: 404 Not Found - http://www.mysite.ie/admin/RootForm"
    Any thoughts on this or why it's happening now? I can edit and save the rest of the site.

Post a comment ...

You cannot post comments until you have logged in. Login Here.

Advertisement

Site of the Month

Find SSbits on

Top Contributers

Rank Avatar Name
1 article image Aram Balakjian
2 article image Daniel Hensby
3 article image Marcus Dalgren
4 article image Hamish Campbell
5 article image Ty Barho
6 article image Martijn van Nieuwenhoven
7 article image Darren-Lee
8 article image Roman Schmid
9 article image Matt Clegg
10 article image dalesaurus

View full leaderboard


Advertisement