19
Apr
18
2.4 - Working with SiteConfig
Requirements
v2.4 +
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:
Or alternatively:
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:
Or alternatively:
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
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 inorder to get SilverStripe to recognize this extention we need to add this line to our _config.php:
Now in our template we can use these new fields like so:
Using DataObjectManager (and ComplextableField) in SiteConfig
So although this should be as simple as using the DataObjectManager on a page, because we are effectively relating 2 DataObjects instead of a page and a DataObject, for some reason SilverStripe is unable to set the ParentID on the DataObject and so it doesn't work. There is however a workaround which is to extend the DataObjectManager class and add a function to explicitly set the sourceID. So to do this all we need to do is create a new class after our CustomSiteConfig class. We could do this in a seperate file, but as it is only used for the SiteConfig it makes sense to keep it there. We also need to adjust our DataObjectManger definition from usual to set the sourceID and make sure it picks up the correct controller:
CustomSiteConfig.php
So as you can see instead of using $this we use $this->owner to set the DOM controller. We also use the SiteConfig_DataObjectManager class that we define at the bottom so that we can call our custom setSourceID() function and set the sourceID to $this->owner->ID, which is the ID of the SiteConfig.
Special thanks to Dan Hensby for providing the DOM extension code. You can see the forum post here.
So there you have it, the new SiteConfig. Very useful and easy when you know how! :)
Thanks to Johannes Fischer for suggesting this post.
About the Author
Name: Aram Balakjian
Website: http://www.aabweb.co.uk
Aram is a web designer/developer running London based agency aab web. He has a strong passion for developing attractive, usable sites around the SilverStripe CMS.
Comments (10)
-
http://http://www.findsoso.com/
Our website wholesale for many kinds of fashion clothing, such
as nike,jordan,prama,****, we also sell jeans,shirts,bags,hat and
many kinds of tiffany. All the products are free shipping, and the
the price is enticement , and also can accept the paypal
payment.we can ship within 24 hours after your payment.
accept the paypal
free shipping
competitive price
any size available
Everything is here http://www.findsoso.com friends, please
enjoy viewing bar
ThanksPosted by xihuan, 27/09/2010 10:22am (3 years ago)
-
http://http://www.findsoso.com/
Our website wholesale for many kinds of fashion clothing, such
as nike,jordan,prama,****, we also sell jeans,shirts,bags,hat and
many kinds of tiffany. All the products are free shipping, and the
the price is enticement , and also can accept the paypal
payment.we can ship within 24 hours after your payment.
accept the paypal
free shipping
competitive price
any size available
Everything is here http://www.findsoso.com friends, please
enjoy viewing bar
ThanksPosted by xihuan, 27/09/2010 10:13am (3 years ago)
-
march
Posted by coach outlet, 16/09/2010 2:32am (3 years ago)
-
march
Posted by louis vuitton bags, 16/09/2010 2:01am (3 years ago)
-
jiemo
Posted by nike air max, 16/09/2010 1:22am (3 years ago)
-
Thanks for this tutorial, awesome job! I have a question: why is it that in the view I could not get a summary using for example:
$SiteConfig.Spiel.Summary(20)
But instead this worked:
<% control SiteConfig.Spiel %>
$Summary(20)
<% end_control %>Posted by Frank, 26/08/2010 3:58pm (3 years ago)
-
Hi, is it possible to do an if test on SiteConfig??
I'm trying to do something like this in a template (for a multisite setup):
<% if SiteConfig.ID %> etc...
but i just get a blank screen.Posted by nick, 25/08/2010 1:21am (3 years ago)
-
Hey I\'ve tried your solution but I get error: could not find class for CustomSiteConfig (even thought php file is in mysite/code, I used your naming also). Any ideas?
using: SS 2.4.1Posted by Jake, 06/08/2010 4:54am (3 years ago)
-
...ment aram, not aaron, sry
Posted by max, 25/06/2010 3:02am (3 years ago)
-
thanks for this one, aaron. silverstripe staff is currently evaluating it's documentation, your ssbits collection is a good point of reference :)
Posted by max, 25/06/2010 3:01am (3 years ago)
RSS feed for comments on this page RSS feed for all comments
Post your comment
Comments have been disabled. Please visit this post on SSBits.com to post your comment