8

I have a twig template for the compact display of a user where I am able to output the defined content fields. Is there a way to output the username of that user?

0

3 Answers 3

12

{{ user.name.0.value }}

should give you what you want.

The preferred way is inject the username as a variable in a preprocess function. To do so, tweak that in your theme's .theme file

<?php

/**
 * Implements hook_preprocess_user().
 */
function yourtheme_preprocess_user(&$variables) {
  /** @var User $account */
  $account = $variables['elements']['#user'];

  $variables['username'] = $account->getDisplayName();
}

then in your user template you can use that as {{ username }}.

Edit:

To get the full URL:

  • In your template {{ path('entity.user.canonical', {'user': user.id}, {}) }}
  • In yourtheme_preprocess_user(): $variables['user_url'] = Url::fromRoute('entity.user.canonical', ['user' => $account->id()])->setAbsolute()->toString();
5
  • 2
    The later is preferred so that hook_user_format_name() runs to do anything that a site may want for usernames.
    – mpdonadio
    Commented Aug 5, 2016 at 13:22
  • Can the full URL of the user page be retreived in a similar way using the user preprocess? Commented Aug 5, 2016 at 15:10
  • Updated response to include 2 methods to get the user page url
    – zerolab
    Commented Aug 5, 2016 at 15:44
  • Thanks so much for your help. The template method works fine, but the preprocess gives me the following error: Recoverable fatal error: Object of class Drupal\Core\Field\FieldItemList could not be converted to string in Drupal\Core\Routing\UrlGenerator->doGenerate() (line 192 of core/lib/Drupal/Core/Routing/UrlGenerator.php). Commented Aug 5, 2016 at 16:02
  • if you use yourtheme_preprocess_user() solution, you need to add use Drupal\Core\Url; in your theme's .theme file. Commented May 17, 2022 at 21:13
20

The display name is not a field you can configure in {{ content }}. You can get it directly from the user entity:

{{ user.displayname }}

Reference for the php method: AccountInterface::getDisplayName

4
  • 3
    Should have more votes IMO. This is the equivalent of the preprocess function in the accepted answer but called from the Twig template.
    – star-szr
    Commented Sep 9, 2019 at 15:41
  • 2
    Definitely the best solution. Commented Apr 4, 2020 at 20:44
  • it works. but how can I prevent it's being cached. sometimes it's displaying prevoiusly logged in username. Or If I login with other user in other browser and come back and refresh here, the user name is getting changed. Commented Apr 15, 2022 at 12:04
  • 1
    @KrishnaMohan, the question is about the user template. If you have a different template this is probably not cached per user. By bubbling up a cache context you can cache per user: {{ {'#cache': {'contexts': ['user']}} }}. Caveat: Drupal is optimized to cache per user permissions. Adding a per user context to random templates could cause a severe impact on cache performance, depending on the amount of users registered on the site.
    – 4uk4
    Commented Apr 15, 2022 at 16:32
2

This answer is specifically for getting the username in menu.html.twig

The solution of passing the username through macro worked for me. Setting a variable and passing it into the macro is needed as these are out of scope of the macro in menu.html.twig

{% set username = user.account.name %}
{{ menus.menu_links(items, attributes, 0,logged_in,username) }}
{% macro menu_links(items, attributes, menu_level,logged_in,username) %}
{{ username }}
{% endmacro %}
1
  • This answer would be more helpful if it also showed how you setup the logged_in and username variables in a hook_preprocess_menu(). Commented Aug 29, 2023 at 19:47

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.