class WorkspacesUiToolbarLinkBuilder

Same name and namespace in other branches
  1. 11.x core/modules/toolbar/src/WorkspacesUiToolbarLinkBuilder.php \Drupal\toolbar\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',
    ];
  }

}

Members

Title Sort descending 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. 1
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
WorkspacesUiToolbarLinkBuilder::removeTabAttributes public static function Render callback for the workspace toolbar tab.
WorkspacesUiToolbarLinkBuilder::renderToolbarTab public function Lazy builder callback for rendering the workspace toolbar tab.
WorkspacesUiToolbarLinkBuilder::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
WorkspacesUiToolbarLinkBuilder::__construct public function

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