class UserToolbarLinkBuilder
Fills out the placeholders generated in ToolbarHooks::userToolbar().
Hierarchy
- class \Drupal\toolbar\UserToolbarLinkBuilder implements \Drupal\Core\Security\TrustedCallbackInterface uses \Drupal\Core\StringTranslation\StringTranslationTrait
Expanded class hierarchy of UserToolbarLinkBuilder
1 file declares its use of UserToolbarLinkBuilder
- UserToolbarLinkBuilderTest.php in core/
modules/ toolbar/ tests/ src/ Unit/ UserToolbarLinkBuilderTest.php
1 string reference to 'UserToolbarLinkBuilder'
- toolbar.services.yml in core/
modules/ toolbar/ toolbar.services.yml - core/modules/toolbar/toolbar.services.yml
1 service uses UserToolbarLinkBuilder
- toolbar.user_link_builder in core/
modules/ toolbar/ toolbar.services.yml - Drupal\toolbar\UserToolbarLinkBuilder
File
-
core/
modules/ toolbar/ src/ UserToolbarLinkBuilder.php, line 13
Namespace
Drupal\toolbarView source
class UserToolbarLinkBuilder implements TrustedCallbackInterface {
use StringTranslationTrait;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $account;
/**
* Constructs a UserToolbarLinkBuilder object.
*
* @param \Drupal\Core\Session\AccountProxyInterface $account
* The current user.
*/
public function __construct(AccountProxyInterface $account) {
$this->account = $account;
}
/**
* Lazy builder callback for rendering toolbar links.
*
* @return array
* A renderable array as expected by the renderer service.
*/
public function renderToolbarLinks() {
$links = [
'account' => [
'title' => $this->t('View profile'),
'url' => Url::fromRoute('user.page'),
'attributes' => [
'title' => $this->t('User account'),
],
],
'account_edit' => [
'title' => $this->t('Edit profile'),
'url' => Url::fromRoute('entity.user.edit_form', [
'user' => $this->account
->id(),
]),
'attributes' => [
'title' => $this->t('Edit user account'),
],
],
'logout' => [
'title' => $this->t('Log out'),
'url' => Url::fromRoute('user.logout'),
],
];
$build = [
'#theme' => 'links__toolbar_user',
'#links' => $links,
'#attributes' => [
'class' => [
'toolbar-menu',
],
],
'#cache' => [
'contexts' => [
'user',
],
],
];
return $build;
}
/**
* Lazy builder callback for rendering the username.
*
* @return array
* A renderable array as expected by the renderer service.
*/
public function renderDisplayName() {
return [
'#plain_text' => $this->account
->getDisplayName(),
];
}
/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return [
'renderToolbarLinks',
'renderDisplayName',
];
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.