diff --git a/core/modules/user/src/ToolbarHandler.php b/core/modules/user/src/ToolbarHandler.php new file mode 100644 index 0000000000..2ade366c02 --- /dev/null +++ b/core/modules/user/src/ToolbarHandler.php @@ -0,0 +1,85 @@ +account = $account; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('current_user') + ); + } + + /** + * Lazy builder callback for the toolbar user items. + * + * @return array + * A renderable array as expected by the renderer service. + */ + public function lazyBuilder() { + $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; + } + +} diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 19b9527a21..e9f971c643 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -9,7 +9,6 @@ use Drupal\Component\Render\PlainTextOutput; use Drupal\Component\Utility\Unicode; use Drupal\Core\Asset\AttachedAssetsInterface; -use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Render\Element; @@ -1320,72 +1319,24 @@ function user_cookie_delete($cookie_name) { function user_toolbar() { $user = \Drupal::currentUser(); - // Add logout & user account links or login link. - $links_cache_contexts = []; - if ($user->isAuthenticated()) { - $links = [ - 'account' => [ - 'title' => t('View profile'), - 'url' => Url::fromRoute('user.page'), - 'attributes' => [ - 'title' => t('User account'), - ], - ], - 'account_edit' => [ - 'title' => t('Edit profile'), - 'url' => Url::fromRoute('entity.user.edit_form', ['user' => $user->id()]), - 'attributes' => [ - 'title' => t('Edit user account'), - ], - ], - 'logout' => [ - 'title' => t('Log out'), - 'url' => Url::fromRoute('user.logout'), - ], - ]; - // The "Edit user account" link is per-user. - $links_cache_contexts[] = 'user'; - } - else { - $links = [ - 'login' => [ - 'title' => t('Log in'), - 'url' => Url::fromRoute('user.page'), - ], - ]; - } - $items['user'] = [ '#type' => 'toolbar_item', 'tab' => [ '#type' => 'link', - '#title' => $user->getDisplayName(), + '#title' => $user->isAnonymous() ? $user->getDisplayName() : t('My account'), '#url' => Url::fromRoute('user.page'), '#attributes' => [ 'title' => t('My account'), 'class' => ['toolbar-icon', 'toolbar-icon-user'], ], '#cache' => [ - 'contexts' => [ - // Cacheable per user, because the current user's name is shown. - 'user', - ], + // Cacheable per "anonymous or not", because the links to + // display depend on that. + 'contexts' => ['user.roles:anonymous'], ], ], 'tray' => [ '#heading' => t('User account actions'), - 'user_links' => [ - '#cache' => [ - // Cacheable per "authenticated or not", because the links to - // display depend on that. - 'contexts' => Cache::mergeContexts(['user.roles:authenticated'], $links_cache_contexts), - ], - '#theme' => 'links__toolbar_user', - '#links' => $links, - '#attributes' => [ - 'class' => ['toolbar-menu'], - ], - ], ], '#weight' => 100, '#attached' => [ @@ -1395,6 +1346,28 @@ function user_toolbar() { ], ]; + if ($user->isAnonymous()) { + $links = [ + 'login' => [ + 'title' => t('Log in'), + 'url' => Url::fromRoute('user.page'), + ], + ]; + $items['user']['tray']['user_links'] = [ + '#theme' => 'links__toolbar_user', + '#links' => $links, + '#attributes' => [ + 'class' => ['toolbar-menu'], + ], + ]; + } + else { + $items['user']['tray']['user_links'] = [ + '#lazy_builder' => ['user.toolbar_handler:lazyBuilder', []], + '#create_placeholder' => TRUE, + ]; + } + return $items; } diff --git a/core/modules/user/user.services.yml b/core/modules/user/user.services.yml index c273cb3443..dfbbe0fa12 100644 --- a/core/modules/user/user.services.yml +++ b/core/modules/user/user.services.yml @@ -66,6 +66,9 @@ services: arguments: ['@current_user', '@entity.manager'] tags: - { name: 'context_provider' } + user.toolbar_handler: + class: Drupal\user\ToolbarHandler + arguments: ['@current_user'] parameters: user.tempstore.expire: 604800