Symfony EasyAdmin Bundle Custom Action Modal

130 views Asked by At

I wanted to transform my addflash into a modal on the dashboard to show the winner of the giveaway, I've been trying so hard,but I actually can't do it because of the lack of documentation. I've never used stackoverflow before, but I'm actually so stuck I need help

Here is the CrudController, I've tried experiencing javascript and twig template, using chatgpt, looking from the actual documentation and read lessons, but nothing is about a global action modal. Can't find any documentations, trying to get parent twig from easyadmin bundle ect but I haven't understood everything


`<?php

namespace App\Controller\Admin;

use App\Entity\NewsletterUserlist;
use Doctrine\ORM\EntityManager;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface;

class NewsletterUserlistCrudController extends AbstractCrudController
{

    public static function getEntityFqcn(): string
    {
        return NewsletterUserlist::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            IdField::new('id')->hideOnForm(),
            EmailField::new('mail'),
            DateTimeField::new('created_at')->hideOnForm(),
        ];
    }

    public function configureCrud(Crud $crud): Crud
    {
        return $crud
            ->renderContentMaximized()
            ->renderSidebarMinimized()
            ->setEntityLabelInSingular('Email')
            ->setEntityLabelInPlural('Newsletter');
    }

    public function configureActions(Actions $actions): Actions
    {
        $exportAction = Action::new('giveaway')
            ->linkToCrudAction('giveaway')
            ->addCssClass('btn btn-success')
            ->setIcon('fa fa-download')
            ->createAsGlobalAction();

        return parent::configureActions($actions)
            ->add(Crud::PAGE_INDEX, $exportAction);
    }


    public function giveaway(AdminContext $context, AdminUrlGenerator $adminUrlGenerator, EntityManagerInterface $em)
    {
        $randomUser = $em->getRepository(NewsletterUserlist::class)->findRandomUser();
        $randomUserMessage = 'Ceci est un test avec l\'utilisateur : ' . $randomUser->getMail();
        $this->addFlash('success', 'Félicitation à ' . $randomUser->getMail());


        $url = $adminUrlGenerator
            ->setAction(Action::INDEX)
            ->removeReferrer()
            ->setController($context->getCrud()?->getControllerFqcn() ?? '')
            ->generateUrl();

        return $this->redirect($url);

    }
}
`
0

There are 0 answers