EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Call to a member function getI18n() on null

Open 4d4ch4u32 opened this issue 2 years ago • 5 comments

image

Describe the bug I use the login template provided by EasyAdminBundle, after updating to the latest version, I get the error above.

To Reproduce How I use the template in the controller:

    public function login(AuthenticationUtils $authenticationUtils): Response
    {
        $error        = $authenticationUtils->getLastAuthenticationError();
        $lastUsername = $authenticationUtils->getLastUsername();

        $params = [
            'error'                   => $error,
            'last_username'           => $lastUsername,
            'page_title'              => '<h4>Backoffice Login</h4>',
            'username_parameter'      => 'email',
            'password_parameter'      => 'password',
            'csrf_token_intention'    => 'authenticate',
            'forgot_password_enabled' => true,
            'forgot_password_path'    => $this->generateUrl('backoffice_password_request'),
        ];

        return $this->render('@EasyAdmin/page/login.html.twig', $params);
    }

4d4ch4u32 avatar Oct 22 '24 11:10 4d4ch4u32

I can't reproduce this error. Recently we added the hasContext() method (see https://github.com/EasyCorp/EasyAdminBundle/commit/6c817f95e9b382860d68d02be8efdd17a5f53899) precisely to avoid the error mentioned here. But, in your screenshot I'm seeing the call to hasContext() method, so I'm not sure what's going on.

Could you please try to debug this issue a bit to find out why ea is null but ea.hasContext() returns true? Thanks!

javiereguiluz avatar Oct 30 '24 18:10 javiereguiluz

Closing because we don't have more feedback. Hopefully this was solved.

javiereguiluz avatar Nov 07 '24 19:11 javiereguiluz

I have the same issue on my side with the current tag v4.14.5.

In my case, I am using a "dirty" twig fix to work around cases of Environment being more than once to have the environment available, in my case to i.e. generate ->help('') content in CRUDs.

        $globals = parent::getGlobals();
        $context = $this->adminContextProvider;

        if ($context !== null) {
            $globals['ea'] = $context;
        }

Previously, the line was $this->adminContextProvider->getContext();.

bytes-max avatar Nov 17 '24 00:11 bytes-max

i have a similar error with forgot_password_path parameter LoginController.php ` #[Route("/login", name: "login")] public function login(AuthenticationUtils $authenticationUtils): Response { $error = $authenticationUtils->getLastAuthenticationError(); $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render('Front/login.html.twig', [
        'error' => $error,
        'last_username' => $lastUsername,
        // OPTIONAL parameters to customize the login form:
        'favicon_path' => '<link rel="shortcut icon" href="{{ asset("build/images/favicon.ico") }',
        'page_title' => 'Kinetikon',
        'csrf_token_intention' => 'authenticate',
        'target_path' => $this->generateUrl('admin'),
        'username_label' => 'Your username',
        'password_label' => 'Your password',
        'sign_in_label' => 'Log in',
        'username_parameter' => '_username',
        'password_parameter' => '_password',
        'forgot_password_enabled' => true,
        'forgot_password_path' => $this->generateUrl('app_forgot_password_request'),
        'forgot_password_label' => 'Forgot your password?',
        'remember_me_enabled' => true,
        'remember_me_parameter' => '_remember_me',
        'remember_me_checked' => true,
        'remember_me_label' => 'Remember me',
    ]);
}

`

I use symfonycasts/reset-password-bundle to generate controller and templates. ResetPasswordController.php

#[Route('', name: 'app_forgot_password_request')]
    public function request(Request $request, MailerInterface $mailer, TranslatorInterface $translator, EntityManagerInterface $em): Response
    {
        $form = $this->createForm(ResetPasswordRequestFormType::class);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $input_mail = $form->get('email')->getData();

            $db_mail = $em->getRepository(User::class)->findOneBy(['email' => $input_mail]);

            if (!$db_mail) {
                $this->addFlash(
                    'danger',
                    'This is not a valid email'
                );
                return $this->redirectToRoute('app_forgot_password_request');
            }

            return $this->processSendingPasswordResetEmail(
                $input_mail,
                $mailer,
                $translator
            );
        }

        return $this->render('Front/reset_password/request.html.twig', [
            'requestForm' => $form->createView(),
        ]);
    }

Now i am trying to extend easyadmin content template Front/reset_password/request.html.twig {% extends '@EasyAdmin/page/content.html.twig' %}

the error i get is: "An exception has been thrown during the rendering of a template ("Call to a member function getTemplatePath() on null")."

Image

I end up using custom twig templates for pages and actions where user is not logged in (like forgot password request and confirm). Someone have a better solution? I would like to use easyadmin templates also for not logged in users, or for example i would like to use the login box: Image also for forgot password form.

knk-danielgavriliuc avatar Apr 15 '25 14:04 knk-danielgavriliuc

@danielgavriliuc if you don't extend your ResetPasswordController from an EasyAdminController you don't have the context; ea in the template is null. That is IMHO not the same error.

4d4ch4u32 avatar Apr 16 '25 07:04 4d4ch4u32