Symfony EasyAdmin:使用额外功能更新实际的新操作 CRUD

问题描述 投票:0回答:2

我的 easyAdmin 实际上有一个简单的 CRUD 来创建一个新实体,我想通过添加一个在创建实体后发送电子邮件的功能来自定义这个新操作。

可以自定义当我使用 symfony 命令生成 CRUD 时自动创建的新操作吗?或者我必须创建一个新的操作自定义,其中包含创建我的实体并发送电子邮件的功能?

symfony easyadmin
2个回答
1
投票

希望我的回答有用,EasyAdmin 中有一个自定义函数称为“createEntity”,类似这样:

public function createEntity(string $entityFqcn)
{
    $activity = new Activity();
    $activity->setAccount($this->getUser());

    return $activity;
}

我认为你可以在上面编写你想要的代码,它适合这个功能。


0
投票

@LubnaAltungi
响应适用于版本 <4.0. On version 4.x you can do the following:

在普通 CRUD 操作之后触发代码

行动::新

public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
{
    // Before create entity
    parent::persistEntity($entityManager, $entityInstance);
    // After create entity
}

行动::编辑

public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void
{
    // Before edit entity
    parent::updateEntity($entityManager, $entityInstance);
    // After edit entity
}

操作::删除

public function deleteEntity(EntityManagerInterface $entityManager, $entityInstance): void
{
    // Before edit entity
    parent::deleteEntity($entityManager, $entityInstance);
    // After edit entity
}

奖励:自定义原版操作按钮

例如,如果您还想自定义“保存”按钮,则可以通过这种方式创建一个模拟原版的操作。

如果您只想在一个 CRUD 或一页 CRUD 上覆盖普通按钮的文本、图标或颜色,我找到了以下解决方案:删除普通操作并使用文本和图标模拟普通操作。

$sendMessage=  Action::new('sendMessage', $this->translator->trans('entities.adver.actions.sendMessage'))
    ->setIcon('fas fa-paper-plane')
    ->setCssClass('action-'.Action::SAVE_AND_RETURN)
    ->addCssClass('btn btn-primary action-save')
    ->displayAsButton()
    ->setHtmlAttributes(['type' => 'submit', 'name' => 'ea[newForm][btn]', 'value' => Action::SAVE_AND_RETURN])
    ->linkToCrudAction(Action::NEW);

return $actions
    ->remove(Crud::PAGE_NEW, Action::SAVE_AND_RETURN)
    ->add(Crud::PAGE_NEW, $sendMessage)
    ->reorder(Crud::PAGE_NEW, [Action::INDEX, 'sendMessage']);

在 EasyAdminBundle v4 上,您可以找到以下原版操作:

vendor\easycorp\easyadmin-bundle\src\Config\Actions.php

© www.soinside.com 2019 - 2024. All rights reserved.