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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

CMS Field for setting that will be the same on every Page


Go to End


5 Posts   1993 Views

Avatar
sebastiankozub

Community Member, 59 Posts

18 August 2015 at 8:11am

I'm working on the website that will be displaying 5 logos of cooperatives on every page's footer. But I'd like to have it changeable in CMS.
What is the best solution to have it like some kind of general settings, not only specific page setting?

Avatar
sebastiankozub

Community Member, 59 Posts

18 August 2015 at 9:04am

Avatar
helenclarko

Community Member, 166 Posts

18 August 2015 at 9:10am

Edited: 19/08/2015 4:25pm

Hi Sebastiankozub,

There is multiple ways to do this.

I guess the first thing to do is extend SiteConfig (Settings tab) and add a footer tab.
Then you have the option of simply adding 5 upload fields for you to add images to.
Alternatively I would recommend looking into creating gridfields, as these are far easier to maintain and far less messy.

Gridfield docs:
https://docs.silverstripe.org/en/3.1/developer_guides/forms/field_types/gridfield

Okay, first you will need to create a CustomSiteConfig.php in your "mysite/code" folder.
This will allow you to add tabs/fields to your Settings tab within the CMS.

CustomSiteConfig.php

<?php
class CustomSiteConfig  extends DataExtension {

	 private static $db = array(
	);
	 private static $has_many = array(
		'Images' => 'Image',
	);
	
    function getCMSFields() {

		$fields = parent::getCMSFields(); 
		
		//Footer content
		$fields->addFieldToTab(
				"Root.FooterImages", 
				$uploadField = new UploadField('Images', 'Upload one or more images (max 5 in total)')); 
				$uploadField->allowedExtensions = array('jpg', 'gif', 'png');
				$uploadField->setAllowedMaxFileNumber(5);
		return $fields;
	}
}

This is simply one way of adding images, once again I would recommend looking into Gridfields and using one of those.

You will then need to add the following into your _config.php file (mysite/_config.php), this will tell the CMS that CustomSiteConfig.php can be used to extend SiteConfig.

SiteConfig::add_extension('CustomSiteConfig');

Alternatively, you can do the same thing in your .yml (mysite/_config/config.yml) file, by adding the following.

SiteConfig:
  extensions:
    - CustomSiteConfig

Then the last step is to add your images into your template.

<% loop SiteConfig %>
	<% if Image %>
		<% loop Image %>
			<div style="float: left;">
				<img src="$PaddedImage(72,72).URL"  width="72px" height="72px"/>
			</div>
		<% end_loop %>
	<% end_if %>
<% end_loop %>

This should give you the result you want.

As always, /dev/build?flush=1 and...
Profit???

-helenclarko

Avatar
sebastiankozub

Community Member, 59 Posts

19 August 2015 at 3:42am

thanks

Avatar
sebastiankozub

Community Member, 59 Posts

15 October 2015 at 9:54am

Your solution with UploadField works perfect. I even extend with Photo that extends Image and has one CustomSiteConfig relation that allows me also to remove the images from the UploadField. Now I'd like to extend those photos to give them little description that will appear over the image (alt attribute) on mouse over.
But I stack in the problem and I describe it here:
http://www.silverstripe.org/community/forums/customising-the-cms/show/107916
Could anyone help me?