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.

General Questions /

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

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

Setting the parent folder of an image


Go to End


3 Posts   1138 Views

Avatar
codemonkey88

Community Member, 24 Posts

29 November 2014 at 12:22am

Edited: 29/11/2014 12:22am

I am using a has_one to assign a banner to a page. To try and keep things organised, I am using onBeforePublish to drop the image into a banners folder. The issue is, if I publish the page without uploading an image, then a blank record labeled "new image" appears on the assets page. I've added all the conditionals I can think of, but the blank records are still being created. Any suggestions?

if($this->Banner())
{
$banner = $this->Banner();
$folder = Folder::findOrMake("Uploads/banner");
$banner->setParentID($folder->ID);
	if($banner->Title != "new image")
	{
	$banner->write();
	}
}

Avatar
kinglozzer

Community Member, 187 Posts

29 November 2014 at 1:53am

Can’t you instead set the folder name on the UploadField?

$uploadField = UploadField::create('Banner', 'Banner image');
$uploadField->setFolderName('Uploads/banner');

I’m not sure why your check would be failing, perhaps try:

if($this->Banner() && $this->Banner()->exists())

Avatar
codemonkey88

Community Member, 24 Posts

29 November 2014 at 2:08am

Edited: 29/11/2014 2:15am

Thanks.
I probably should have mentioned, I'm oldschool and use FileIframeField (largely because I find the backend on SS3 to slugish). I will try "if exists" though, I hadn't thought of that.

---

Actually, just had a dig in the documentation, and setFolderName is a method of FileField, so could try that.