2

For various compatibility reasons, I would like my usernames to be standard no-spaces combinations, so jbloggs / jdoe / etc. This becomes unsightly when displaying blogs ("jdoe's blog") or node authorship ("15/09/2016 jbloggs").

I've added first/last name fields to the account, but can't find a way to have them displayed by default instead of the username. I am on Drupal 8, so Profile2 isn't yet ready, which means this answer isn't an option. Likewise, this answer proposes using the "Real Name" module, which doesn't have a D8 version.

If it's any help, I would basically like the full name used in every display context, so if there's a blanket solution that would do that, rather than giving me control by node-type, that would be more than acceptable.

Lastly, if the above isn't possible without a module, can someone point me to some info on referencing account/profile fields in twig? That way I could at least override it in the most prominent places using templates.

4

I'd add a new field called Display Name (or similar), then then use hook__user_format_name_alter() to load in that field, and change the display name to use the field value instead of the user name.

Here's the change record for D7 -> D8, where it used to be hook_username_alter(): https://www.drupal.org/node/1408514

  • Thanks for the tip on which hook to look at. Unfortunately, the $account variable passed in (type: UserSession object) only contains the standard fields and not the custom ones. Any ideas on how to get it to read in the additional data fields? – almcnicoll Sep 15 '16 at 23:34
  • OK, so I implemented it with a database query in the end. Probably not The Drupal Way, but it worked and that's what I needed. Would still love helpful comments about The Right Way To Do It! – almcnicoll Sep 16 '16 at 0:05
  • all you have to do is go to the user profiles configuration , then the manage fields section and add a text field named "name" , then for the display you can customize the template and output the name field where you want it to be – Malek Massoudi Sep 16 '16 at 0:09
  • 3
    what you have to do is load the user entity with User::load($account->id()) to be sure that you always have an entity with the fields. not that it might be the anonymous user as well. – Berdir Sep 16 '16 at 7:05
3

Drupal now has a module that does exactly what you're looking for, Real Name:

https://www.drupal.org/project/realname

I just used it recently. Once configured, it uses tokens to allow you to rewrite the username to appear pretty much any way you want it to.

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.