如何在 EasyAdmin 3 中添加自定义操作?

How to add a custom action in EasyAdmin 3?

我的实体 Participant 有一个 CrudController。我想添加自定义操作 sendAcknowledgementEmail。 EasyAdmin docs 没有提及自定义函数参数或 return 值。

我有以下代码

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?
}

到目前为止,EasyAdmin 检测到自定义函数,但我收到错误消息“控制器必须 return 一个“Symfony\Component\HttpFoundation\Response”对象,但它 return 为空。你忘了吗在控制器的某处添加 return 语句?"

我如何从这里继续?

浏览完 EasyAdmin AbstractCrudController 后,我想到了以下工作代码。

  • 为了获取当前对象,您需要参数 AdminContext
  • 对于我的用例,我想 return 到 CrudController 索引操作,为此我可以执行 redirect.

注意:您需要在构造函数控制器中注入 CrudUrlGenerator 服务。

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

我当前的函数如下所示:

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

我当前的工作代码

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

捆绑包的 v3.x 很新,文档还不完善。

根据 Ceochronos 的回答,这是我对克隆操作的实现。

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