2

By default, the user form title is the user's username which looks a little ugly if you're using the email registration module. In Drupal 7 I used to solve this problem using the title override module which is not ported do Drupal 8.

I would like to change the user form title to "Login details". Is this possible using a hook? If so: how do I do this?

1
  • This doesnt allow dynamic information e.g. node title, node type - unless you use global functions for pulling the node from the url rather than as a parameter. Commented Jan 13, 2021 at 18:51

2 Answers 2

11

The title override module utilizes drupal_set_title() to do this in Drupal 7. This function is deprecated in Drupal 8. The D8 equivalent here is to alter the _title_callback for the entity.user.edit_form route.

This is done by using an event subscriber to alter the RoutingEvents::ALTER event. This can be done through a custom module that:

  • Registering a new service class in mymodule.services.yml that is tagged with event_subscriber (see d.o. documentation example).
  • Defining the new service class to extend RouteSubscriberBase (see d.o. documentation example).
  • Implementing the alterRoutes(RouteCollection $collection) method within the service class to alter the entity.user.edit_form route (see example below).
  • A class/method that defines the title callback (see UserController::userTitle() as an example).

Within your route subscriber class:

  /**
   * {@inheritdoc}
   */
  public function alterRoutes(RouteCollection $collection) {
    // Retrieve the user edit form route.
    if ($route = $collection->get('entity.user.edit_form')) {
      // Your module will need a controller class/method defined in
      // src/Controller/CustomUserController::userTitle
      // see core/modules/user/src/Controller/UserController.php to see how this
      // is implemented by default
      $route->setDefault('_title_callback','Drupal\mymodule\Controller\CustomUserController::userTitle');
    }
  }
0
0

In drupal 8 you can hide the title block :

/admin/structure/block/manage/pagetitle

for the :

user/* pages 

Then create a block that display what you want for the

user/* pages 

.

It can also be dynamic if you create a view block with a contextual view displaying other user fields.

1
  • This solution is incomplete as the title in html header and in breadcrumb remain the inchanged
    – cedric_a
    Commented Jan 11, 2023 at 10:29

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.