StefGuev
StefGuev

Reputation: 649

Silverstripe UploadField upload multiple image but not all at same time

I want to know if there are an extension of UploadField that allow to upload 120 images not all in same time? That cause I/O server issues not responding excessive usage. So how queue images one at time? I dont want to upload images one by one. Modules somewhere or piece of code?

with GridFieldBulkEditingTools added in my projext :

private static $many_many = array(
    'Images' => 'Image',
);

...

    $gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
    $gridFieldConfig->getComponentByType('GridFieldBulkUpload')
        ->setUfSetup('setFolderName', 'Uploads/Images/Rubriques/Galerie')
        ->setUfConfig('sequentialUploads', true);
    $gridfield = new GridFieldBulkUpload("Images", "Images", $this->Images()->sort("SortOrder"), $gridFieldConfig);

Results... blank page.

Upvotes: 1

Views: 1083

Answers (2)

wmk
wmk

Reputation: 4626

You can use a GridField for managing has_many or many_many with help of BulkEditingTools for uploading a lot of images at once.

I normally make an extra DataObject that holds the Image relation with other information (translated captions, copyright info etc...) like:

<?php
class GalleryPic extends DataObject
{
    private static $db = array(
        'Title' => 'Text',
        'Description' => 'Text',
        'Copyright' => 'Text',
        'SortOrder' => 'Int'
    );

    private static $has_one = array(
        'Attachment' => 'Image',
        'ResourcePage' => 'Page'
    );

    public function getCMSFields()
    {
        $fields = new FieldList(
            TextField::create('Title', 'Title'),
            TextareaField::create('Description', 'Desc.'),
            TextField::create('Copyright', 'Copyright / Source'),
            $imageField = UploadField::create('Attachment')
        );
        $imageField->setAllowedFileCategories('image');
        $imageField->setAllowedMaxFileNumber(1);

        return $fields;
    }
}

Then i have a DataExtension that plugs a has_many to any page type or dataobject:

<?php

class PageGallery extends DataExtension
{
    private static $has_many = array(
        'Gallery' => 'GalleryPic'
    );

    function updateCMSFields(FieldList $fields)
    {
        $conf = GridFieldConfig_RecordEditor::create(10);
        $conf->addComponent(new GridFieldSortableRows('SortOrder'));
        $conf->addComponent(new GridFieldGalleryTheme('Attachment'));
        $conf->addComponent(new GridFieldBulkUpload());
        //set upload folder if folderperroot extension is installed...
        if ($this->owner->hasMethod('getRootFolderName')) {
            $conf->getComponentByType('GridFieldBulkUpload')->setUfSetup('setFolderName', $this->owner->getRootFolderName());
        }
        $fields->addFieldToTab("Root.Bilder", Gridfield::create('Gallery', 'Gallery', $this->owner->Gallery(), $conf));

        return $fields;
    }

Add this to your page using the config yml api (e.g. to a class called MyPage):

MyPage:
  extensions:
    - 'PageGallery'

And you have your gallery upload gridfield in a seperate tab in the CMS.

I think you know how to loop over the $Gallery relation in the template.

This modules need to be installed using composer to get the above example working:

Upvotes: 2

Barry
Barry

Reputation: 3318

I'd advise using this module because it handles muliple uploads very well... I will admit I've not tried 120 items but I'm confident its the best option out there.

https://github.com/unclecheese/silverstripe-dropzone

"The Dropzone module provides FileAttachmentField, a robust HTML5 uploading interfaces for SilverStripe, allowing forms to save file uploads to DataObject instances."

Upvotes: 1

Related Questions