class WorkspacesLazyBuilders

Same name in other branches
  1. 11.x core/modules/workspaces/src/WorkspacesLazyBuilders.php \Drupal\workspaces\WorkspacesLazyBuilders

Defines a service for workspaces #lazy_builder callbacks.

@internal

Hierarchy

Expanded class hierarchy of WorkspacesLazyBuilders

1 string reference to 'WorkspacesLazyBuilders'
workspaces.services.yml in core/modules/workspaces/workspaces.services.yml
core/modules/workspaces/workspaces.services.yml
1 service uses WorkspacesLazyBuilders
workspaces.lazy_builders in core/modules/workspaces/workspaces.services.yml
Drupal\workspaces\WorkspacesLazyBuilders

File

core/modules/workspaces/src/WorkspacesLazyBuilders.php, line 18

Namespace

Drupal\workspaces
View source
final class WorkspacesLazyBuilders implements TrustedCallbackInterface {
    use StringTranslationTrait;
    public function __construct(WorkspaceManagerInterface $workspaceManager, 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' => 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' => [
                    'workspaces/drupal.workspaces.toolbar',
                ],
            ],
            '#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 Deprecated 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.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING Deprecated constant Untrusted callbacks trigger E_USER_WARNING errors.
WorkspacesLazyBuilders::removeTabAttributes public static function Render callback for the workspace toolbar tab.
WorkspacesLazyBuilders::renderToolbarTab public function Lazy builder callback for rendering the workspace toolbar tab.
WorkspacesLazyBuilders::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
WorkspacesLazyBuilders::__construct public function

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