Hey guys,
I would like to know how I can make it that when I upload images through my custom Page (ie Categories) in the CMS that I can then have the images saved in assets/categories/imageX.jpg
Thanks
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
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.
Hey guys,
I would like to know how I can make it that when I upload images through my custom Page (ie Categories) in the CMS that I can then have the images saved in assets/categories/imageX.jpg
Thanks
Do you mean you want to save them in a specific folder other than the default /Uploads/ folder (/categories/) or in a specific name (/imageX.jpg)?
Yes, in a specific folder but a good simple solution. I am not worried about the name since that is pretty straight forward.
OK, this might seem a not so simple solution, but in the end: it is pretty easy.
First a small explanation:
Most likely you're using a field from the type ImageField. This can be found in sapphire/forms/ImageField.php
If you open that, you'll see it's a subclass of filefield (ImageField extends FileField). This means: it's using all the features of sapphire/forms/FileField.php unless those features are overridden in ImageField.php
Well... what the SilverStripe devs are doing, can easily be done by yourself (avoid messing around in the core) :D
1) Make a new folder in the mysite-folder, for example: mysite/formfields/
2) Create in that folder a file like myImageField.php
3) Write in the first two lines:
<?php
class myImageField extends ImageField{
protected $folderName = 'Uploads';
}
?>
That should be all.
BTW: I have never tried replacing the default-folder. However I changed the allowedMaxFileSize and that worked fine.
If it doesn't work, please post it here.
Umm,
Thanks. That seems like a very simple solution.
I guess if I want to make the folder dynamic such as make the folder be the name of the page's url segment then I should be able to replace that with the code to allow for that. I just hope the constructor works correctly in order to allow for that to work otherwise I will need to pass a variable into some method.
What do you think?
It's worth a try. And if it doesn't work, some kind of method could work.
I'm not sure though if I would use the URLSegment.
The nice part of the ImageField is that you can reuse images. And my suspicions are that they won't move into another folder if you reused an image from let's say the About-us page on the Contact-us page. So in that case the logic of the image-part of your assets-folder would get a terrible mess (let alone the number of folders you could get)
Ordering them dynamic by ClassName sounds better to me. But I would prefer it more or less hardcoded by function (banner, sidebar etc) or just a plain Images-folder.
By the way: whatever you are planning, I would test it with a hardcoded Images-folder before I would start any coding. Remember: I didn't test it myself.
<input class="hidden" type="hidden" id="Form_EditImageForm_folderName" name="folderName" value="Uploads/projects" />
I tried this out so many different ways and I can see from the souce code that the foldername is being changed but the form still always saves it to uploads, so frustrating. Not sure what I am missing out on here but any advice would be really appreciated
Cheers
Will
If this is on your custom page then you have defined a custom getCMSFields() to add the image field. You can pass the folder name to this image field and it should use that dir rather then the built in one.
new ImageField($name, $title, '', '', '', $folderName = null);