Hey! So I'm basically just trying to change the login form and register form labels to instead say Email address instead of Username or email address and Full Name instead of Username. Any thoughts on what I could be doing wrong? I don't see any changes at all, but I know it's being called because the form id's print. 

<?php

/**
* @file
* Task 1 - only ask for email address and change username label to Full Name
*/

use Drupal\Core\Form\FormStateInterface;

/**
* Task 1 - change label from username to Full Name and asks for email
* Implements hook_form_alter().
*/
function kay_user_form_alter(&$form, Drupal\Core\Form\FormStateInterface $form_state, $form_id){
//attempting to print with dsm
dpm($form_id);

if ($form_id == 'user_login_form') {
$form['name']['#title'] = t('Email address.');
$form['name']['#description'] = t('Enter your email address');
$form['name']['#element_validate'][] = 'kay_user_user_login_validate';
}
if ($form_id == 'user_register_form') {
// change label for username input
$form['name']['#title'] = t('Full Name');
}
}

/**
* Form element validation handler for the user login form.
*
* Allows users to authenticate by email
*/
function kay_user_user_login_validate($form, FormStateInterface $form_state) {
$name_input = $form_state->getValue('name');

// Try loading by email.
if ($user = user_load_by_mail($name_input)) {

// Set the username for further validation.
$form_state->setValue('name', $user->getAccountName());
return TRUE;
}
return FALSE;
}

Comments

wombatbuddy’s picture

1. To explore the structure of the $form array visit
/admin/people/permissions
and give to anonymous users the 'Access kint information' permission.
See the screenshots:
1. https://cdn1.imggmi.com/uploads/2020/1/1/c7c28e0f0a2079b0575f3080fbaa88c...
2. https://cdn1.imggmi.com/uploads/2020/1/1/8b6f795cd1f1d9e936657d3efd685aa...

2. Call kint() like this: 

if ($form_id == 'user_login_form') {
  kint($form);
}

3. Log out and rebuild the cashes.
You can install the Drush and use the following command:
drush cr

4. Visit
/user/login
and explore the $form array.
See the screenshot:
https://cdn1.imggmi.com/uploads/2020/1/1/cb66afe38d49d494d2426fadbabc044...

5. After you'll know the type of the object you can create a new one: 

<?php

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

function YOUR_MODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'user_login_form') {
    $form['name']['#title'] = new TranslatableMarkup('Full Name');
  }
}

6. Rebuild the cashes (drush cr).

7. Refresh the page and you are done.
See the screenshot of the result:
https://cdn1.imggmi.com/uploads/2020/1/1/adcd6054db2e404a200079ca8d4a093...

fojo’s picture

Thanks for this. For some reason, I still bump into the same problem. It's definitely using kint and I see the $form array, but now when I try to alter the labels, I get nothing.

function kay_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'user_register_form') {
$form['name']['#title'] = new TranslatableMarkup('Full Name');
}
if ($form_id == 'user_login_form') {
$form['name']['#title'] = new TranslatableMarkup('Full Name');
$form['name']['#title'] = new TranslatableMarkup('Email address.');
$form['name']['#description'] = new TranslatableMarkup('Enter your email address');
$form['name']['#element_validate'][] = 'kay_user_user_login_validate';
}
}

Any thoughts? Thanks again, you're amazing

fojo’s picture

For some reason, this works for the register form, but the login form remains unchanged. Any thoughts?

function kay_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
kint($form);

if ($form_id == 'user_register_form') {
$form['account']['name']['#title'] = t('Full Name');
}

if ($form_id == 'user_login_form') {
$form['name']['#title'] = t('Email address.');
$form['name']['#description'] = t('Enter your email address');
$form['name']['#element_validate'][] = 'login_emailusername_user_login_validate';
}
}

wombatbuddy’s picture

1. Yes, you can use t() instead.
2. I have tested your initial code and it seems, the validation is working.
3. I guess, that you didn't rebuild the cache after log out.
You can do this with 'Drush' commnad: 

druch cr

or 'Drupal Console' command: 

drupal cr all

After rebuilding the chashe reload the page and, I hope, you'll see the changes. Also, you can try to reinstall your module.

wrg20’s picture

You just saved me a lot of time. I'm not sure why I didn't have that permission enabled on my local dev but I'll remember that for the future.  Thanks!

karmraj.zala’s picture

Hi fojo,

To alter a form, you need to call hook_form_alter(). Add an empty implementation of hook_form_alter(), like so:

  /**
 * Implements hook_form_alter().
 */
function THEMENAME_form_alter(&$form, &$form_state, $form_id) {

}

To alter a form, you need to know its ID. There are two easy ways to find a form’s ID. 

 function THEMENAME_form_alter(&$form, &$form_state, $form_id) {
  print $form_id;
}
  

Now, you can change all the code inside the form alter function.

 function THEMENAME_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'FORM_ID') {
    $form['field_name'] = array(
      '#type' => 'item',
      '#title' => t('New Lable'),
    );
  }
}

Hope this will help you. 

Thanks.