function PreprocessHooks::toolbarUserPicture
Same name and namespace in other branches
- 11.x core/themes/admin/src/Hook/PreprocessHooks.php \Drupal\admin\Hook\PreprocessHooks::toolbarUserPicture()
Lazy builder callback for the user picture.
Attributes
#[Hook('preprocess_toolbar_user_picture')]
File
-
core/
themes/ admin/ src/ Hook/ PreprocessHooks.php, line 1249
Class
- PreprocessHooks
- Provides preprocess implementations.
Namespace
Drupal\admin\HookCode
public function toolbarUserPicture() : array {
$user = $this->entityTypeManager
->getStorage('user')
->load($this->currentUser
->id());
$url = $user->toUrl();
// If the user is anonymous, we cannot link to the user profile.
if ($user->isAnonymous()) {
$url = Url::fromUri('route:<nolink>');
}
$build = [
'#type' => 'link',
'#url' => $url,
'#title' => [
'#markup' => $user->getDisplayName(),
],
'#attributes' => [
'id' => 'toolbar-item-user-secondary',
'class' => [
'toolbar-icon',
'toolbar-icon-user',
'trigger',
'toolbar-item',
],
'role' => 'button',
],
];
/** @var \Drupal\image\ImageStyleInterface|null $style */
$style = NULL;
try {
$style = $this->entityTypeManager
->getStorage('image_style')
->load('thumbnail');
} catch (PluginNotFoundException) {
// The image style plugin does not exists. $style stays NULL and no user
// picture will be added.
}
if ($style === NULL) {
return [
'link' => $build,
];
}
/** @var \Drupal\file\FileInterface|null $file */
$file = $user->user_picture->entity;
if ($file === NULL) {
return [
'link' => $build,
];
}
$image_url = $style->buildUrl($file->getFileUri());
$build['#attributes']['class'] = [
'toolbar-item icon-user',
];
$build['#title'] = [
'#type' => 'html_tag',
'#tag' => 'img',
'#attributes' => [
'src' => $image_url,
'alt' => $user->getAccountName(),
'class' => [
'icon-user__image',
],
],
];
return [
'link' => $build,
];
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.