function WorkspacesUiHooks::toolbar

Implements hook_toolbar().

Attributes

#[Hook('toolbar')]

File

core/modules/workspaces_ui/src/Hook/WorkspacesUiHooks.php, line 38

Class

WorkspacesUiHooks
Hook implementations for the workspaces_ui module.

Namespace

Drupal\workspaces_ui\Hook

Code

public function toolbar() : array {
  $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' => [
      '#lazy_builder' => [
        'workspaces.lazy_builders:renderToolbarTab',
        [],
      ],
      '#create_placeholder' => TRUE,
      '#lazy_builder_preview' => [
        '#type' => 'link',
        '#title' => $active_workspace ? $active_workspace->label() : $this->t('Live'),
        '#url' => Url::fromRoute('entity.workspace.collection'),
        '#attributes' => [
          'class' => [
            'toolbar-tray-lazy-placeholder-link',
          ],
        ],
      ],
    ],
    '#wrapper_attributes' => [
      'class' => [
        'workspaces-toolbar-tab',
      ],
    ],
    '#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';
  }
  // \Drupal\toolbar\Element\ToolbarItem::preRenderToolbarItem adds an
  // #attributes property to each toolbar item's tab child automatically. Lazy
  // builders don't support an #attributes property so we need to add another
  // render callback to remove the #attributes property. We start by adding
  // the defaults, and then we append our own pre render callback.
  $items['workspace'] += \Drupal::service('plugin.manager.element_info')->getInfo('toolbar_item');
  $items['workspace']['#pre_render'][] = 'workspaces.lazy_builders:removeTabAttributes';
  return $items;
}

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