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   415 Views

Avatar
Jaymonkey

Community Member, 10 Posts

24 June 2014 at 9:51am

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

Forum Moderator, 203 Posts

25 June 2014 at 5:27pm

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

Community Member, 10 Posts

25 June 2014 at 8:13pm

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

Forum Moderator, 203 Posts

25 June 2014 at 9:41pm

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

Community Member, 41 Posts

7 July 2014 at 5:44am

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

Community Member, 53 Posts

8 July 2014 at 8:32am

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