Jump to:

22977 Posts in 11758 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Image subfolder

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

Page: 1 2
Go to End
Author Topic: 3133 Views
  • Terminator4
    Avatar
    Community Member
    81 Posts

    Image subfolder Link to this post

    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

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: Image subfolder Link to this post

    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)?

  • Terminator4
    Avatar
    Community Member
    81 Posts

    Re: Image subfolder Link to this post

    Yes, in a specific folder but a good simple solution. I am not worried about the name since that is pretty straight forward.

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: Image subfolder Link to this post

    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)

    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{


    4) Now look in ImageField.php and FileField.php if you can find a place where the defaultfolder is set. It happens to be in FileField.php with the following line of code (line 64):

    protected $folderName = 'Uploads';


    5) Copy that line to your myImageField.php -file and paste it in there. Replace 'Uploads' with 'Categories'.
    6) Close everything with these two lines:

    }
    ?>


    7) Instead of calling new ImageField in mysite/code/Categories.php, you should now call new myImageField.
    8) Don't forget to run a /db/build

    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.

  • Terminator4
    Avatar
    Community Member
    81 Posts

    Re: Image subfolder Link to this post

    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?

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: Image subfolder Link to this post

    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.

  • vancouverWill
    Avatar
    Community Member
    121 Posts

    Re: Image subfolder Link to this post

    <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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Image subfolder Link to this post

    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);

    3133 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.