summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/navigation/src/WorkspacesLazyBuilder.php
blob: 146f7d83dc9b224073c9db61ed1be96b97e3cb43 (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
<?php

declare(strict_types=1);

namespace Drupal\navigation;

use Drupal\Component\Serialization\Json;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Security\Attribute\TrustedCallback;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\workspaces\WorkspaceManagerInterface;

/**
 * Defines a service for workspaces #lazy_builder callbacks.
 *
 * @internal
 */
final class WorkspacesLazyBuilder {

  use RedirectDestinationTrait;
  use StringTranslationTrait;

  public function __construct(
    protected WorkspaceManagerInterface $workspaceManager,
  ) {}

  /**
   * Lazy builder callback for rendering navigation links.
   *
   * @return array
   *   A renderable array as expected by the renderer service.
   */
  #[TrustedCallback]
  public function renderNavigationLinks(): array {
    $active_workspace = $this->workspaceManager->getActiveWorkspace();

    $url = Url::fromRoute('entity.workspace.collection', [], ['query' => $this->getDestinationArray()]);
    $url->setOption('attributes', [
      'class' => [
        $active_workspace ? 'toolbar-button--workspaces' : 'toolbar-button--workspaces--live',
        'use-ajax',
      ],
      'data-dialog-type' => 'dialog',
      'data-dialog-renderer' => 'off_canvas_top',
      'data-dialog-options' => Json::encode([
        'height' => 161,
        'classes' => [
          'ui-dialog' => 'workspaces-dialog',
        ],
      ]),
    ]);

    return [
      '#theme' => 'navigation_menu',
      '#title' => $this->t('Workspace'),
      '#items' => [
        [
          'title' => $active_workspace ? $active_workspace->label() : $this->t('Live'),
          'url' => $url,
          'class' => 'workspaces',
          'icon' => [
            'icon_id' => 'workspaces',
          ],
        ],
      ],
      '#attached' => [
        'library' => [
          'navigation/internal.navigation-workspaces',
          'workspaces/drupal.workspaces.off-canvas',
        ],
      ],
      '#cache' => [
        'max-age' => 0,
      ],
    ];
  }

}