summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/navigation/src/Plugin/Block/NavigationUserBlock.php
blob: 39c13acec221dbc94f952382b6e037e2bd050e1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php

declare(strict_types=1);

namespace Drupal\navigation\Plugin\Block;

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Menu\MenuLinkDefault;
use Drupal\Core\Menu\MenuLinkTreeElement;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Security\Attribute\TrustedCallback;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\user\Entity\User;

/**
 * Defines a user navigation block.
 *
 * @internal
 */
#[Block(
  id: 'navigation_user',
  admin_label: new TranslatableMarkup('User'),
)]
final class NavigationUserBlock extends BlockBase {

  const string NAVIGATION_LINKS_MENU = 'navigation-user-links';

  /**
   * {@inheritdoc}
   */
  public function build(): array {
    return [
      '#create_placeholder' => TRUE,
      '#lazy_builder' => [static::class . '::buildLinks', [$this->configuration['label']]],
      '#cache' => [
        'keys' => ['navigation_user_block'],
      ],
    ];
  }

  /**
   * Lazy builder callback.
   */
  #[TrustedCallback]
  public static function buildLinks(string $label): array {
    $parameters = new MenuTreeParameters();
    $parameters
      ->setMinDepth(0)
      ->setMaxDepth(2)
      ->onlyEnabledLinks();
    /** @var \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree */
    $menu_tree = \Drupal::service('navigation.menu_tree');
    $subtree = $menu_tree->load(static::NAVIGATION_LINKS_MENU, $parameters);

    // Load the current user so that they can be added as a cacheable dependency
    // of the final render array.
    $account = User::load(\Drupal::currentUser()->id());

    $menu_definition = [
      'menu_name' => static::NAVIGATION_LINKS_MENU,
      'route_name' => 'user.page',
      'route_parameters' => [],
      'title' => $account->getDisplayName(),
      'description' => '',
      'options' => [],
      'provider' => 'navigation',
      'enabled' => '1',
    ];
    // Create a parent link that serves as a wrapper.
    // If the menu is removed for any reason, this item shows a link to the
    // user profile page as a fallback.
    $link = MenuLinkDefault::create(\Drupal::getContainer(), [], 'navigation.user_links.user.wrapper', $menu_definition);
    $tree = new MenuLinkTreeElement($link, TRUE, 1, FALSE, $subtree);

    $manipulators = [
      ['callable' => 'menu.default_tree_manipulators:checkAccess'],
      ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
    ];
    $tree = $menu_tree->transform([$tree], $manipulators);
    $build = $menu_tree->build($tree);
    $build['#title'] = $label;
    $build['#cache']['contexts'][] = 'user';
    $cacheable_metadata = CacheableMetadata::createFromRenderArray($build);
    $cacheable_metadata->addCacheableDependency($account);
    $cacheable_metadata->applyTo($build);

    return $build;
  }

}