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!

Mollyhug (not verified)

Where here against authority

---
Can fill a blank... how much does viagra cost, viagra, viagra online viagra 200mg

Mollyhug (not verified)

It yet did not get.

---
It agree, it is the amusing answer online viagra, viagra walmart, viagra on sale viagra on prescription

Mollyhug (not verified)

Willingly I accept.

---
Excellent viagra generics, buy cheap viagra on line, getting viagra viagra price walgreens

Epicksonsix (not verified)

Happens even more cheerfully :)

---
You the abstract person purchase viagra, viagra cheapest, viagra cheap canada viagra pills for sale

Epicksonsix (not verified)

I think, that you are not right. I can prove it.

---
You are not right. I am assured. I can defend the position. generic name for viagra, buy viagra generics, viagra on line online pharmacy viagra

Epicksonsix (not verified)

In it something is and it is good idea. It is ready to support you.

---
Prompt, where I can read about it? generic viagra cost, pharmacy canada viagra, viagra canada pharmacy viagra generic 100mg

StewartHit (not verified)
Sallynal (not verified)

In my opinion you are not right. I am assured.

---
I agree with told all above. Let's discuss this question. buy cheap viagra online, viagra can you buy over counter, viagra online orders viagra 20mg

Sallynal (not verified)

In my opinion you are not right. I am assured.

---
I agree with told all above. Let's discuss this question. buy cheap viagra online, viagra can you buy over counter, viagra online orders viagra 20mg

StewartHit (not verified)
StewartHit (not verified)
StewartHit (not verified)
StewartHit (not verified)
BrettkiZ (not verified)
ryqncblqgy (not verified)

buy cialis now buy generic viagra order cialis

gilpbrvhfj (not verified)

cialis 10mg sildenafil citrate generic for cialis

gzszizsptk (not verified)

cialis 5 mg buy ed drugs online order cialis

BrettkiZ (not verified)
BrettkiZ (not verified)

[url=http://antabuse02.us.com]generic antabuse[/url]
[url=http://proscar365.us.com]proscar[/url]
[url=http://albuterolcheapestoffers.com]http://www.albuterolcheapestoffers.com[/url]
[url=http://buyprovera.us.org]http://buyprovera.us.org[/url]
[url=http://clozaril.network]clozaril[/url]

jgxpdqwgci (not verified)

cialis without prescription buy ed drugs generic cialis

pvqrdctnpf (not verified)

cialis coupon over the counter viagra generic cialis

agjefrbyks (not verified)

generic cialis online ed drugs cialis 5 mg

agjefrbyks (not verified)

generic cialis online ed drugs cialis 5 mg

hxfqzhennb (not verified)

cialis price cheap medications cialis super active

hxfqzhennb (not verified)

cialis price cheap medications cialis super active

sbhmuijbqt (not verified)

generic cialis lyrica coupon cialis

sbhmuijbqt (not verified)

generic cialis lyrica coupon cialis

sbhmuijbqt (not verified)

generic cialis lyrica coupon cialis

ljqwdsprbe (not verified)

cialis vs viagra erectile medicines levitra cheap cialis

dzkqonjhxu (not verified)

buy cialis now canada pharmacy online buy cialis now

yseeklqhho (not verified)

Organisms in Bengalis 4th ed buy viagra cheap

yseeklqhho (not verified)

Organisms in Bengalis 4th ed buy viagra cheap

ghgcnopcsp (not verified)

but there are also medicate cheap generic viagra that sandbar a alveolar from producing: a solar with enlisted incompatibility buy cheap generic cialis online

Add new comment