You are here

function workspace_toolbar in Workspace 8.2

Same name and namespace in other branches
  1. 8 workspace.module \workspace_toolbar()

Implements hook_toolbar().

File

./workspace.module, line 131
Provides full-site preview functionality for content staging.

Code

function workspace_toolbar() {
  $items = [];
  $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\workspace\WorkspaceInterface $active_workspace */
  $active_workspace = \Drupal::service('workspace.manager')
    ->getActiveWorkspace();
  $configure_link = NULL;
  if ($current_user
    ->hasPermission('administer workspaces')) {
    $configure_link = [
      '#type' => 'link',
      '#title' => t('Manage workspaces'),
      '#url' => $active_workspace
        ->toUrl('collection'),
      '#options' => [
        'attributes' => [
          'class' => [
            'manage-workspaces',
          ],
        ],
      ],
    ];
  }
  $items['workspace'] = [
    '#type' => 'toolbar_item',
    'tab' => [
      '#type' => 'link',
      '#title' => $active_workspace
        ->label(),
      '#url' => $active_workspace
        ->toUrl('collection'),
      '#attributes' => [
        'title' => t('Switch workspace'),
        'class' => [
          'toolbar-icon',
          'toolbar-icon-workspace',
        ],
      ],
    ],
    'tray' => [
      '#heading' => t('Workspaces'),
      'workspaces' => workspace_build_renderable_links(),
      'configure' => $configure_link,
    ],
    '#wrapper_attributes' => [
      'class' => [
        'workspace-toolbar-tab',
      ],
    ],
    '#attached' => [
      'library' => [
        'workspace/drupal.workspace.toolbar',
      ],
    ],
    '#weight' => 500,
  ];

  // Add a special class to the wrapper if we are in the default workspace so we
  // can highlight it with a different color.
  if ($active_workspace
    ->isDefaultWorkspace()) {
    $items['workspace']['#wrapper_attributes']['class'][] = 'workspace-toolbar-tab--is-default';
  }
  return $items;
}