diff core/modules/user/src/ToolbarLinkBuilder.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/modules/user/src/ToolbarLinkBuilder.php	Thu Jul 05 14:24:15 2018 +0000
@@ -0,0 +1,86 @@
+<?php
+
+namespace Drupal\user;
+
+use Drupal\Core\Session\AccountProxyInterface;
+use Drupal\Core\StringTranslation\StringTranslationTrait;
+use Drupal\Core\Url;
+
+/**
+ * ToolbarLinkBuilder fills out the placeholders generated in user_toolbar().
+ */
+class ToolbarLinkBuilder {
+
+  use StringTranslationTrait;
+
+  /**
+   * The current user.
+   *
+   * @var \Drupal\Core\Session\AccountProxyInterface
+   */
+  protected $account;
+
+  /**
+   * ToolbarHandler constructor.
+   *
+   * @param \Drupal\Core\Session\AccountProxyInterface $account
+   *   The current user.
+   */
+  public function __construct(AccountProxyInterface $account) {
+    $this->account = $account;
+  }
+
+  /**
+   * Lazy builder callback for rendering toolbar links.
+   *
+   * @return array
+   *   A renderable array as expected by the renderer service.
+   */
+  public function renderToolbarLinks() {
+    $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;
+  }
+
+  /**
+   * 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(),
+    ];
+  }
+
+}