diff --git a/core/modules/user/src/ToolbarHandler.php b/core/modules/user/src/ToolbarHandler.php index 2ade366..b738174 100644 --- a/core/modules/user/src/ToolbarHandler.php +++ b/core/modules/user/src/ToolbarHandler.php @@ -2,16 +2,14 @@ namespace Drupal\user; -use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Url; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Toolbar integration handler. */ -class ToolbarHandler implements ContainerInjectionInterface { +class ToolbarHandler { use StringTranslationTrait; @@ -33,21 +31,12 @@ public function __construct(AccountProxyInterface $account) { } /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('current_user') - ); - } - - /** - * Lazy builder callback for the toolbar user items. + * Lazy builder callback for rendering toolbar links. * * @return array * A renderable array as expected by the renderer service. */ - public function lazyBuilder() { + public function renderToolbarLinks() { $links = [ 'account' => [ 'title' => $this->t('View profile'), @@ -82,4 +71,16 @@ public function lazyBuilder() { return $build; } + /** + * Lazy builder callback for rendering the username. + * + * @return array + * A renderable array as expected by the renderer service. + */ + public function renderDisplayName() { + return [ + '#markup' => $this->account->getDisplayName(), + ]; + } + } diff --git a/core/modules/user/user.module b/core/modules/user/user.module index e303de3..9994916 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -1331,15 +1331,14 @@ function user_toolbar() { '#type' => 'toolbar_item', 'tab' => [ '#type' => 'link', - '#title' => $user->isAnonymous() ? $user->getDisplayName() : t('My account'), + '#title' => $user->getDisplayName(), '#url' => Url::fromRoute('user.page'), '#attributes' => [ 'title' => t('My account'), 'class' => ['toolbar-icon', 'toolbar-icon-user'], ], '#cache' => [ - // Cacheable per "anonymous or not", because the links to - // display depend on that. + // Vary cache for anonymous and authenticated users. 'contexts' => ['user.roles:anonymous'], ], ], @@ -1370,8 +1369,12 @@ function user_toolbar() { ]; } else { + $items['user']['tab']['#title'] = [ + '#lazy_builder' => ['user.toolbar_handler:renderDisplayName', []], + '#create_placeholder' => TRUE, + ]; $items['user']['tray']['user_links'] = [ - '#lazy_builder' => ['user.toolbar_handler:lazyBuilder', []], + '#lazy_builder' => ['user.toolbar_handler:renderToolbarLinks', []], '#create_placeholder' => TRUE, ]; }