How to render user login form and user register form in Drupal 8

Posted by admin on Thu, 06/29/2017 - 10:04

I've spend an hour to understand how to render user login form and user register form in Drupal 8 to print them in a custom place.
Here is the solution.

Lets say you want to print those two forms in a custom place on your page (in a popup in my case).

So if you want to print it in the page.html.twig - so in the THEMENAME.theme write this first:

function THEMENAME_preprocess_page(&$variables) {
}

User login form

$form = Drupal::formBuilder()->getForm(Drupal\user\Form\UserLoginForm::class) ;
$render = Drupal::service('renderer');
$variables['login_form'] = $render->renderPlain($form);

User registration form

    $entity = \Drupal::entityTypeManager()->getStorage('user')->create(array());
    $formObject = \Drupal::entityTypeManager()
      ->getFormObject('user', 'register')
      ->setEntity($entity);
    $form = \Drupal::formBuilder()->getForm($formObject);
    $variables['register_form'] = \Drupal::service('renderer')->render($form);

The final step would be to output the results using your twig template.
For example in page.html.twig you just write to ouput the login form:

{{ login_form }}

Or register form:

{{ register_form }}
×

Hey! Don't miss a new cool stuff!

Comments

summer (not verified)

Thanks! I'm sure you saved me a ton of time and one less module!

sudharson (not verified)

Thanks, it saves me lot of time

Julius (not verified)

Thanks, works good on pages. If I try to use it on nodes it produces an error. Do I have to change anything if I have THEMENAME_preprocess_node ?

admin

What error do you have, could you explain?

Navneet (not verified)

Thanks! You saved my lots of time

johnatasjmo (not verified)

Can you share the full code in a git repo? Thks

Nhat (not verified)

Can you share the full code .. Thanks

admin

The full code is in the article ;)
So you need to add inside you THEMENAME.theme:

function THEMENAME_preprocess_page(&$variables) {
  $form = Drupal::formBuilder()->getForm(Drupal\user\Form\UserLoginForm::class) ;
  $render = Drupal::service('renderer');
  $variables['login_form'] = $render->renderPlain($form);
}

Then inside your page.html.twig:
Just add {{ login_form }} to print login form wherever you want.

Alex (not verified)

working in page.html.tpl but not work if i use for example menu.html.tpl :(

Lalit (not verified)

How to render the password reset form in another file like login and register form

Alvaro (not verified)

Very useful info but do you know how get it working in block--myblock.html.twig template instead page.html.twig?

Thank a ton in advance.

Catherine (not verified)

oh my goodness thank you so much!

aftab ali (not verified)

Here while influencing a determination you to must be watchful about the subtleties and need to confirm all.

Sebastian (not verified)

Thank you very much, this helped me a ton!

StewartHit (not verified)
Theroncidly (not verified)
Theroncidly (not verified)
Theroncidly (not verified)
Theroncidly (not verified)
Theroncidly (not verified)
hofggzmdzb (not verified)

Faint of tide like as fabulously online kamagra
http://test.fungodoctor.com.ua/index.php/component/users/?option=com_k2&view=itemlist&task=user&id=18622&lang=ru http://parcheggiromatiburtina.it/?option=com_k2&view=itemlist&task=user&id=1628980 http://giusto-shop.ru/forums/user/Norwoodyss

hofggzmdzb (not verified)

Faint of tide like as fabulously online kamagra
http://test.fungodoctor.com.ua/index.php/component/users/?option=com_k2&view=itemlist&task=user&id=18622&lang=ru http://parcheggiromatiburtina.it/?option=com_k2&view=itemlist&task=user&id=1628980 http://giusto-shop.ru/forums/user/Norwoodyss

Theroncidly (not verified)
Theroncidly (not verified)

[url=https://jodhsuphogeli.tk]jodhsuphogeli.tk[/url]

Theroncidly (not verified)

[url=https://jodhsuphogeli.tk]jodhsuphogeli.tk[/url]

Bobbywropy (not verified)

[url=https://tadetworlrighnad.tk]https://tadetworlrighnad.tk[/url]

Bobbywropy (not verified)

[url=https://tadetworlrighnad.tk]https://tadetworlrighnad.tk[/url]

Bobbywropy (not verified)

[url=https://tadetworlrighnad.tk]https://tadetworlrighnad.tk[/url]

Bobbywropy (not verified)

[url=https://tadetworlrighnad.tk]https://tadetworlrighnad.tk[/url]

Bobbywropy (not verified)

[url=https://willlandprednili.gq]girls transvestite porn teen webcams [/url]

Bobbywropy (not verified)

[url=https://willlandprednili.gq]girls transvestite porn teen webcams [/url]

Michealfrest (not verified)

[url=https://crookoxmora.cf]mature teen cams [/url]
[url=https://ondysbebacon.cf]ondysbebacon.cf[/url]
[url=https://riesukindcoruc.tk]riesukindcoruc.tk[/url]
[url=https://procimoutor.tk]cam strip nude [/url]
[url=https://riewithsaddcompsu.ml]https://riewithsaddcompsu.ml[/url]

Michealfrest (not verified)

[url=https://crookoxmora.cf]mature teen cams [/url]
[url=https://ondysbebacon.cf]ondysbebacon.cf[/url]
[url=https://riesukindcoruc.tk]riesukindcoruc.tk[/url]
[url=https://procimoutor.tk]cam strip nude [/url]
[url=https://riewithsaddcompsu.ml]https://riewithsaddcompsu.ml[/url]

Bobbywropy (not verified)

[url=https://cerlansjourkoger.gq]cerlansjourkoger.gq[/url]

Bobbywropy (not verified)

[url=https://cerlansjourkoger.gq]cerlansjourkoger.gq[/url]

Bobbywropy (not verified)

[url=https://cerlansjourkoger.gq]cerlansjourkoger.gq[/url]

Michealfrest (not verified)

[url=https://neyrearepolcams.ml]private chat rooms teen video [/url]
[url=https://convibardforsbun.gq]hidden cam reality teen thai [/url]
[url=https://centutaders.tk]centutaders.tk[/url]
[url=https://llevimalpreas.tk]llevimalpreas.tk[/url]
[url=https://ciomudomysla.gq]https://ciomudomysla.gq[/url]

Michealfrest (not verified)

[url=https://neyrearepolcams.ml]private chat rooms teen video [/url]
[url=https://convibardforsbun.gq]hidden cam reality teen thai [/url]
[url=https://centutaders.tk]centutaders.tk[/url]
[url=https://llevimalpreas.tk]llevimalpreas.tk[/url]
[url=https://ciomudomysla.gq]https://ciomudomysla.gq[/url]

Bobbywropy (not verified)

[url=https://willlandprednili.gq]willlandprednili.gq[/url]

Michealfrest (not verified)

[url=https://terpnesdybackher.ml]free teen chat rooms welcome [/url]
[url=https://leyspenemacle.ml]hidden cams women jacking off men [/url]
[url=https://hoviterqure.cf]hidden cameras having sex [/url]
[url=https://oridnerneu.ga]oridnerneu.ga[/url]
[url=https://terpnesdybackher.ml]terpnesdybackher.ml[/url]

Michealfrest (not verified)

[url=https://terpnesdybackher.ml]free teen chat rooms welcome [/url]
[url=https://leyspenemacle.ml]hidden cams women jacking off men [/url]
[url=https://hoviterqure.cf]hidden cameras having sex [/url]
[url=https://oridnerneu.ga]oridnerneu.ga[/url]
[url=https://terpnesdybackher.ml]terpnesdybackher.ml[/url]

Zaduelpep (not verified)

viagra viagra celebrex best price
[url=http://genviagrabst.com]buy generic viagra online cheap
[/url] cheap genuine viagra online trusted uk
is it safe to buy generic viagra online
- norvasc viagra interact
levitra viagra vs viagra comment.php

SzephanTeday (not verified)

is cialis daily the best
[url=http://viagforsl.com]where can i buy generic viagra in the usa
[/url] cialis 10 mg effectiveness name
sidenafil for sale
- cialis for daily use commercial what
cialis daily medication

Michealfrest (not verified)

[url=https://ciomudomysla.cf]ciomudomysla.cf[/url]
[url=https://tramconsglenun.tk]tramconsglenun.tk[/url]
[url=https://mitdoboturci.cf]freeweb cam sex [/url]
[url=https://dardtsotultiuwil.cf]order brides came [/url]
[url=https://biagebdada.tk]biagebdada.tk[/url]

Aaronzorse (not verified)

[url=https://tadetworlrighnad.tk]huge cock on cam [/url]

SzephanTeday (not verified)

retalis cialis
[url=http://viagforsl.com]us generic viagra
[/url] enzyte
viagra for sale
- can ed be reversed
ed treatment options

SzephanTeday (not verified)

retalis cialis
[url=http://viagforsl.com]us generic viagra
[/url] enzyte
viagra for sale
- can ed be reversed
ed treatment options

Add new comment