user_toolbar()
Implements hook_toolbar().
File
- core/modules/user/user.module, line 1307
- Enables the user registration and login system.
Code
function user_toolbar() { $user = \Drupal::currentUser(); // Add logout & user account links or login link. $links_cache_contexts = []; if ($user->isAuthenticated()) { $links = array( 'account' => array( 'title' => t('View profile'), 'url' => Url::fromRoute('user.page'), 'attributes' => array( 'title' => t('User account'), ), ), 'account_edit' => array( 'title' => t('Edit profile'), 'url' => Url::fromRoute('entity.user.edit_form', ['user' => $user->id()]), 'attributes' => array( 'title' => t('Edit user account'), ), ), 'logout' => array( 'title' => t('Log out'), 'url' => Url::fromRoute('user.logout'), ), ); // The "Edit user account" link is per-user. $links_cache_contexts[] = 'user'; } else { $links = array( 'login' => array( 'title' => t('Log in'), 'url' => Url::fromRoute('user.page'), ), ); } $items['user'] = array( '#type' => 'toolbar_item', 'tab' => array( '#type' => 'link', '#title' => $user->getDisplayName(), '#url' => Url::fromRoute('user.page'), '#attributes' => array( 'title' => t('My account'), 'class' => array('toolbar-icon', 'toolbar-icon-user'), ), '#cache' => [ 'contexts' => [ // Cacheable per user, because the current user's name is shown. 'user', ], ], ), 'tray' => array( '#heading' => t('User account actions'), 'user_links' => array( '#cache' => [ // Cacheable per "authenticated or not", because the links to // display depend on that. 'contexts' => Cache::mergeContexts(array('user.roles:authenticated'), $links_cache_contexts), ], '#theme' => 'links__toolbar_user', '#links' => $links, '#attributes' => array( 'class' => array('toolbar-menu'), ), ), ), '#weight' => 100, '#attached' => array( 'library' => array( 'user/drupal.user.icons', ), ), ); return $items; }
Please login to continue.