I need to create a view so that the user can edit his profile, but it cannot be the same edit view because there are fields that only the user can access in session, such as the password and no other user, not even a admin can change the password, among other fields. I wanted to use all the strength that easyadmin has for this. This is my code:
class DashboardController extends AbstractDashboardController
{
public function configureUserMenu(UserInterface $user): UserMenu
{
// ...
$profileUrl = $this->get(CrudUrlGenerator::class)
->build()
->setAction('profile')
->setController("App\\Controller\\UserCrudController")
->setEntityId($user->getId())
->includeReferrer(true)
->generateUrl();
$menus->addMenuItems([
MenuItem::linkToUrl('Profile', 'fa fa-user', $profileUrl),
]);
return $menus;
}
}
class UserCrudController extends AbstractCrudController
{
public function configureFields(string $pageName): iterable
{
// ...
if ($pageName == 'profile'){
$fields [] = TextField::new('password', 'Current Password')
->setFormType(PasswordType::class)
->setRequired(true)
;
$fields[] = Field::new('plainPassword', 'New password')->onlyOnForms()
->setFormType(RepeatedType::class)
->setFormTypeOptions([
'type' => PasswordType::class,
'first_options' => ['label' => 'New password'],
'second_options' => ['label' => 'Repeat password'],
]);
}
}
//NOTE: At the moment I have copied and pasted the edit code to see the view that it shows and modify it. But I get the error shown below
public function profile(AdminContext $context)
{
$event = new BeforeCrudActionEvent($context);
$this->get('event_dispatcher')->dispatch($event);
if ($event->isPropagationStopped()) {
return $event->getResponse();
}
if (!$this->isGranted(Permission::EA_EXECUTE_ACTION)) {
throw new ForbiddenActionException($context);
}
if (!$context->getEntity()->isAccessible()) {
throw new InsufficientEntityPermissionException($context);
}
$context->getEntity()->setInstance($this->createEntity($context->getEntity()->getFqcn()));
$this->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_NEW)));
$this->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
$user = $this->getUser();
$newForm = $this->createForm(User::class, $user);
$newForm->handleRequest($context->getRequest());
$entityInstance = $newForm->getData();
$context->getEntity()->setInstance($entityInstance);
if ($newForm->isSubmitted() && $newForm->isValid()) {
$this->processUploadedFiles($newForm);
$event = new BeforeEntityPersistedEvent($entityInstance);
$this->get('event_dispatcher')->dispatch($event);
$entityInstance = $event->getEntityInstance();
$this->persistEntity($this->get('doctrine')->getManagerForClass($context->getEntity()->getFqcn()), $entityInstance);
$this->get('event_dispatcher')->dispatch(new AfterEntityPersistedEvent($entityInstance));
$context->getEntity()->setInstance($entityInstance);
$submitButtonName = $context->getRequest()->request->all()['ea']['newForm']['btn'];
if (Action::SAVE_AND_CONTINUE === $submitButtonName) {
$url = $this->get(AdminUrlGenerator::class)
->setAction(Action::EDIT)
->setEntityId($context->getEntity()->getPrimaryKeyValue())
->generateUrl();
return $this->redirect($url);
}
if (Action::SAVE_AND_RETURN === $submitButtonName) {
$url = $context->getReferrer()
?? $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
return $this->redirect($url);
}
if (Action::SAVE_AND_ADD_ANOTHER === $submitButtonName) {
$url = $this->get(AdminUrlGenerator::class)->setAction(Action::NEW)->generateUrl();
return $this->redirect($url);
}
return $this->redirectToRoute($context->getDashboardRouteName());
}
$responseParameters = $this->configureResponseParameters(KeyValueStore::new([
'pageName' => 'profile',
'templateName' => 'crud/edit',
'entity' => $context->getEntity(),
'new_form' => $newForm,
]));
$event = new AfterCrudActionEvent($context, $responseParameters);
$this->get('event_dispatcher')->dispatch($event);
if ($event->isPropagationStopped()) {
return $event->getResponse();
}
return $responseParameters;
}
}
However I get the following error: "Call to a member function all () on array"
As it says here Add custom page for custom action I can create a totally new view, but as I said before, I would like to be able to use all the flexibility and power of easyadmin.
It would be mach easier to just set field permissions like this:
So only certain roles can access certain fields.
Se documentation https://symfony.com/doc/current/bundles/EasyAdminBundle/security.html#restrict-access-to-fields