Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 namespace Drupal\user;
|
Chris@14
|
4
|
Chris@14
|
5 use Drupal\Core\Session\AccountProxyInterface;
|
Chris@14
|
6 use Drupal\Core\StringTranslation\StringTranslationTrait;
|
Chris@14
|
7 use Drupal\Core\Url;
|
Chris@14
|
8
|
Chris@14
|
9 /**
|
Chris@14
|
10 * ToolbarLinkBuilder fills out the placeholders generated in user_toolbar().
|
Chris@14
|
11 */
|
Chris@14
|
12 class ToolbarLinkBuilder {
|
Chris@14
|
13
|
Chris@14
|
14 use StringTranslationTrait;
|
Chris@14
|
15
|
Chris@14
|
16 /**
|
Chris@14
|
17 * The current user.
|
Chris@14
|
18 *
|
Chris@14
|
19 * @var \Drupal\Core\Session\AccountProxyInterface
|
Chris@14
|
20 */
|
Chris@14
|
21 protected $account;
|
Chris@14
|
22
|
Chris@14
|
23 /**
|
Chris@14
|
24 * ToolbarHandler constructor.
|
Chris@14
|
25 *
|
Chris@14
|
26 * @param \Drupal\Core\Session\AccountProxyInterface $account
|
Chris@14
|
27 * The current user.
|
Chris@14
|
28 */
|
Chris@14
|
29 public function __construct(AccountProxyInterface $account) {
|
Chris@14
|
30 $this->account = $account;
|
Chris@14
|
31 }
|
Chris@14
|
32
|
Chris@14
|
33 /**
|
Chris@14
|
34 * Lazy builder callback for rendering toolbar links.
|
Chris@14
|
35 *
|
Chris@14
|
36 * @return array
|
Chris@14
|
37 * A renderable array as expected by the renderer service.
|
Chris@14
|
38 */
|
Chris@14
|
39 public function renderToolbarLinks() {
|
Chris@14
|
40 $links = [
|
Chris@14
|
41 'account' => [
|
Chris@14
|
42 'title' => $this->t('View profile'),
|
Chris@14
|
43 'url' => Url::fromRoute('user.page'),
|
Chris@14
|
44 'attributes' => [
|
Chris@14
|
45 'title' => $this->t('User account'),
|
Chris@14
|
46 ],
|
Chris@14
|
47 ],
|
Chris@14
|
48 'account_edit' => [
|
Chris@14
|
49 'title' => $this->t('Edit profile'),
|
Chris@14
|
50 'url' => Url::fromRoute('entity.user.edit_form', ['user' => $this->account->id()]),
|
Chris@14
|
51 'attributes' => [
|
Chris@14
|
52 'title' => $this->t('Edit user account'),
|
Chris@14
|
53 ],
|
Chris@14
|
54 ],
|
Chris@14
|
55 'logout' => [
|
Chris@14
|
56 'title' => $this->t('Log out'),
|
Chris@14
|
57 'url' => Url::fromRoute('user.logout'),
|
Chris@14
|
58 ],
|
Chris@14
|
59 ];
|
Chris@14
|
60 $build = [
|
Chris@14
|
61 '#theme' => 'links__toolbar_user',
|
Chris@14
|
62 '#links' => $links,
|
Chris@14
|
63 '#attributes' => [
|
Chris@14
|
64 'class' => ['toolbar-menu'],
|
Chris@14
|
65 ],
|
Chris@14
|
66 '#cache' => [
|
Chris@14
|
67 'contexts' => ['user'],
|
Chris@14
|
68 ],
|
Chris@14
|
69 ];
|
Chris@14
|
70
|
Chris@14
|
71 return $build;
|
Chris@14
|
72 }
|
Chris@14
|
73
|
Chris@14
|
74 /**
|
Chris@14
|
75 * Lazy builder callback for rendering the username.
|
Chris@14
|
76 *
|
Chris@14
|
77 * @return array
|
Chris@14
|
78 * A renderable array as expected by the renderer service.
|
Chris@14
|
79 */
|
Chris@14
|
80 public function renderDisplayName() {
|
Chris@14
|
81 return [
|
Chris@14
|
82 '#markup' => $this->account->getDisplayName(),
|
Chris@14
|
83 ];
|
Chris@14
|
84 }
|
Chris@14
|
85
|
Chris@14
|
86 }
|