0

The following function resides in a .module file:

function user_menu_avatar_menu_links_discovered_alter(&$links) {

    $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
    $name = $user->get('name')->value;
    $userMenuAvatar = $user->get('user_picture')->entity->url();

    $links['user.page']['title'] = $name;

}

When logged in as another user, I am only getting the account info from the Admin user account. User name, email, etc..

What am I missing?

3

hook_menu_links_discovered_alter() is not invoked right before the links are shown, but when the links need to be rebuilt. Using the code you are showing, you are getting values from the user who is currently logged-in when the links are rebuilt; until the next rebuilt, that is the value shown for every user. (This means this is not the right method to dynamically change the menu link.)
If the links rebuilt is caused from a action you take when logged-in as administrator user, that explains why the name shown is taken from the administrator user.

If you need to alter the title used for a page, you need to alter the route and set its _title_callback property. The title callback is invoked right before the title is shown, so accessing there the object for the currently logged-in user would be correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.