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

Alta (not verified)

I loved as much as you'll receive carried out right here.

The sketch is tasteful, your authored material stylish.

nonetheless, you command gett got an edginess over that you wish

bbe delivering the following. unwell unquestionably come more formerly

again as exactly the same nearly a lot often inside case you

shield this increase.

my web-site skull kid hacked unblocked games

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?

Jannie (not verified)

I am really delighted to glance at this weblog posts which contains plenty of useful facts, thanks for providing these

data.

my web page - unblocked games run 3

Chana (not verified)

Hi friends, its fantastic paragraph regarding educationand entirely explained, keep it up all the time.

Also visit my website :: tank trouble swf

Carina (not verified)

It's very effortless to find out any matter on web as compared to

books, as I found this article at this website.

my site ... bad piggies 3

Navneet (not verified)

Thanks! You saved my lots of time

johnatasjmo (not verified)

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

Ralf (not verified)

I got this website from my pal who told me regarding this web page and now this time I am visiting this web site and reading very informative posts at this place.

My site: strike force heroes 2

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.

Mallory (not verified)

Greetings from Los angeles! I'm bored to tears at work so

I decided to check out your blog on my iphone during lunch

break. I love the knowledge you provide here and can't

wait to take a look when I get home. I'm amazed at how quick your blog loaded on my cell phone ..

I'm not even using WIFI, just 3G .. Anyways, great blog!

Feel free to surf to my weblog :: friv 4

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

Larry (not verified)

Hi, I think your website might bee having browser compatibility issues.

When I look at your blog site in Safari, it looks fine bbut when opening in Internet Explorer,

it has some overlapping. I just wanted to give you

a quick heads up! Other then that, excellent blog!

Also visit my site :: gods of the arena

Loyd (not verified)

Hello there! This is kind of off topic but I need some advice from an established blog.

Is bomb it 2

hard to set up your own blog? I'm not very techincal but I can figure things out pretty quick.

I'm thinking about setting up my own but I'm not sure where to begin.

Do you have any points or suggestions? Cheers

Marisa (not verified)

Hello, every time i used to check website posts here early in the morning, as i enjoy to find out more and more.

Feel free to visit my web-site friv

Anonymous (not verified)

I like to read your periodical advice best articles. You’ve got sensible writing ability and it makes the reader to read your articles additional and additional. This post contains useful facts that are the aid to the readers. Invariably this paper has terribly useful details. Thanks such a lot for this wonderful post. Thanks again! I actually have some sensible work expertise with write my essay ( https://essayjaguar.com )and my words are clearly supported what I felt through such processes within the past

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.

Tanisha (not verified)

Wonderful goods from you, man. I have understand your stuff previous to

and you are just extremely great. I really like what you have

acquired here, certainly like what you're stating

and the way in which you say it. You make it entertaining

and you still take care of to keep it smart. I can not wait to read far more from you.

This is actually a tremendous site.

Visit my blog post ... snail bob 3

Eleanore (not verified)

I like the helpful info you provide in your

articles. I will bookmark your weblog and check again here frequently.

I am quite certain I'll learn many new stuff right here!

Best of luck for the next!

Feel free to surf to my homepage ... friv games

sda (not verified)

This blog was extremely helpful. karamba I really appreciate your kindness in sharing this with me and everyone else!

Taj (not verified)

Way cool! Some extremely valid points! I appreciate you penning this write-up and the

rest of the site is also really good.

Review my web blog - alien attack team 3

aftab ali (not verified)

the web based wagering surveys web sites and the internet wagering society talk sites as you could before settling on your definitive choice on turning into a piece of a demanding on the web gambling club society. hopa

Rachele (not verified)

I seriously love your blog.. Very nice colors & theme. Did you make this amazing site yourself?

Please reply back as I'm hoping to create my very own blog and would love to find out where you got this from or what the theme

is called. Cheers!

Look into my site ... age of war 4

asd (not verified)

We are truly thankful for your blog entry. You will discover a great deal of methodologies in the wake of going to your post. I was precisely scanning for. A debt of gratitude is in order for such post and please keep it up.  iq option

emily seo (not verified)

When you have occupied with the above advances, it is currently time to decide whether you might want to arrange a standard clubhouse poker chips set or on. storspelare

aftab ali (not verified)

trade in help of lay wagering you are stating that the result of the occasion or game that you are lay wagering wouldn't come to fruition, not going to win. 1xbet

Carin (not verified)

Excellent post. I was checking continuously this blog and I

am impressed! Extremely useful info specially the last part :

) I care for such information much. I was seeking this particular information for a very long time.

Thank you and good luck.

my webpage clicker heroes 3

aftabak (not verified)

the estimation of the result more confused. Here are a couple of tips to make things simpler. nightrush

aftabak (not verified)

You could even print anything you need simply like nearby printing shops would do. Jetbull

Greg (not verified)

Hey would you mind stating which blog platform you're using?

I'm looking to start my own blog soon but I'm having a tough time

choosing between BlogEngine/Wordpress/B2evolution and Drupal.

The reason I ask is because your layout seems different then most blogs

and I'm looking for something completely unique.

P.S Sorry for being off-topic but I had to ask!

My weblog - geometry dash online

Paul (not verified)

Fantastic post however I was wondering if you could write a litte more on this topic?

I'd be very thankful if you could elaborate a little bit more.

Many thanks!

My web site; friv 2

Teena (not verified)

WOW just what I was looking for. Came here by searching for how to summon chocobo

Here is my web site: electric man 2

Catherine (not verified)

oh my goodness thank you so much!

Add new comment