Hey! I'm extremely new to Drupal development and have the following code:
kay_user.module:
<?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') {
//- change label for username input
$form['name']['#title'] = t('Full Name');
$form['name']['#title'] = t('Email address.');
$form['name']['#description'] = t('Enter your email address');
$form['name']['#element_validate'][] = 'kay_user_user_login_validate'
}
}
/**
* 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;
}
kay_user.info.yml:
name: Kay User
description: Edits login and registration forms
package: Custom
type: module
core: 8.x
version: 1.0
I've checked that the module is enabled, but I don't get any response when I try to run this. It also never prints the form id from the dpm(form_id) command. Any thoughts?
Comments
You forgot '$' prefix,
You forgot '$' prefix, replace
with
Happy New Year!
Thank you and Happy New Year!
Thank you and Happy New Year!
Fixed that, but still no luck. I feel like the module itself just isn’t being used/neither hook is being called.
1. Ensure that you rebuild
1. Ensure that you rebuild the caches.
2. Check that the 'Devel' module is installed.
3. Try to follow this guide: 'Drupal 8 : Custom Form Validate and Submit in hook_form_alter() with Example'.
Also, here is the article that may be helpful: 'How to Print Variables using Kint in Drupal 8'.
Thank you! It prints! Hook
Thank you! It prints! Hook doesn't do what I want lol, but it's being called so that's progress!