PHP code example of silasjoisten / sonata-multiupload-bundle

1. Go to this page and download the library: Download silasjoisten/sonata-multiupload-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

silasjoisten / sonata-multiupload-bundle example snippets




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

        // ...
    }

    // ...
}
 


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



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));
    }
}


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



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,
        ]);
    }
}