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

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

Betsey (not verified)

Фильм онлайн смотреть Рассказ Служанки смотреть в хорошем качестве онлайн Лучший онлайн-кинотеатр?

Добрый день, граждане.

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.

Marilyn (not verified)

Фильм онлайн смотреть лайф дом 2 онлайн в хорошем качестве посмотреть новинки

фильмов и сериалов

Catherine (not verified)

oh my goodness thank you so much!

Sebastian (not verified)

Thank you very much, this helped me a ton!

utuoallb (not verified)

cialis online sicher kaufen vendita cialis online how long before cialis daily starts to work

ahwnixqm (not verified)

cialis getting pregnant
cialis cialis for sale craigslist

ahwnixqm (not verified)

cialis getting pregnant
cialis cialis for sale craigslist

Elsie (not verified)

Смотреть фильм онлайн Меч императора посмотреть онлайн в хорошем качестве большая база фильмов в HD

ahwnixqm (not verified)

cialis getting pregnant
cialis cialis for sale craigslist

Allen (not verified)

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

xphthnar (not verified)

viagra 36 hour pill buying viagra online viagra sildenafil citrate 100mg price

Tawnya (not verified)

In case your videos are about a product you

promote, ensure that to include a link in the video description.

Your video advertising marketing campaign must start out with a

effectively performed video if it's that can assist you promote your product or website.

To know how one can present your on-line videos it is best to take a look at the best way to optimize your videos.

If you have obtained a couple of completely different videos uploaded, the likelihood is better that at least one or two of them might take off and start spreading like wildfire.

Take advantage of YouTube's editing options everytime you add videos to this

site (many different video sharing sites also offer such things).

Nevertheless, sharing with others outside your network

requires video sharing and social networking sites. Sharing on-line movies is principally a suave

method of selling your organization and model and distributing relevant data by

way of the production firms NYC of emotional and fascinating content.

i love gay marriage

Feel free to visit my blog post; panty hose gay

puoyzkbz (not verified)

buying cialis cheap cialis pills online levitra compared to cialis

puoyzkbz (not verified)

buying cialis cheap cialis pills online levitra compared to cialis

3aplus63num (not verified)
3aplus63num (not verified)
olivorgn (not verified)

efeito colateral do cialis how much does cialis cost tadalafil 5mg reviews

Joycelyn (not verified)

teen gay date

whoah this weblog is magnificent i love studying your articles.

Stay up the great work! You understand, many individuals are searching

around for this info, you can aid them greatly.

My website: turk teen porno

Regena (not verified)
nzlwijpc (not verified)

safest place to buy cialis online uk buy cialis canada cialis black 800mg buy viagra on line

nzlwijpc (not verified)

safest place to buy cialis online uk buy cialis canada cialis black 800mg buy viagra on line

Louisagono (not verified)

Most qualified subserviently 5 downloads sites by motive of unshackled:
https://downloadsi153.shelbae.us
https://downloads135.deltatickets4all.us
https://downloadsq7.shelbae.us
https://downloads46.inforead.xyz
https://downloadsu149.shelbae.us

staletyfzd (not verified)

generic cialis online https://canadian-drugrbnl.com/# - buy cialis online with prescription order cialis canada cialis online nz

Margherita (not verified)

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

г

mtaletskfw (not verified)

viagra online canada https://canadian-drugwqae.com/# - is it legal to buy viagra online brand viagra 100mg best place to buy generic viagra online buy viagra pill

btaletsxgm (not verified)

buy 20 mg cialis online https://canadian-drugrbnl.com/# - cialis online no prescription cialis dosage cialis street price

ataletkqav (not verified)

can i buy viagra online can women take viagra can you buy viagra without a prescription

ataletkqav (not verified)

can i buy viagra online can women take viagra can you buy viagra without a prescription

Deanne (not verified)

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

ataletkqav (not verified)

can i buy viagra online can women take viagra can you buy viagra without a prescription

Bailey (not verified)

Онлайн смотреть фильм дом 2 эфиры смотреть онлайн в хорошем качестве смотреть Онлайн кинотеатр

etalethtpx (not verified)

https://canadian-drugrbnl.com/# - cialis generic cost cialis 20mg online mail order cialis