1

So I want to create a separate user login page for my website.

I create a twig file called page--user--login.html.twig which is great for the outer container. I still want to edit the form divand or the block div. But with debugger on It doesn't give me suggestions for creating a custom form or block twig file just for just the login form.

I can create individual form elements for the login but I want to edit the form div or block div itself without affecting anything else.

Any suggestions?

2
  • The level of detail you are talking about involves things like hook_form_alter, and individual hook_theme_suggestions_alter per form element. You should not be writing raw HTML forms in twig templates.
    – Kevin
    Feb 8, 2017 at 17:42
  • if you open up form.html.twig you get a simple form html tag, that's all I want but it needs to be separate from any other generic forms on the website. I'm not interested in writing a raw HTML form. I just want a separate twig file only for the login form.
    – Tyler
    Feb 8, 2017 at 18:15

1 Answer 1

1

You can add a theme name suggestion in a hook_theme_suggestions_HOOK_alter:

function mytheme_theme_suggestions_form_alter(&$suggestions, $variables) {
  if ($variables['element']['#form_id'] == 'user_login_form') {
    $suggestions[] = 'form__user_login_form';
  }
}

If you want to add suggestions for form elements you need some more code. See the file themable_forms.module from the module Themable forms. This is the only code file in this module and it is a good example how you can approach this in form alter, preprocess form element and theme suggestions hooks.

2
  • This didn't work for me. I try changing user_login_form to user-login-form and I was to have user-login-forms for the inputs but not a suggestion for the <form> itself. I'll play around with this some more but I think I'll just end up using an if statement on form.html.twig and load the added attributes I want to <form> tag among other things.
    – Tyler
    Feb 9, 2017 at 3:39
  • OK, this was only the start. See the edited answer where you can go from there.
    – 4uk4
    Feb 9, 2017 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.