2017 © Pedro Peláez
 

symfony-bundle sonata-multiupload-bundle

A Bundle which allows you to enable Multiupload in Sonata

image

silasjoisten/sonata-multiupload-bundle

A Bundle which allows you to enable Multiupload in Sonata

  • Thursday, July 19, 2018
  • by silasjoisten
  • Repository
  • 3 Watchers
  • 8 Stars
  • 545 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 2 Open issues
  • 9 Versions
  • 1085 % Grown

The README.md

silasjoisten/sonata-multiupload-bundle

Build Status Latest Stable Version Total Downloads Latest Unstable Version License codecov, (*1)

Versions

Sample

Checkout the Sample Project, (*2)

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:, (*3)

<?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:, (*4)

<?php

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

Step 3: Configuration

You have to open the configuration file for this bundle and configure the providers which you want to enable multi upload., (*5)

# config/packages/sonata_multi_upload.yaml

sonata_multi_upload:
  # ...
  providers:
    - sonata.media.provider.image
    - sonata.media.provider.video

Add JavaScript and CSS to SonataAdmin config:, (*6)

# 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., (*7)

# config/packages/sonata_multi_upload.yaml

sonata_multi_upload:
  redirect_to: 'admin_sonata_media_media_list'

HINT: The MultiUploadBundle passes automatically the id's from the uploaded Media objects 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., (*8)

Create controller

The controller takes your request and create in this example a Gallery with GalleryItems and redirects to the edit view of GalleryAdmin, (*9)

<?php

namespace App\Controller;

use App\Entity\SonataMediaGallery;
use App\Entity\SonataMediaGalleryItem;
use Sonata\MediaBundle\Admin\GalleryAdmin;
use Sonata\MediaBundle\Entity\MediaManager;
use Sonata\MediaBundle\Entity\GalleryManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

final class CreateGalleryAction
{
    public function __construct(
        private MediaManager   $mediaManager,
        private GalleryManager $galleryManager,
        private GalleryAdmin   $galleryAdmin
    ) {
    }

    public function __invoke(Request $request): RedirectResponse
    {
        $idx = $request->query->get('idx');
        $idx = json_decode($idx);

        /** @var SonataMediaGallery $gallery */
        $gallery = $this->galleryManager->create();
        $gallery->setName('Auto Created Gallery');
        $gallery->setEnabled(false);
        $gallery->setContext('default');

        foreach ($idx as $id) {
            $media = $this->mediaManager->find($id);

            $galleryHasMedia = new SonataMediaGalleryItem();
            $galleryHasMedia->setGallery($gallery);
            $galleryHasMedia->setMedia($media);
            $gallery->addGalleryItem($galleryHasMedia);
        }

        $this->galleryManager->save($gallery);

        return new RedirectResponse($this->galleryAdmin->generateObjectUrl('edit', $gallery));
    }
}

Register route

If you already override the default MediaAdmin you can add the route in the admin class via, (*10)

   protected function configureRoutes(RouteCollectionInterface $collection): void
    {
        $collection->add('create_gallery', 'multi-upload/create-gallery', [
            '_controller' => CreateGalleryAction::class,
        ]);
    }

otherwise you can create an AdminExtension like the following:, (*11)

<?php

declare(strict_types=1);

namespace App\Admin\Extension;

use App\Controller\CreateGalleryAction;
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Route\RouteCollectionInterface;

final class MediaAddRouteExtension extends AbstractAdminExtension
{
    public function configureRoutes(AdminInterface $admin, RouteCollectionInterface $collection): void
    {
        $collection->add('create_gallery', 'multi-upload/create-gallery', [
            '_controller' => CreateGalleryAction::class,
        ]);
    }
}

and register this extension in your config/services.yaml, (*12)

services:
    # ...
    App\Admin\Extension\MediaAddRouteExtension:
        tags:
            - { name: sonata.admin.extension, target: sonata.media.admin.media }

Now configure the redirect_to in config/packages/sonata_multi_upload.yaml, (*13)

sonata_multi_upload:
    redirect_to: 'admin_app_sonatamediamedia_create_gallery'

Maybe you need to create an alias for MediaManager and GalleryManager like:, (*14)

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

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

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

Thats it., (*15)

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

4. Look & Feel

multiupload, (*17)

Used Library: * jQuery Upload, (*18)

The Versions