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.

3
  • Cant you use form alter and show the text Sep 16, 2016 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.
    – Amy
    Sep 16, 2016 at 18:22
  • @Matt please check the answer, how to do easily using hook.
    – CodeNext
    Sep 16, 2016 at 19:35

3 Answers 3

6

You can create a custom block that appears only on /user.

After you create the custom block, configure it to appear in the content region. You could also need to set a condition for anonymous users.

1
  • 3
    You would also need to set a Condition for the anonymous user role.
    – mradcliffe
    Sep 16, 2016 at 18:49
7

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.

0
0

Place this hook in your .theme file and replace HOOK with your theme name. You can change your heading and paragraph text accordingly.

/**
 * Implements hook_form_alter().
 */

function HOOK_form_alter(&$form, &$form_state, $form_id) {
    if ($form['#form_id'] == 'user_login_form') {
    $form['forgot'] = array(
      '#type' => 'markup',
      '#weight' => -10,
      '#markup' => '<h2>This is my page template</h2><p>Enter your login information here</p>', 
    );
    }
}

This hook will work properly. That is working fine for me.

Thank you

Your Answer

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

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