summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/navigation/src/ShortcutLazyBuilder.php
blob: 121c29f7e865b21666597e5d1e13446967b949d7 (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
<?php

declare(strict_types=1);

namespace Drupal\navigation;

use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\shortcut\ShortcutLazyBuilders;

/**
 * Lazy Builders for Navigation shortcuts links.
 *
 * @internal The navigation module is experimental.
 * @see \Drupal\shortcut\ShortcutLazyBuilders
 */
final class ShortcutLazyBuilder implements TrustedCallbackInterface {

  /**
   * Constructs a ShortcutLazyBuilders object.
   *
   * @param \Drupal\shortcut\ShortcutLazyBuilders $shortcutLazyBuilder
   *   The original shortcuts lazy builder service.
   */
  public function __construct(
    protected readonly ShortcutLazyBuilders $shortcutLazyBuilder,
  ) {}

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return ['lazyLinks'];
  }

  /**
   * The #lazy_builder callback; builds shortcut navigation links.
   *
   * @param string $label
   *   (Optional) The links label. Defaults to "Shortcuts".
   *
   * @return array
   *   A renderable array of shortcut links.
   */
  public function lazyLinks(string $label = 'Shortcuts') {
    $shortcut_links = $this->shortcutLazyBuilder->lazyLinks();

    if (empty($shortcut_links['shortcuts']['#links'])) {
      return [
        '#cache' => $shortcut_links['#cache'],
      ];
    }
    $shortcuts_items = [
      [
        'title' => $label,
        'class' => 'shortcuts',
        'icon' => [
          'icon_id' => 'shortcuts',
        ],
        'below' => $shortcut_links['shortcuts']['#links'],
      ],
    ];

    return [
      '#theme' => 'navigation_menu',
      '#menu_name' => 'shortcuts',
      '#title' => $label,
      '#items' => $shortcuts_items,
      '#cache' => $shortcut_links['#cache'],
    ];
  }

}