I need to know how to use hook_form_alter in .theme file as i have to alter the form fields related to user_register_form.

I tried putting the below code in the .theme file but it does not even reach the function

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

  // Adding custom validation for the welcome page type field.
  if ($form_id == 'user_register_form') {
     print_r('Reaching in here');exit;

1 Answer 1


If you want to alter a specific form, and you know the ID, use hook_form_FORM_ID_alter() rather than the generic version:

function THEMENAME_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Add alterations specific to the user registration form

Make sure you clear the cache after renaming the function.

  • 1
    This worked well Great Thanks. Though i had to use the interface : use Drupal\Core\Form\FormStateInterface; Commented Sep 4, 2018 at 13:08

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.