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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Getting Parent (article) info into DataExtension.


Go to End


2 Posts   1895 Views

Avatar
-lohe-

Community Member, 5 Posts

18 March 2016 at 5:02am

Edited: 18/03/2016 5:06am

Hi.

I'm very new on SS (working on first page) and I stumbled upon very weird situation.

For news page, client wanted the gallery, so I downloaded one frankmullenger/gallery. Now that one has small thing I wanted to change without any "change" to actual module due to update possibilities: upload folder and try to make it also dynamical.

Idea is simple - use setFolderName("news/subfolder") - where subfolder is changing based on URLSegment of the specific article/page.
So I came up with solution that I created the new class inside the page code:

class GalleryModule_PageExtension extends Gallery_PageExtension{
    public function updateCMSFields(FieldList $fields){
        $fields -> removeFieldFromTab("Root.Gallery", "Images");

        $fields->addFieldToTab('Root.Gallery', $newsImage = GalleryUploadField::create(
            'Images',
            '',
            $this->owner->OrderedImages()
        ));
        $newsImage -> setFolderName("News-Images/".<WANT_URLSegment_HERE>);
    }
}

what then extends the part where files input is created so, that it will remove the default, creates new and adds the folder name (that seamed like most reasonable way to do it). So far so good (no problems there), added into yaml file of extension the necessary info for that specific class.

Now problem starts, when trying to get the URLSegment part for that specific article/news (Gallery is as a tab for that news). What ever I try, I either get an error of that method not allowed or browsers 500 error page.

Any ideas or suggestion how to resolve that issue?

Thank you.

Avatar
-lohe-

Community Member, 5 Posts

5 May 2016 at 8:06pm

Edited: 05/05/2016 8:35pm

Hi,

Considering there was no response what so ever, I decided to do things a bit differently. After extensive search in forums, I came up with following.

1. If there are several places you need to set up the gallery (that module I described 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>"
    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).

2. 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 :)