2

I'm attempting to add text to user login page, with general info about accounts rather than only displaying the login form.

I have a page.html.twig template like so

{{ page.header }}
<h2>This is my page template</h2>
{{page.content}}
{{ page.footer }}

I then can create content in page--user--login.html.twig like so.

{{ page.header }}
<h2>This is my page template</h2>
<p>Enter your login information here</p>
{{page.content}}
{{ loginform }}
{{ page.footer }}

But is there anyway to just add text to the login form without recreating page.html.twig. Since it would just be a copy I'd rather consolidate everything so any change wouldn't have to be made to both. Is there a template I could use like node--user--login.html.twig. So that the content would use page.html.twig and use the page.content block?

I understand that you can make changes to the form using a theme_form_alter hook but I'm not sure how to add html to the form from there. I can only change things like placeholder text, and submit values.

  • Cant you use form alter and show the text – Shreya Shetty Sep 16 '16 at 18:21
  • I am using form alter but I'm not sure how to add text using it. I know I can change things like the submit value, but I don't see a way to just add html. – Matt Sep 16 '16 at 18:22
  • @Matt please check the answer, how to do easily using hook. – CodeNext Sep 16 '16 at 19:35
4

You can create a custom block, and set it to appear only under yoursitesname.com/user

After you create the custom block, configure it to appear in the content region --- above the form fields (or in a relevant region above the content region).

This will save any twig manipulation or contrib module usage.

| improve this answer | |
  • 2
    You would also need to set a Condition for the anonymous user role. – mradcliffe Sep 16 '16 at 18:49
5

You can do this very simple way using hook_form_FORM_ID_alter.

Paste following code in your themename.theme file and clear the cache

function YourThemeName_form_user_login_form_alter(&$form, FormStateInterface $form_state) {
    $form['newline'] = array(
      '#type' => 'markup',
      '#markup' => t('<h2>Enter your login information here!</h2>'),
      '#weight' => -10,
    );
}

Image output:

Before:

enter image description here

After:

enter image description here

This way you do not need to worry about anything, it will only show in Login page.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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