class UserLazyBuilder

Same name and namespace in other branches
  1. 11.x core/modules/navigation/src/UserLazyBuilder.php \Drupal\navigation\UserLazyBuilder

User navigation block lazy builder.

@internal The navigation module is experimental.

Hierarchy

Expanded class hierarchy of UserLazyBuilder

1 string reference to 'UserLazyBuilder'
navigation.services.yml in core/modules/navigation/navigation.services.yml
core/modules/navigation/navigation.services.yml
1 service uses UserLazyBuilder
navigation.user_lazy_builder in core/modules/navigation/navigation.services.yml
Drupal\navigation\UserLazyBuilder

File

core/modules/navigation/src/UserLazyBuilder.php, line 18

Namespace

Drupal\navigation
View source
final class UserLazyBuilder implements TrustedCallbackInterface {
  use StringTranslationTrait;
  
  /**
   * Constructs an UserLazyBuilder object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler.
   * @param \Drupal\Core\Session\AccountProxyInterface $account
   *   The current user.
   */
  public function __construct(protected readonly ModuleHandlerInterface $moduleHandler, protected readonly AccountProxyInterface $account) {
  }
  
  /**
   * Lazy builder callback for rendering navigation links.
   *
   * @return array
   *   A renderable array as expected by the renderer service.
   */
  public function renderNavigationLinks() {
    return [
      '#help' => $this->moduleHandler
        ->moduleExists('help'),
      '#theme' => 'menu_region__footer',
      '#items' => $this->userOperationLinks(),
      '#menu_name' => 'user',
      '#title' => $this->account
        ->getDisplayName(),
      '#cache' => [
        'contexts' => [
          'user',
        ],
      ],
    ];
  }
  
  /**
   * Returns the user operation links in navigation expected format.
   *
   * @param bool $include_edit
   *   (Optional) Whether to include the edit account link or not.
   *
   * @return array
   *   List of operation links for the current user.
   */
  public function userOperationLinks(bool $include_edit = TRUE) : array {
    $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'),
      ],
    ];
    if (!$include_edit) {
      unset($links['account_edit']);
    }
    return $links;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'renderNavigationLinks',
    ];
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING Deprecated constant Untrusted callbacks trigger E_USER_WARNING errors.
UserLazyBuilder::renderNavigationLinks public function Lazy builder callback for rendering navigation links.
UserLazyBuilder::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
UserLazyBuilder::userOperationLinks public function Returns the user operation links in navigation expected format.
UserLazyBuilder::__construct public function Constructs an UserLazyBuilder object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.