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!

Bjgzua n (not verified)

Vgtgmr fdknym sildenafil

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!

Sebastian (not verified)

Thank you very much, this helped me a ton!

utuoallb (not verified)

cialis online sicher kaufen vendita cialis online how long before cialis daily starts to work

ahwnixqm (not verified)

cialis getting pregnant
cialis cialis for sale craigslist

ahwnixqm (not verified)

cialis getting pregnant
cialis cialis for sale craigslist

ahwnixqm (not verified)

cialis getting pregnant
cialis cialis for sale craigslist

xphthnar (not verified)

viagra 36 hour pill buying viagra online viagra sildenafil citrate 100mg price

puoyzkbz (not verified)

buying cialis cheap cialis pills online levitra compared to cialis

puoyzkbz (not verified)

buying cialis cheap cialis pills online levitra compared to cialis

3aplus63num (not verified)
3aplus63num (not verified)
olivorgn (not verified)

efeito colateral do cialis how much does cialis cost tadalafil 5mg reviews

nzlwijpc (not verified)

safest place to buy cialis online uk buy cialis canada cialis black 800mg buy viagra on line

nzlwijpc (not verified)

safest place to buy cialis online uk buy cialis canada cialis black 800mg buy viagra on line

Louisagono (not verified)

Most qualified subserviently 5 downloads sites by motive of unshackled:
https://downloadsi153.shelbae.us
https://downloads135.deltatickets4all.us
https://downloadsq7.shelbae.us
https://downloads46.inforead.xyz
https://downloadsu149.shelbae.us

staletyfzd (not verified)

generic cialis online https://canadian-drugrbnl.com/# - buy cialis online with prescription order cialis canada cialis online nz

mtaletskfw (not verified)

viagra online canada https://canadian-drugwqae.com/# - is it legal to buy viagra online brand viagra 100mg best place to buy generic viagra online buy viagra pill

btaletsxgm (not verified)

buy 20 mg cialis online https://canadian-drugrbnl.com/# - cialis online no prescription cialis dosage cialis street price

ataletkqav (not verified)

can i buy viagra online can women take viagra can you buy viagra without a prescription

ataletkqav (not verified)

can i buy viagra online can women take viagra can you buy viagra without a prescription

ataletkqav (not verified)

can i buy viagra online can women take viagra can you buy viagra without a prescription

etalethtpx (not verified)

https://canadian-drugrbnl.com/# - cialis generic cost cialis 20mg online mail order cialis

ftaletfedo (not verified)

new viagra for women https://canadian-drugwqae.com/# - order viagra onlines viagra otc usa viagra online without prescription

ytaletcrri (not verified)

discount cialis pills cialis coupon buy cialis brand online

ntaletcqck (not verified)

buying viagra without prescription https://canadian-drugwqae.com/# - best online pharmacy for viagra viagra discount coupon order viagra now viagra 120 pills

ntaletcqck (not verified)

buying viagra without prescription https://canadian-drugwqae.com/# - best online pharmacy for viagra viagra discount coupon order viagra now viagra 120 pills

ntaletcqck (not verified)

buying viagra without prescription https://canadian-drugwqae.com/# - best online pharmacy for viagra viagra discount coupon order viagra now viagra 120 pills

wtaletxfjq (not verified)

buy real cialis online https://canadian-drugrbnl.com/# - cialis without a prescription generic cialis professional 20 mg how to buy cialis generic cialis for sale

italetbyye (not verified)

viagra 50mg usa pills like viagra viagra 10 pills viagra 180 pills

ctaletujfx (not verified)

buy cialis uk https://canadian-drugrbnl.com/# - cialis cheap female cialis online generic cialis without a prescription

ltaletuftp (not verified)

best place to buy generic viagra viagra cheap buy viagra 100mg

ztaletuyqu (not verified)

cialis online no prescription https://canadian-drugrbnl.com/# - discount cialis best online pharmacy to buy cialis cialis generic price

mtalettnqi (not verified)

female version of viagra buy viagra fedex generic viagra online canada price of viagra per pill best place to buy generic viagra

mtalettnqi (not verified)

female version of viagra buy viagra fedex generic viagra online canada price of viagra per pill best place to buy generic viagra

vtaletdrqi (not verified)

https://canadian-drugrbnl.com/# - buy cialis brand online online cialis buy cialis without a prescription

ptaletddya (not verified)

where to buy real viagra online new viagra for women buy viagra los angeles buy viagra low cost cheap 25mg viagra

ntaletfeox (not verified)

us online pharmacy cialis https://canadian-drugrbnl.com/# - buy cialis online with mastercard coupons for cialis cialis tadalafil buy online

ltaletzfoq (not verified)

buy cialis cheap prices fast delivery https://canadian-drugrbnl.com/# - cheap cialis online canada buy cialis no prescription cialis street price cialis professional online

Add new comment