You are here

public function Toolbar::toolbar in Workspace 8

Hook bridge; Responds to hook_toolbar().

See also

hook_toolbar().

File

src/Toolbar.php, line 70

Class

Toolbar
Service for hooks and utilities related to Toolbar integration.

Namespace

Drupal\workspace

Code

public function toolbar() {
  $items = [];
  $active = $this->workspaceManager
    ->getActiveWorkspace();
  $user = \Drupal::currentUser();
  $access = $user
    ->hasPermission('view_any_workspace');
  if ($access) {
    $items['changes'] = [
      '#type' => 'toolbar_item',
      '#weight' => 130,
      'tab' => [
        '#type' => 'link',
        '#title' => $this
          ->t('Changes'),
        '#url' => Url::fromRoute('entity.workspace.changes', [
          'workspace' => $active
            ->id(),
        ]),
        '#attributes' => [
          'title' => $this
            ->t('View changes on the current workspace'),
          'class' => [
            'toolbar-icon',
            'toolbar-icon-changes',
          ],
        ],
      ],
      '#wrapper_attributes' => [
        'class' => [
          'workspace-toolbar-tab',
        ],
      ],
      '#attached' => [
        'library' => [
          'workspace/drupal.workspace.toolbar',
        ],
      ],
    ];
  }
  $items['workspace_switcher'] = [
    // Include the toolbar_tab_wrapper to style the link like a toolbar tab.
    // Exclude the theme wrapper if custom styling is desired.
    '#type' => 'toolbar_item',
    '#weight' => 125,
    '#wrapper_attributes' => [
      'class' => [
        'workspace-toolbar-tab',
      ],
    ],
    '#attached' => [
      'library' => [
        'workspace/drupal.workspace.toolbar',
      ],
    ],
  ];
  $items['workspace_switcher']['tab'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('@active', [
      '@active' => $active
        ->label(),
    ]),
    '#url' => Url::fromRoute('entity.workspace.collection'),
    '#attributes' => [
      'title' => $this
        ->t('Switch workspaces'),
      'class' => [
        'toolbar-icon',
        'toolbar-icon-workspace',
      ],
    ],
  ];
  $create_link = [
    '#type' => 'link',
    '#title' => t('Add workspace'),
    '#url' => Url::fromRoute('entity.workspace.add'),
    '#options' => [
      'attributes' => [
        'class' => [
          'add-workspace',
        ],
      ],
    ],
  ];
  $items['workspace_switcher']['tray'] = [
    '#heading' => $this
      ->t('Switch to workspace'),
    '#pre_render' => [
      'workspace.toolbar:preRenderWorkspaceSwitcherForms',
    ],
    // This wil get filled in via pre-render.
    'workspace_forms' => [],
    'create_link' => $create_link,
    '#cache' => [
      'contexts' => $this->entityTypeManager
        ->getDefinition('workspace')
        ->getListCacheContexts(),
      'tags' => $this->entityTypeManager
        ->getDefinition('workspace')
        ->getListCacheTags(),
    ],
    '#attributes' => [
      'class' => [
        'toolbar-menu',
      ],
    ],
  ];
  $last_replication_failed = \Drupal::state()
    ->get('workspace.last_replication_failed', FALSE);
  $update_access = $user
    ->hasPermission('update any workspace from upstream');
  $has_upstream = isset($active->upstream) && !$active->upstream
    ->isEmpty();
  if ($has_upstream) {
    $update_in_queue = $this->entityTypeManager
      ->getStorage('replication')
      ->getQuery()
      ->condition('source', $active->upstream->target_id)
      ->condition('target', WorkspacePointer::loadFromWorkspace($active)
      ->id())
      ->condition('replication_status', [
      Replication::QUEUED,
      Replication::REPLICATING,
    ], 'IN')
      ->execute();
    if (!empty($update_in_queue)) {
      $this
        ->messenger()
        ->addWarning(t('Users are only allowed to create one push and one pull deployment between the same source and target workspace. New deployments are only allowed after the currently queued deployment finish.'));
    }
  }
  if ($update_access && $has_upstream && !$last_replication_failed && empty($update_in_queue)) {
    $items['workspace_update'] = [
      '#type' => 'toolbar_item',
      '#weight' => 124,
      'tab' => [
        '#type' => 'link',
        '#title' => t('Update'),
        '#url' => Url::fromRoute('workspace.update.form'),
        '#attributes' => [
          'title' => t('Update current workspace from upstream'),
          'class' => [
            'toolbar-icon',
            'toolbar-icon-workspace-update',
            'use-ajax',
          ],
          'data-dialog-type' => 'modal',
          'data-dialog-options' => json_encode([
            'width' => '50%',
          ]),
        ],
      ],
      '#wrapper_attributes' => [
        'class' => [
          'workspace-update-toolbar-tab',
        ],
      ],
      '#attached' => [
        'library' => [
          'workspace/drupal.workspace.toolbar',
        ],
      ],
    ];
  }
  return $items;
}