Updated:
I would like data from the users.data
table displayed with every "User Name" however it is displayed.
As an example, I have this in hook_preprocess_page_title()
:
function hook_preprocess_page_title(&$variables) {
if (\Drupal::routeMatch()->getRouteName() == 'entity.user.canonical') {
$gUD = \Drupal::service('user.data');
$routeUID = \Drupal::routeMatch()->getRawParameter('user');
$gudValue = $gUD->get('module', $routeUID, 'name');
if ($gudValue = $moduleValue)) {
kpr('Success!');
}
}
}
The route entity.user.canonical
allows me to reference the viewed user, and so I can use \Drupal::routeMatch()->getRawParameter('user')
to get the non-current user ID. (it's not the route that counts, but my ability to reference the users.data
table for the displayed user)
And this in hook_preprocess_username()
:
function hook_preprocess_username(&$variables) {
$gUD = getUsersData();
$gudValue = $gUD->get('module', $variables['uid'], 'name');
}
So what I am looking for is something like:
hook_preprocess_HOOK() {
$UID = // From displayed user name.
\Drupal::service('user.data')->get('module', $UID, 'name');
}
for each displayed user name.
The problem here: get user id from username or email without logging in, is that I don't know, and cannot manually enter the username value.
A couple of examples where the username is displayed on a page without using the username
field:
- Views field Name (not linked to user, uses
views field
base) - User type entity reference field (format display
label
, usesfield
base)
hook_preprocess_page_title()
and hook_preprocess_username()
are pretty straight-forward. And I believe I know where/when this template/hook will be used, but I am still having trouble defining a couple of other instances, and determining the best hook to alter the output as needed.
loadByProperties()
queries the entire user table. Why should it be restricted to current user values?username
from a preprocess function, without a route parameter of.user.
, I have not found a way to set that value to something other than the current user.format
option of the user entity reference field toAuthor
, instead oflabel
on my content type, and now actually renders out the username field value,hook_preprocess_username(&$variables)
does return$variables['name']
relative to the referenced user, not the current user. My error? Maybe, but now I'm unsure about `label format display.