class WorkspacesUiToolbarLinkBuilder

Fills out the placeholder generated in ToolbarHooks::workspacesUiToolbar().

The service is always registered, but the workspace manager it depends on only exists when the Workspaces module is installed, which is also the only situation in which the toolbar adds the workspaces tab.

@internal

Hierarchy

Expanded class hierarchy of WorkspacesUiToolbarLinkBuilder

1 string reference to 'WorkspacesUiToolbarLinkBuilder'
toolbar.services.yml in core/modules/toolbar/toolbar.services.yml
core/modules/toolbar/toolbar.services.yml
1 service uses WorkspacesUiToolbarLinkBuilder
toolbar.workspaces_ui_link_builder in core/modules/toolbar/toolbar.services.yml
Drupal\toolbar\WorkspacesUiToolbarLinkBuilder

File

core/modules/toolbar/src/WorkspacesUiToolbarLinkBuilder.php, line 23

Namespace

Drupal\toolbar
View source
final class WorkspacesUiToolbarLinkBuilder implements TrustedCallbackInterface {
  use StringTranslationTrait;
  public function __construct(protected readonly ?WorkspaceManagerInterface $workspaceManager, protected readonly ElementInfoManagerInterface $elementInfo) {
  }
  
  /**
   * Lazy builder callback for rendering the workspace toolbar tab.
   *
   * @return array
   *   A render array.
   */
  public function renderToolbarTab() : array {
    $active_workspace = $this->workspaceManager
      ->getActiveWorkspace();
    $build = [
      '#type' => 'link',
      '#title' => $active_workspace ? $active_workspace->label() : $this->t('Live'),
      '#url' => Url::fromRoute('entity.workspace.collection', [], [
        'query' => \Drupal::destination()->getAsArray(),
      ]),
      '#attributes' => [
        'title' => $this->t('Switch workspace'),
        'class' => [
          'toolbar-item',
          'toolbar-icon',
          'toolbar-icon-workspace',
          'use-ajax',
        ],
        'data-dialog-type' => 'dialog',
        'data-dialog-renderer' => 'off_canvas_top',
        'data-dialog-options' => Json::encode([
          'height' => 161,
          'classes' => [
            'ui-dialog' => 'workspaces-dialog',
          ],
        ]),
      ],
      '#attached' => [
        'library' => [
          'toolbar/toolbar.workspaces_ui',
        ],
      ],
      '#cache' => [
        'max-age' => 0,
      ],
    ];
    // The renderer has already added element defaults by the time the lazy
    // builder is run.
    // @see https://www.drupal.org/project/drupal/issues/2609250
    $build += $this->elementInfo
      ->getInfo('link');
    return $build;
  }
  
  /**
   * Render callback for the workspace toolbar tab.
   */
  public static function removeTabAttributes(array $element) : array {
    unset($element['tab']['#attributes']);
    return $element;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() : array {
    return [
      'removeTabAttributes',
      'renderToolbarTab',
    ];
  }

}

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