2

Drupal 8.x

I am currently using hook_preprocess_user().

I would like to alter the username to add some custom markup to every username.

MYMODULE.module:

function MYMODULE_preprocess_user(&$variables) {
  $variables['elements']['#user']->name->value = t('Name @newMarkup', ['@newMarkup' => ' Hello']);
}

This returns Name Hello Hello. Concatenation adds 'Hello' twice so this approach is not working.

I've also worked with user_format_name_alter(&$name, $account), however, this does not seem to fit my use case.

How can I alter the username text? Not looking for a "currentUser" solution, but every user so names are changed everywhere. Reference, Views, etc.

  • In a views preview at /admin/structure/views/view/view_name and a views page, it appears to render ok. I have been testing via a referenced views block. – Prestosaurus Dec 1 '18 at 5:52
3

Seems hook_user_format_name_alter() is your best bet. But same as in the other answer markup doesn't seem to be allowed everywhere. Normally the user name is only allowed to be a string.

The following won't work for Views for example. It will print just a string.

use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Implements hook_user_format_name_alter().
 */
function MYMODULE_user_format_name_alter(&$name, $account) {

  $name = new TranslatableMarkup('@name <span class="foo-bar">Foo Bar</span>', ['@name' => $name]);
}

/**
 * Implements hook_preprocess_HOOK().
 */
function MYMODULE_preprocess_page_title(&$variables) {

  if (\Drupal::routeMatch()->getRouteName() == 'entity.user.canonical') {

    $name = $variables['title']['#markup']->__toString();
    $variables['title'] = new TranslatableMarkup('@name <span class="foo-bar">Foo Bar</span>', ['@name' => $name]);
  }
}

So, what I'd recommend now is, you maybe take the *_preprocess_page_title hook – as this is working just fine on user pages – and for all other places (references, Views etc.) you maybe create a new custom formatter or pseudo field to do the job.

  • 1
    +1 ^. I've noticed a lot of different implementations of the "username". For example a view of users displaying fields, and the field username. If you link that field to the user it will use username.html.twig template, if you do not link the field it uses views--field.html.twig template. So far it seems whether with preprocess or template overrides, you need multiple implementations to get all username instances... The other side of the problem is that hook_preprocess_user() gets rendered multiple times in some instances. – Prestosaurus Dec 3 '18 at 16:54
0
<?php
use Drupal\user\Entity\User;

// Updating a user is a three step process:
// 1) load the user object to change
// 2) set property/field to new value
// 3) Save the user object.

// This example updates the user name.

// $uid is the user id of the user user update
$user = \Drupal\user\Entity\User::load($uid);

// Don't forget to save the user, we'll do that at the very end of code.

// Modify username
$username = $user->getUsername();
$username .= " Hello";
$user->setUsername($username); // string $username: The new user name.

// The crucial part! Save the $user object, else changes won't persist.
$user->save();

// Congratulations, you have updated a user!

I based this on the examples in this Github gist:

https://gist.github.com/dreambubbler/671afd7f962ae46687e41340b396d266

  • 1
    Seems you are actually recommending to fetch all users and re-save them with an updated user name. In a batch maybe, on hook_update_N? And this doesn't append markup. Only strings seem to be allowed. – leymannx Dec 1 '18 at 2:16
  • 1
    Good point -- this is wrong. The question is just how to alter the markup, not the actual user object. So this doesn't answer the question at all. Hopefully someone else can do a better job! (I'll just leave this here rather than deleting it, because it does something interesting and somewhat related.) – hotwebmatter Dec 1 '18 at 2:20

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.