symfony 重定向至EasyAdmin 3中包含预填充值的新操作

wtzytmuj  于 2023-11-22  发布在  其他
关注(0)|答案(2)|浏览(129)

我目前正在尝试添加一个克隆操作到我的CubeCrudController。
该操作应该重定向到Action::NEW视图,并具有一些预填充的值。
但是我不知道如何填写这张表格。
下面是我如何定义我的动作在CableCrudController中:

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

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

字符串
这就是我的cloneAction的样子,它当前重定向到Action::NEW,但没有预填充值:

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

    $clone = new Employee();
    $entity->copyProperties($clone);
    $clone->setFirstname('');
    $clone->setLastname('');
    $clone->setEmail('');

    $routeBuilder = $this->get(CrudUrlGenerator::class);
    $url = $routeBuilder->build([
            'Employee_lastname' => 'test',
            'Employee[teamMembershipts][]' => $clone->getTeamMemberships(),

        ])
        ->setController(EmployeeCrudController::class)
        ->setAction(Action::NEW)
        ->generateUrl()
    ;

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

cnwbcb6i

cnwbcb6i1#

您可以在easyAdmin中使用选项数据设置字段的值。

$builder->add('Employee_lastname', null, ['data' => $clone->getTeamMemberships()]);

字符串
如果字段有多个选项,则可以使用choices和choices_value。

xkrw2x1b

xkrw2x1b2#

例如我是如何做到的:

public function configureActions(Actions $actions): Actions
{
    $actions =  parent::configureActions($actions);

    ...

    $createRegistration = Action::new('createRegistration', 'Create registration')
        ->linkToUrl(function ($entity) {
            return $this->container->get(AdminUrlGenerator::class)
                ->setController(RegistrationCrudController::class)
                ->set('form_field_event_id', $entity->getId())
                ->setAction(Action::NEW)
                ->generateUrl();
        })
    ;

    ...

    return $actions;
}

public function configureFields(string $pageName): iterable
    {

    ...

    $eventFormTypeOptions = [];
    if($pageName === Crud::PAGE_NEW && $this->requestStack->getCurrentRequest()->get('form_field_event_id')){
        $eventFormTypeOptions['data'] = $this->eventRepository->find($this->requestStack->getCurrentRequest()->get('form_field_event_id'));
    }
    yield AssociationField::new('event')
        ->setFormTypeOptions($eventFormTypeOptions);

    ...

}

字符串

相关问题