Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Custom Site Config!


Reply

6 Posts   317 Views

Avatar
Jaymonkey

24 June 2014 at 9:51am Community Member, 10 Posts

Hi,

I am trying to create some site wide content which will reside in the footer area of my site. I have created a DataExtension to extend siteconfig, which includes a DataObject as this is basically for sponsor images and you need to be able to add and remove as many as necessary.

From the backend I seem to have everything working, but don't seem to be able to access the data through the template, here is my code:

CustomSiteConfig.php

<?php
class CustomSiteConfig extends DataExtension {

private static $has_many = array(
'Sponsors' => 'Sponsor'
);

public function updateCMSFields(FieldList $fields) {

$config = GridFieldConfig_RelationEditor::create();

$config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'Link' => 'Link'
));

$sponsorfield = new GridField(
'Sponsors',
'Sponsor',
new DataList('Sponsor'),
$config
);

$fields->addFieldToTab('Root.Main', $sponsorfield);
}

}

?>

Sponsor.php

<?php
class Sponsor extends DataObject {

static $db = array (
'Link' => 'text',
);

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

public function getCMSFields() {
$sizeMB = 2; // 2 MB
$size = $sizeMB * 1024 * 1024; // 2 MB in bytes

$uploadField = new UploadField('Image', 'Sponsor Image');

$uploadField->getValidator()->setAllowedMaxFileSize($size);

$fields = new FieldList(
new TextField('Link'),
$uploadField
);

return $fields;
}
}

?>

I have also updated the file in /framework/_config/config.yml to read:

SiteConfig:
extensions:
- CustomSiteConfig

In my template I am trying to access the data like this:

<% loop $SiteConfig.Sponsors %>
   
      <p>$Link</p>
   
<% end_loop %>

Unfortunately this is not working for me, any ideas anyone?

Many Thanks

Avatar
camfindlay

25 June 2014 at 5:27pm Forum Moderator, 149 Posts

Something to check would be to ensure your yaml config file is using 2 spaces indentation and that you have build the database with dev/build and run a flush.

Avatar
Jaymonkey

25 June 2014 at 8:13pm Community Member, 10 Posts

Hey Camfindlay,

Thanks for your suggestion, the .yaml file is correctly indented and I did rebuild and flush! Still no joy here!

I was wondering if I am trying to access the data in the template the correct way?

Thanks,

Jason

Avatar
camfindlay

25 June 2014 at 9:41pm Forum Moderator, 149 Posts

Yep I believe you are doing it correctly, I have just tried the code on a fresh 3.1.x core code and got it to work.

Here is my code example + a few changes as to how I would have approached it. Let me know if you have any questions.

https://gist.github.com/camfindlay/af531b60502888b07529#file-customsiteconfig-php-L13

Avatar
okotoker

7 July 2014 at 5:44am Community Member, 41 Posts

Usually for something like this, I would just use ModelAdmin. Easier and clearer I think. For a client going into a sponsors section makes more sense than going into settings.

Avatar
martbarr

8 July 2014 at 8:32am Community Member, 46 Posts

I normally add some footer tabs to my home page and write a getter....