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

Image field in CMS


Go to End


4 Posts   1458 Views

Avatar
mhdesign

Community Member, 216 Posts

5 December 2014 at 5:05pm

Edited: 05/12/2014 5:25pm

Despite reading and re-reading tutorial http://doc.silverstripe.org/framework/en/tutorials/2-extending-a-basic-site I can't see what I missed... but I think it's probably fairly fundamental...

I want to add a field (Jack) to my page type. So page.php says:

class Page extends SiteTree {

	private static $db = array();

	private static $has_one = array('SingleImage' => 'Image'								
	);
	
function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab('Root.Jack',$uploadField = new UploadField($name = 'Jack', $title = 'JackRussell'));
		return $fields;
	}
}

And on my page I call the image:
$Jack

Can upload image in CMS using field 'Jack'. But no image on the page?!

Did I miss anything?

Avatar
kinglozzer

Community Member, 187 Posts

5 December 2014 at 11:55pm

Hi Arthur,

Your has_one relation is named "SingleImage", but you're passing the name "Jack" to the form field (the first parameter). You need to either rename your has_one from "SingleImage" to "Jack", or pass "SingleImage" as the first parameter when you create the UploadField.

The same in the template: you're trying to get a relation named "Jack" when this doesn't exist - "SingleImage" is what the relation is named, so either use $SingleImage in the template or rename the has_one to "Jack".

Hope this helps,
Loz

Avatar
mhdesign

Community Member, 216 Posts

6 December 2014 at 1:03pm

Sweet! That cracked it!

Silly mistake of mine -- thanks for your help!

Avatar
mhdesign

Community Member, 216 Posts

10 December 2014 at 11:14am

Edited: 10/12/2014 11:16am

Now trying to get three image positions that client can load random images into!

class Page extends SiteTree {

	private static $db = array(							   
	);

	private static $has_one = array (
									 'JackLeft' => 'Image',
									 'JackCenter' => 'Image',
									 'JackRight' => 'Image'
	);
	
function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab('Root.Jack Left',$uploadField = new UploadField($name = 'JackLeft', $title = 'Dr Jack Russell (left column)'));
		$fields->addFieldToTab('Root.Jack Center',$uploadField = new UploadField($name = 'JackCenter', $title = 'Dr Jack Russell (page center)'));
		$fields->addFieldToTab('Root.Jack Right',$uploadField = new UploadField($name = 'JackRight ', $title = 'Dr Jack Russell (right column)'));
		return $fields;
	}
}

And on Page.ss:

<div id="jack" class="row">
	<div class="col_3 first">$JackLeft</div>
	<div class="col_3">$JackCenter</div>
	<div class="col_3 last">$JackRight</div>
</div>

But no image? Also I need to get rid of 'alt' tag on these because it shows "assets/" when an image is not loaded... client will only use one position per page.

Any tips?