frankmullenger / silverstripe-gallery

Image gallery for SilverStripe 3
23 stars 17 forks source link

How to change upload folder - update #37

Open lohe opened 8 years ago

lohe commented 8 years ago

Hi,

After intensive (re)search I have found following solution for custom upload folder names. And cause that module has not been updated for a while, it makes sense to actually change file directly. Just wanted to make sure everybody has a change to benefit from it.

  1. If there are several places you need to set up the gallery (frankmullenger/gallery), first set up one new config entry into gallery/_config/gallery.yaml
UploadFolder:
    Folders:
        <PAGE TYPE THE GALLERY GOES>: "<NEW FOLDER NAME>*"
        <PAGE TYPE THE GALLERY GOES>: "<NEW FOLDER NAME>"
        etc.
    Folder:
        - "<NEW FOLDER NAME>"

where * will have sub folders described later. IF there will not be several places, you can just remove the Folders part till Folder.

If only one place, you can set up the folder name as what ever you like or do not change yaml file at all and all will be default (but in that case you do not need the second part of that tutorial as well).

  1. Change/edit file gallery/code/Gallery.php so, that you would replace/change original updateCMSFields function as follows
    public function updateCMSFields(FieldList $fields){
        $fields -> addFieldToTab("Root.Gallery", $uploadImage = GalleryUploadField::create(
            "Images",
            "",
            $this -> owner -> OrderedImages()
        ));

        // generate upload folder name
        $folders = Config::inst() -> get("UploadFolder", "Folders");
        $_className = Controller::curr() -> currentPage() -> ClassName;
        $_ID = Controller::curr() -> currentPage() -> ID;
        $_Created = date("Y-m-d", strtotime(Controller::curr() -> currentPage() -> Created));

        $folder = Config::inst() -> get("UploadFolder", "Folder");
        $folder = (!isSet($folder) && empty($folder))?"Uploads":$folder;

        if(!empty($folders)){
            foreach($folders AS $_key => $_val){
                if($_key == $_className) $folder = ($_key == "NewsPage")?"{$_val}/{$_ID}-{$_Created}":$_val;
            }
        }
        $uploadImage -> setFolderName($folder);
    }

And cause for news page I wanted each news to be in separated folder (images) then there will be the sub folder part I mentioned before, contains name from ID and date created.

That should eventually cover all the bases - starting from multiple location, multiple upload folders, ending with single location (if yaml file will not be changed).

That's it. Thank you :)

friizu commented 8 years ago

Nice work (y)