How to Change the Title of a Drupal Form (Drupal 9 & 10)

29. September 2023 approx. 2 min read Drupal
Drupal user registration form

If you want to change the title of a form, whether it's the title of the registration form or a node add form, you have two options:

Here's an example of how to change the title of the registration form:

1. Use the hook_preprocess_page_title

/**
 * Implements hook_preprocess_HOOK().
 */
function MYTHEME_MYMODULE_preprocess_page_title(&$variables) {
  if (\Drupal::routeMatch()->getRouteName() == 'registration_link.register') {
    $variables['title'] = t('New Title');
  }
}

2. Utilize the hook_preprocess_html

/**
 * Implements hook_preprocess_HOOK().
 */
function MYTHEME_MYMODULE_preprocess_html(&$variables) {
  if (\Drupal::routeMatch()->getRouteName() == 'registration_link.register') {
    $variables['head_title']['title'] = t('New Title');
  }
}

These two approaches allow you to customize the title of a Drupal form of your choice. Replace 'New Title' with the desired title text you want to display.

Comments