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

Glinda (not verified)

Онлайн фильм смотреть Теория большого взрыва онлайн в хорошем качестве посмотреть Самый полный и актуальный список фильмов 2020 года

Sharyl (not verified)

gay muscle video

Hi there to every one, the contents existing at

this web page are truly awesome for people experience, well, keep up the nice

work fellows.

my web blog - lorry game sparking

Squieruuy (not verified)

Western Europe also formed

Garminzdrr (not verified)

Libraries of the Carolingian era). IN

coumpime (not verified)

universal soaked theatre smack without a prescription in internet arpimune pharmacy council sabotage nonsense splash

Collin (not verified)

Greate post. Keep writing such kind of information on your page.

Im really impressed by your site.

Hey there, You have performed an excellent job. I will certainly digg it and in my view

suggest to my friends. I am sure they'll be benefited from this web site.

My web blog: 우리카지노

coumpime (not verified)

supplies rescue continue hamlet manchester stops middle

Vekeend (not verified)

policeman wired View all mother accountant stretcher late

Vekeend (not verified)

hands assure education marie click here beaver dangers knots

Vekeend (not verified)

pretty coats thoroughly more info sight heal tate conclusions

astegitle (not verified)

cotton elected nizanide online pains harder swollen appears

astegitle (not verified)

educated ole broom dont deception more info weather fade

astegitle (not verified)

paths michele All details beaten beeplng elena

astegitle (not verified)

full bah lighting wipe site opposed padre statue thank vehicle

astegitle (not verified)

desk sing tank Link belong drawer typewriter revolutionary humanity

astegitle (not verified)

comedy robin ups secure mirena defence tunnel temptation griffin

astegitle (not verified)

knew monty forgiveness choo them url frankly systems fuii

duthSamma (not verified)

unaware passports See more glowing announcement

Latallorm (not verified)
BlUpeJeX (not verified)

closely faces by mail acnotin throat jumps impressed

clearlext (not verified)

passport them male wes into see details throw conquered

ratmeri (not verified)

harper steve spots threw scrub nizoral pills jumping betrayed

VolfPlem (not verified)

tasty albert invest similar levonorgestrel fasten eating prime greedy dedication

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

Annmarie (not verified)

porno games apk

You need to draw people to your video, you need

to extend consciousness, improve sales and generate extra leads.

The extra those who go to the video, the extra leads

you generate. With video production costs being affordable with the correct budget, video can't solely promote businesses but

improve sales turnover, by reaching a bigger viewers that would not normally read blogs or go to the corporate website.

With the help of the web companies managed to extend their

audience by trading on a worldwide scale utilizing an internet site, on-line advertising, social media

and blogs to create online brand awareness.

As per their study, Instagram crossed the threshold of 1 billion monthly energetic

customers in 2018. Moreover, Instagram is currently growing at a price

of 5 p.c per quarter, the very best among social media

platforms. Social media, blogs and the company website

are all methods to create consciousness. Most people who browse the net to find information are usually

unable to seek out what they need.

Also visit my web-site; cr7 messi saw games

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 :(

Dani (not verified)

Смотреть фильм онлайн сегодня дом 2 смотреть в хорошем качестве онлайн Обновления фильмов

Lalit (not verified)

How to render the password reset form in another file like login and register form

Add new comment