7

I have a CrudController for my entity, Participant. I want to add a custom action, sendAcknowledgementEmail. The EasyAdmin docs doesn't mention anything about the custom function parameters or return values.

I have the following code

public function configureActions(Actions $actions): Actions
{
    $send_acknowledgement_email = Action::new('sendAcknowledgementEmail', 'Send Acknowledgement Email', 'fa fa-send')
        ->linkToCrudAction('sendAcknowledgementEmail');

    return $actions
        ->add(Crud::PAGE_INDEX, $send_acknowledgement_email)
        ->add(Crud::PAGE_EDIT, $send_acknowledgement_email)
    ;
}

public function sendAcknowledgementEmail() //Do I need parameters?
{
    //How do I get the Entity?

    //What should I return?
}

So far, EasyAdmin detects the custom function but I get an error "The controller must return a "Symfony\Component\HttpFoundation\Response" object but it returned null. Did you forget to add a return statement somewhere in your controller?"

How do I continue from here?

ceochronos
  • 350
  • 1
  • 9
  • 22

2 Answers2

11

The v3.x of the bundle is quite new and the documentation is not perfect yet.

Based on Ceochronos answer, here is my implementation for a clone action.

public function configureActions(Actions $actions): Actions
{
    $cloneAction = Action::new('Clone', '')
        ->setIcon('fas fa-clone')
        ->linkToCrudAction('cloneAction');

    return $actions
        ->add(Crud::PAGE_INDEX, $cloneAction);

}

public function cloneAction(AdminContext $context)
{
    $id     = $context->getRequest()->query->get('entityId');
    $entity = $this->getDoctrine()->getRepository(Product::class)->find($id);

    $clone = clone $entity;

    // custom logic 
    $clone->setEnabled(false);
    // ...
    $now = new DateTime();
    $clone->setCreatedAt($now);
    $clone->setUpdatedAt($now);

    $this->persistEntity($this->get('doctrine')->getManagerForClass($context->getEntity()->getFqcn()), $clone);
    $this->addFlash('success', 'Product duplicated');

    return $this->redirect($this->get(CrudUrlGenerator::class)->build()->setAction(Action::INDEX)->generateUrl());
}
Kaizoku Gambare
  • 3,143
  • 3
  • 29
  • 41
  • 3
    Thanks! It saved my day. I have changed a bit because I prefer the user being redirected to edit page of the new entity: `return $this->redirect($this->get(CrudUrlGenerator::class)->build()->setAction(Action::EDIT)->setEntityId($clone->id)->generateUrl());` – senechaux Jan 05 '21 at 15:39
  • You should contribute this to the documentation. Thank you. – Aries Apr 06 '22 at 19:29
3

After browsing through the EasyAdmin AbstractCrudController I came up with the following working code.

  • In order to get the current object you need the parameter AdminContext
  • For my use case I want to return to the CrudController index action, so for that I can do a redirect.

Note: you need to inject the CrudUrlGenerator service in your constructor controller.

public function sendAcknowledgementEmail(AdminContext $context)
{
    $participant = $context->getEntity()->getInstance();

    // Your logic

    $url = $this->crudUrlGenerator->build()
        ->setController(ParticipantCrudController::class)
        ->setAction(Action::INDEX)
        ->generateUrl();

    return $this->redirect($url);
}

My current function looks like this:

public function sendAcknowledgementEmail(AdminContext $context)
{
    $participant = $context->getEntity()->getInstance();

    $participant->sendAcknowledgementEmail();

    $this->addFlash('notice','<span style="color: green"><i class="fa fa-check"></i> Email sent</span>');

    $url = $this->crudUrlGenerator->build()
        ->setController(ParticipantCrudController::class)
        ->setAction(Action::INDEX)
        ->generateUrl();

    return $this->redirect($url);
}

My current working code

<?php

namespace App\Controller\Admin;

use App\Service\WebinarService;
use EasyCorp\Bundle\EasyAdminBundle\Router\CrudUrlGenerator;
use Symfony\Contracts\Translation\TranslatorInterface;
// ...

class ParticipantCrudController extends AbstractCrudController
{

    private CrudUrlGenerator $crudUrlGenerator;
    private WebinarService $webinar_service;
    private TranslatorInterface $translator;

    public function __construct(CrudUrlGenerator $crudUrlGenerator, WebinarService $webinar_service, TranslatorInterface $translator)
    {
        $this->crudUrlGenerator = $crudUrlGenerator;
        $this->webinar_service = $webinar_service;
        $this->translator = $translator;
    }

    // ...

    public function sendAcknowledgementEmail(AdminContext $context): Response
    {
        $participant = $context->getEntity()->getInstance();

        try {
            $this->webinar_service->sendAcknowledgementEmail($participant);

            $this->addFlash('notice', 'flash.email.sent');
        } catch (Exception $e) {
            $this->addFlash('error', $this->translator->trans('flash.error', ['message' => $e->getMessage()]));
        }

        $url = $this->crudUrlGenerator->build()
            ->setController(ParticipantCrudController::class)
            ->setAction(Action::INDEX)
            ->generateUrl()
        ;

        return $this->redirect($url);
    }
}
ceochronos
  • 350
  • 1
  • 9
  • 22
  • Could you please add the constructor code as well? – Quatsch Jul 07 '21 at 15:47
  • 1
    @Quatsch there you are! – ceochronos Jul 08 '21 at 01:49
  • 2
    Thank you! In the meantime I figured out that since EasyAdmin 3.2.0 they changed it to [AdminUrlGenerator](https://symfony.com/doc/current/bundles/EasyAdminBundle/crud.html). The implementation and usage is very similar though. – Quatsch Jul 08 '21 at 05:05