5

I would like to call the user login form in my theme but I can't.

In Drupal 7 I did this.

$elements = drupal_get_form("user_login"); 
$form = drupal_render($elements);    
echo utf8_decode($form);

How I can do this with Drupal 8?

I tried this.

Drupal::formBuilder()->getForm(Drupal\user\Form\UserLoginForm::class) 

How do I render this?

With the following code, I can show the login form, but I can't log in with it. What should I add to make it work?

$form = Drupal::formBuilder()->getForm(Drupal\user\Form\UserLoginForm::class) ; 
$render = Drupal::service('renderer');
$render->renderPlain($form); 
  • 1
    drupal_render is still available in Drupal 8, you should use that – Clive Jan 7 '16 at 14:28
  • 1
    The problem's with drupal_render it's they are Deprecated with drupal 8. What should be done to can use RendererInterface in my theme ? – Agriesean Jan 7 '16 at 14:45
  • 1
    Oops you're right, sorry. So the deprecation notice has the answer: Use the 'renderer' service instead. – Clive Jan 7 '16 at 14:46
  • 1
    tank's many. Wenn I found How to check the login I put this question as answered. (now wenn I test to log I have this in my url mysitename/cms/system/403?destination=/cms/admin/modules and I return in my login page ) – Agriesean Jan 7 '16 at 15:00
  • 1
    Finally I don't was find the possibility to log in with this form beacause the login doesn't works. I have found a other solution. The login works perfectly with bartik theme , I changed the theme to login page to bartik. They are a little diy but but it works. I do not close the question because I'm curious how to solve the problem in a more clean – Agriesean Jan 13 '16 at 12:51
9

The code seems to be working fine. If it doesn't work for some reason, make sure that there is nothing else in the theme or modules that might interfere with this.

Here's how to provide a rendered login form through preprocess function. (This code would go to YOUR_THEME.theme file.)

function YOUR_THEME_preprocess_page(&$variables) {
  // You will most likely want to show and render the form for anonymous users.
  $variables['login_form'] = FALSE;
  if (!\Drupal::currentUser()->id()) {
    $form = Drupal::formBuilder()->getForm(Drupal\user\Form\UserLoginForm::class) ; 
    $render = Drupal::service('renderer');
    $variables['login_form'] = $render->renderPlain($form);
  }
}

Then in the page.html.twig template, you can render it (and use it) with the following code.

{{ login_form }}
| improve this answer | |
  • You should use the User method isAnonymous() instead of testing the ID (it's basically the same, but more readable I think). And the ::class PHP property return the class name and namespace of a class in string, witch is exactly the class full name you define : then use directly 'Drupal\user\Form\UserLoginForm' as a string. – TytooF Dec 4 '19 at 12:20
2

You should use \Drupal::service('renderer')->renderRoot() instead of drupal_render() which is deprecated in Drupal 8, e.g.

$output = \Drupal::service('renderer')->renderRoot($form);
| improve this answer | |
1

In D8 you create render Arrays in Controllers (extends Drupal\Core\Controller\ControllerBase). For your need you could do it like with the following code.

$fb = $this->formBuilder();
return $fb->getForm("Drupal\user\Form\UserLoginForm");
| improve this answer | |
  • 1
    what does $this refer to?! – MiharbKH Jan 29 '19 at 15:45
  • 1
    to any class that inherits from ControllerBase – Rainer Feike Jan 30 '19 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.