silasjoisten/sonata-multiupload-bundle

A Bundle which allows you to enable Multiupload in Sonata

Installs: 11 757

Dependents: 0

Suggesters: 0

Stars: 26

Watchers: 4

Forks: 5

Open Issues: 2

Type:symfony-bundle

3.2.0 2019-07-23 12:48 UTC

README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License codecov

Installation

Step 1: Download the Bundle

composer require silasjoisten/sonata-multiupload-bundle

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:

<?php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new SilasJoisten\Sonata\MultiUploadBundle\SonataMultiUploadBundle(),
        );

        // ...
    }

    // ...
}

If you are using flex register bundle in config/bundles.php:

<?php

return [
    //...
    SilasJoisten\Sonata\MultiUploadBundle\SonataMultiUploadBundle::class => ['all' => true]
];

Step 3: Configuration

First you need to override the default MediaAdminController.php via:

# config/services.yaml

parameters:
    sonata.media.admin.media.controller: SilasJoisten\Sonata\MultiUploadBundle\Controller\MultiUploadController

services:
    # ...

Now add to the service definition of your Provider and add to tag multi_upload: true like:

App\Provider\VideoProvider:
    arguments:
        - 'App\Provider\VideoProvider'
        # ...
    calls:
        # ...
    tags:
        - { name: sonata.media.provider, multi_upload: true }

If you are using default Sonata MediaProvider these arguments are already injected. Otherwise be aware that the provider gets allowedMimeTypes and allowedExtensions injected. This arguments will be used by JavaScript to validate MimeType and FileExtensions before the Request starts.

For Example:

App\Provider\VideoProvider:
    arguments:
        #- 'App\Provider\VideoProvider'
        #- '@sonata.media.filesystem.local'
        #- '@sonata.media.cdn.server'
        #- '@sonata.media.generator.default'
        #- '@sonata.media.video.thumbnail'
        - ['mp4', 'mov', 'flv', 'wmv', 'mxf', 'mkv','m4v','mpg']
        - ['video/mp4', 'video/quicktime', 'video/x-flv','video/x-ms-wmv','application/mxf','video/x-matroska','video/x-m4v','video/mpeg']
        # ...

In your provider where you want to use multiupload you need to add the SilasJoisten\Sonata\MultiUploadBundle\Traits\MultiUploadTrait trait. If you dont know how to use them take a look at PHP trait definintion

In the following you see how to use the MultiUploadTrait:

namespace App\Provider;

use SilasJoisten\Sonata\MultiUploadBundle\Traits\MultiUploadTrait;

final class VideoProvider extends FileProvider
{
    use MultiUploadTrait;
    
    // ...
}

Add JavaScript and CSS to SonataAdmin config:

# config/packages/sonata_admin.yaml

sonata_admin:
    assets:
        extra_stylesheets:
            - bundles/sonatamultiupload/dist/sonata-multiupload.css

        extra_javascripts:
            - bundles/sonatamultiupload/dist/sonata-multiupload.js

OPTIONAL

# config/packages/sonata_multi_upload.yaml

sonata_multi_upload:
    max_upload_filesize: 3000000 # 3MB the default value is 0 -> allow every size

There is an option redirect_to which allows you to redirect after complete upload to your configured page.

# config/packages/sonata_multi_upload.yaml

sonata_multi_upload:
    redirect_to: 'admin_sonata_media_media_list'

HINT: You can create a controller action in your MediaAdminController which is called like createGalleryFromMultiUploadAction. The MultiUploadBundle passes automatically the id's from the uploaded medias to the redirection route for example: /foo/bar?idx=%5B70%2C71%2C72%5D so you can take them and create a gallery from uploaded medias.

In the following i provide an example:

In your MediaAdminController.php:

namespace App\Controller;

use App\Application\Sonata\MediaBundle\Admin\GalleryAdmin;
use SilasJoisten\Sonata\MultiUploadBundle\Controller\MultiUploadController;
use Sonata\MediaBundle\Entity\MediaManager;
use Sonata\MediaBundle\Entity\GalleryManager;

final class MediaAdminController extends MultiUploadController
{
    public function createGalleryAction(Request $request, MediaManager $mediaManager, GalleryManager $galleryManager, GalleryAdmin $galleryAdmin): RedirectResponse
    {
        $idx = $request->query->get('idx');
        $idx = json_decode($idx);
    
        $gallery = $galleryManager->create();
        $gallery->setName('Auto Created Gallery');
        $gallery->setEnabled(false);
        $gallery->setContext('default');
    
        foreach ($idx as $id) {
            $media = $mediaManager->find($id);
            
            $galleryHasMedia = new GalleryHasMedia();
            $galleryHasMedia->setGallery($gallery);
            $galleryHasMedia->setMedia($media);
            $gallery->addGalleryHasMedia($galleryHasMedia);
        }
    
        $galleryManager->save($gallery);
    
        return $this->redirect($galleryAdmin->generateObjectUrl('edit', $gallery));
    }
}

Maybe you need to create an alias for MediaManager and GalleryManager like:

# config/services.yaml
services:
    Sonata\MediaBundle\Entity\MediaManager:
        alias: sonata.media.manager.media

    Sonata\MediaBundle\Entity\GalleryManager:
        alias: sonata.media.manager.gallery

    App\Application\Sonata\MediaBundle\Admin\GalleryAdmin:
        alias: sonata.media.admin.gallery

Register Route in MediaAdmin.php:

protected function configureRoutes(RouteCollection $collection): void
{
    $collection->add('create_gallery', 'create/gallery/uploaded/medias');
}

And update the config accordingly:

# config/packages/sonata_multi_upload.yaml

sonata_multi_upload:
    redirect_to: 'admin_sonata_media_media_create_gallery'

This is how you can create a Gallery by uploaded Medias.

Notice that the uploader won't work for Providers like: YouTubeProvider, VimeoProvider!

4. Look & Feel

multiupload

Used Library: