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

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.

sda (not verified)

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

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

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

Add new comment