You are here

function workspace_build_renderable_links in Workspace 8.2

Returns an array of workspace activation form links, suitable for rendering.

Return value

array A render array containing links to the workspace activation form.

1 call to workspace_build_renderable_links()
workspace_toolbar in ./workspace.module
Implements hook_toolbar().

File

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

Code

function workspace_build_renderable_links() {
  $entity_type_manager = \Drupal::entityTypeManager();

  /** @var \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository */
  $entity_repository = \Drupal::service('entity.repository');

  /** @var \Drupal\workspace\WorkspaceInterface $active_workspace */
  $active_workspace = \Drupal::service('workspace.manager')
    ->getActiveWorkspace();
  $links = $cache_tags = [];
  foreach ($entity_type_manager
    ->getStorage('workspace')
    ->loadMultiple() as $workspace) {
    $workspace = $entity_repository
      ->getTranslationFromContext($workspace);

    // Add the 'is-active' class for the currently active workspace.
    $options = [];
    if ($workspace
      ->id() === $active_workspace
      ->id()) {
      $options['attributes']['class'][] = 'is-active';
    }

    // Get the URL of the workspace activation form and display it in a modal.
    $url = Url::fromRoute('entity.workspace.activate_form', [
      'workspace' => $workspace
        ->id(),
    ], $options);
    if ($url
      ->access()) {
      $links[$workspace
        ->id()] = [
        'type' => 'link',
        'title' => $workspace
          ->label(),
        'url' => $url,
        'attributes' => [
          'class' => [
            'use-ajax',
          ],
          'data-dialog-type' => 'modal',
          'data-dialog-options' => Json::encode([
            'width' => 500,
          ]),
        ],
      ];
      $cache_tags = Cache::mergeTags($cache_tags, $workspace
        ->getCacheTags());
    }
  }
  if (!empty($links)) {
    $links = [
      '#theme' => 'links__toolbar_workspaces',
      '#links' => $links,
      '#attributes' => [
        'class' => [
          'toolbar-menu',
        ],
      ],
      '#cache' => [
        'tags' => $cache_tags,
      ],
    ];
  }
  return $links;
}