【问题标题】:How to add a custom action in EasyAdmin 3?如何在 EasyAdmin 3 中添加自定义操作?
【发布时间】:2023-03-08 02:54:02
【问题描述】:

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

我有以下代码

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 检测到自定义函数,但我收到错误消息“控制器必须返回“Symfony\Component\HttpFoundation\Response”对象,但它返回 null。您是否忘记在控制器的某处添加 return 语句? "

我该如何从这里继续?

【问题讨论】:

  • 一年后,EasyAdmin 3 仍然没有合适的文档。
  • 已经一年了!?时间过得真快。我的项目成功了,从那以后我就再也没有碰过那个 repo。

标签: symfony easyadmin


【解决方案1】:

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

  • 为了获取当前对象,您需要参数 AdminContext
  • 对于我的用例,我想返回 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);
    }
}

【讨论】:

  • 能否请您也添加构造函数代码?
  • @Quatsch 你来了!
  • 谢谢!与此同时,我发现从 EasyAdmin 3.2.0 开始,他们将其更改为 AdminUrlGenerator。但实现和使用非常相似。
【解决方案2】:

捆绑包的 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());
}

【讨论】:

  • 谢谢!它拯救了我的一天。我做了一些改变,因为我更喜欢将用户重定向到新实体的编辑页面:return $this-&gt;redirect($this-&gt;get(CrudUrlGenerator::class)-&gt;build()-&gt;setAction(Action::EDIT)-&gt;setEntityId($clone-&gt;id)-&gt;generateUrl());
猜你喜欢
  • 1970-01-01
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多