Reputation: 137
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);
}
Upvotes: 8
Views: 2210
Reputation: 11
Example how i do that:
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);
...
}
Upvotes: 1
Reputation: 564
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.
Upvotes: -1