By syedfarhansajid on
Since menu templates do not have direct access to the user object, we need to pass it manually in my_theme.theme
function my_theme_preprocess_menu(&$variables) {
$current_user = \Drupal::currentUser();
$variables['current_user_name'] = $current_user->getDisplayName();
}
Now, you can use it inside menu.html.twig
<p>Hello, {{ current_user_name }}!</p>
Comments
Pretty sure this will fail as
Pretty sure this will fail as soon as any caching is enabled. For that to work you'd have to add the necessary caching information and return a render array instead of just the user name value, see: https://www.drupal.org/docs/drupal-apis/render-api/cacheability-of-rende...