6

I am currently trying to add a Clone action to my EmployeeCrudController.
The action should redirect to the Action::NEW view and have some prefilled values.
However I can't figure out how to prefill this form.

Here is where how I define my action within the EmployeeCrudController:

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

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

And this is how my cloneAction looks like, which currently redirects to the Action::NEW as expected but without prefilled values:

 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);
 }
EriCreator
  • 117
  • 1
  • 13

1 Answers1

0

You can set the value of a field in easyAdmin using the option data.

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

If your field has multiple options, you can use the choices and choices_value.

Christos Lytras
  • 36,310
  • 4
  • 80
  • 113