function workspaces_toolbar

Same name and namespace in other branches
  1. 8.9.x core/modules/workspaces/workspaces.module \workspaces_toolbar()
  2. 10 core/modules/workspaces/workspaces.module \workspaces_toolbar()
  3. 11.x core/modules/workspaces/workspaces.module \workspaces_toolbar()

Implements hook_toolbar().

File

core/modules/workspaces/workspaces.module, line 187

Code

function workspaces_toolbar() {
    $items['workspace'] = [
        '#cache' => [
            'contexts' => [
                'user.permissions',
            ],
        ],
    ];
    $current_user = \Drupal::currentUser();
    if (!$current_user->hasPermission('administer workspaces') && !$current_user->hasPermission('view own workspace') && !$current_user->hasPermission('view any workspace')) {
        return $items;
    }
    
    /** @var \Drupal\workspaces\WorkspaceInterface $active_workspace */
    $active_workspace = \Drupal::service('workspaces.manager')->getActiveWorkspace();
    $items['workspace'] += [
        '#type' => 'toolbar_item',
        'tab' => [
            '#type' => 'link',
            '#title' => $active_workspace ? $active_workspace->label() : t('Live'),
            '#url' => Url::fromRoute('entity.workspace.collection', [], [
                'query' => \Drupal::destination()->getAsArray(),
            ]),
            '#attributes' => [
                'title' => t('Switch workspace'),
                'class' => [
                    'use-ajax',
                    'toolbar-icon',
                    'toolbar-icon-workspace',
                ],
                'data-dialog-type' => 'dialog',
                'data-dialog-renderer' => 'off_canvas_top',
                'data-dialog-options' => Json::encode([
                    'height' => 161,
                    'classes' => [
                        'ui-dialog' => 'workspaces-dialog',
                    ],
                ]),
            ],
            '#cache' => [
                'tags' => $active_workspace ? $active_workspace->getCacheTags() : [],
            ],
        ],
        '#wrapper_attributes' => [
            'class' => [
                'workspaces-toolbar-tab',
            ],
        ],
        '#attached' => [
            'library' => [
                'workspaces/drupal.workspaces.toolbar',
            ],
        ],
        '#weight' => 500,
    ];
    // Add a special class to the wrapper if we don't have an active workspace so
    // we can highlight it with a different color.
    if (!$active_workspace) {
        $items['workspace']['#wrapper_attributes']['class'][] = 'workspaces-toolbar-tab--is-default';
    }
    return $items;
}

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