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.

aftab ali (not verified)

In the sites you will run over assortment of subtleties which can be tom-toming about their status. parimatch

Sebastian (not verified)

Thank you very much, this helped me a ton!

Nataljajem (not verified)

Хоть пару людей с пониманием нашлось

---
Какая нужная фраза... супер, отличная идея fifa 15 moddingway mod скачать торрент, скачать игру fifa 15 на компьютер бесплатно а также fifa 15 комментаторы черданцев скачать скачать fifa 15 ultimate edition

Kristinajem (not verified)

Извините за то, что вмешиваюсь… Мне знакома эта ситуация. Пишите здесь или в PM.

---
Я думаю, что Вы не правы. Предлагаю это обсудить. Пишите мне в PM, пообщаемся. fifa 15 cracks 3dm, fifa 15 iso скачать и скачать fifa 15 с яндекс диска игры футбол fifa 15 скачать

Bellajem (not verified)

Ох уж эти славянки!

---
Рекомендую Вам зайти на сайт, на котором есть много статей по этому вопросу. скачать fifa 15 последняя версия, fifa 15 cracks v3 скачать или crack fifa 15 v5 скачать скачать fifa 15 через торрент на pc

Polinajem (not verified)

ДА СУПЕР!!!!!!!!!!!!

---
качество фу скачать фифа 15 на ноутбук, fifa 15 c таблеткой скачать торрент или fifarus fhl bh editor fifa 15 скачать

Oscarrib (not verified)

I hope, it's OK

---
You the abstract person guangzhou escort, film escort, here rs2000 escort

Gregoryrib (not verified)

Has cheaply got, it was easily lost.

---
You commit an error. I can defend the position. Write to me in PM, we will talk. escort romania, tranny escort, here target escort

Luccilerib (not verified)

You have hit the mark. It seems to me it is excellent thought. I agree with you.

---
I think, that you commit an error. Let's discuss. Write to me in PM, we will communicate. escort girls, aurelia escort, Escorts London escort 9500i

Lawrenceml (not verified)

Be mistaken.

---
I congratulate, your idea is useful online porn kino, secretary porn online, Naty lpar brasil rpar online porn game

Lynncils (not verified)

On mine the theme is rather interesting. I suggest all to take part in discussion more actively.

---
In my opinion you are not right. I am assured. I can prove it. Write to me in PM, we will talk. porn dp online, porn cams online, Mature amateur porn cam online

AlexaPap (not verified)

Excuse, I have removed this idea :)

---
I apologise, but, in my opinion, you are mistaken. I suggest it to discuss. Write to me in PM. online porn drunk, porn stream online, Orgasm porn cams online

DianaHela (not verified)

Not in it business.

---
Consider not very well? online party porn, publicagent porn online, Hot mom and teen slut intimate lesbo sex on the couch online teen porn

KyleCype (not verified)

Excuse for that I interfere … But this theme is very close to me. Write in PM.

---
Certainly. And I have faced it. Let's discuss this question. Here or in PM. online porn group, porn thai online, Mommy and daughter fingerfuck ashlyn rae Rayveness yang porn online

AlfredWACE (not verified)

I recommend to you to look in google.com

---
I well understand it. I can help with the question decision. Together we can find the decision. cartoons porn online, nylon porn online, Hot blonde lesbians attack cock skinny porn online

AlfredWACE (not verified)

I recommend to you to look in google.com

---
I well understand it. I can help with the question decision. Together we can find the decision. cartoons porn online, nylon porn online, Hot blonde lesbians attack cock skinny porn online

Anitastek (not verified)

This situation is familiar to me. It is possible to discuss.

---
I apologise, but, in my opinion, you are not right. I am assured. I can defend the position. Write to me in PM, we will communicate. animal online porn, porn online com, Moms doggystyle fuck outdoor online porn hentai

KeithBug (not verified)

I think, that you are not right. Let's discuss it. Write to me in PM.

---
Prompt, where I can read about it? online katsuni porn, porn show online, Epic 18yo black shemale jerks huge cock webcam porn zoo online

AmandaKift (not verified)

Should you tell, that you are not right.

---
I apologise, but, in my opinion, you are mistaken. I suggest it to discuss. Write to me in PM. online webcams porn, online furry porn, Masturbation online porn смотреть

Ashtonicex (not verified)

I think, that you are not right. I am assured. Write to me in PM, we will discuss.

---
Very much I regret, that I can help nothing. I hope, to you here will help. Do not despair. katsuni online porn, русские porn online, Lascivious ebony sadie santana parody banged and cumshot online porn milf

EleanorKib (not verified)

I apologise, but, in my opinion, you commit an error. I suggest it to discuss. Write to me in PM.

---
In it something is. Thanks for the help in this question how I can thank you? online porn video, porn usa online, Fetish porn online lesbian

CarolineSl (not verified)

I apologise that, I can help nothing. But it is assured, that you will find the correct decision.

---
To speak on this question it is possible long. porn смотреть online, porn online pretty, Vanucci Trans argentina online porn com

Gavinral (not verified)

I apologise, but, in my opinion, you commit an error. I can prove it. Write to me in PM.

---
The remarkable answer :) cinema porn online, webcams porn online, Indian big tits categories porn online

EvelynCow (not verified)

You are not right. Write to me in PM, we will discuss.

---
You are right, it is exact online mommy porn, kino porn online, Dad porn mom online

CurtisMic (not verified)

Certainly. All above told the truth. Let's discuss this question. Here or in PM.

---
I join. So happens. We can communicate on this theme. Here or in PM. russia porn online, online porn asian, anal 3g online porn

KeithShuh (not verified)

I am sorry, that I interrupt you, I too would like to express the opinion.

---
You have hit the mark. In it something is also I think, what is it good idea. download porn online, porn online young, Cougar online porn scat

AlisePa (not verified)

Thanks for the help in this question, the easier, the better …

---
It cannot be! mobile porn online, games porn online, Bangbros london keyes gets deep anal excl porn online ddf

Arianawam (not verified)

I join. And I have faced it. We can communicate on this theme. Here or in PM.

---
You are not right. I am assured. Let's discuss. Write to me in PM, we will communicate. teacher porn online, fakehospital porn online, Sanskari indian bahu masterbates with a vail porn group online

MarijaProw (not verified)

The ideal answer

---
All above told the truth. We can communicate on this theme. porn online ddf, online pussy porn, Hairy granny pussy red mary and rob porn hairy online

LukeFum (not verified)

Be assured.

---
I apologise, but, in my opinion, you commit an error. Let's discuss. thai porn online, porn film online, Makin a little black baby online porn anal

AmandaPymn (not verified)

You commit an error.

---
It is removed (has mixed section) online cinema porn, porn online bbw, Bdsm party online porn

Ariannast (not verified)

It is a pity, that now I can not express - it is very occupied. But I will return - I will necessarily write that I think.

---
Between us speaking, I would ask the help for users of this forum. forced online porn, handjob online porn, Spoilt ebony teen decides she wants stepdaddies big white cock scat online porn

Inessakr (not verified)

It seems to me, what is it it was already discussed.

---
The matchless phrase, is pleasant to me :) porn фильмы online, casting porn online, Asian shemale gets ass fucked until cumming porn indian online

AdrianRah (not verified)

In my opinion you are not right. I am assured. I suggest it to discuss. Write to me in PM.

---
Even so online porn private, porn wife online, Hot Lesbo Videos loli online porn

MargaretBoni (not verified)

Ur!!!! We have won :)

---
Absolutely with you it agree. I like your idea. I suggest to take out for the general discussion. pretty online porn, brazzers porn online, Student comic porn online

Devinwaw (not verified)

All not so is simple, as it seems

---
Willingly I accept. In my opinion, it is actual, I will take part in discussion. Together we can come to a right answer. I am assured. pov porn online, online granny porn, Sexy girl pretty round ass farts child porn online

VictoriaPeld (not verified)

It is remarkable, very amusing phrase

---
In my opinion you are not right. I am assured. Write to me in PM, we will discuss. porn online cinema, kino online porn, Cam to cam chats livexchat period solidcams period com porn online thai

Add new comment