You are here

function user_toolbar in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/user.module \user_toolbar()
  2. 9 core/modules/user/user.module \user_toolbar()

Implements hook_toolbar().

File

core/modules/user/user.module, line 1136
Enables the user registration and login system.

Code

function user_toolbar() {
  $user = \Drupal::currentUser();
  $items['user'] = [
    '#type' => 'toolbar_item',
    'tab' => [
      '#type' => 'link',
      '#title' => $user
        ->getDisplayName(),
      '#url' => Url::fromRoute('user.page'),
      '#attributes' => [
        'title' => t('My account'),
        'class' => [
          'toolbar-icon',
          'toolbar-icon-user',
        ],
      ],
      '#cache' => [
        // Vary cache for anonymous and authenticated users.
        'contexts' => [
          'user.roles:anonymous',
        ],
      ],
    ],
    'tray' => [
      '#heading' => t('User account actions'),
    ],
    '#weight' => 100,
    '#attached' => [
      'library' => [
        'user/drupal.user.icons',
      ],
    ],
  ];
  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']['tab']['#title'] = [
      '#lazy_builder' => [
        'user.toolbar_link_builder:renderDisplayName',
        [],
      ],
      '#create_placeholder' => TRUE,
    ];
    $items['user']['tray']['user_links'] = [
      '#lazy_builder' => [
        'user.toolbar_link_builder:renderToolbarLinks',
        [],
      ],
      '#create_placeholder' => TRUE,
    ];
  }
  return $items;
}