Drupal - Get username in custom user twig template
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
{{ 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();