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));
}
}
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,
]);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.