ActiveWorkspaceCheck.php

Same filename in other branches
  1. 8.9.x core/modules/workspaces/src/Access/ActiveWorkspaceCheck.php
  2. 10 core/modules/workspaces/src/Access/ActiveWorkspaceCheck.php
  3. 11.x core/modules/workspaces/src/Access/ActiveWorkspaceCheck.php

Namespace

Drupal\workspaces\Access

File

core/modules/workspaces/src/Access/ActiveWorkspaceCheck.php

View source
<?php

namespace Drupal\workspaces\Access;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\workspaces\WorkspaceManagerInterface;
use Symfony\Component\Routing\Route;

/**
 * Determines access to routes based on the presence of an active workspace.
 */
class ActiveWorkspaceCheck implements AccessInterface {
    
    /**
     * The workspace manager.
     *
     * @var \Drupal\workspaces\WorkspaceManagerInterface
     */
    protected $workspaceManager;
    
    /**
     * Constructs a new ActiveWorkspaceCheck.
     *
     * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
     *   The workspace manager.
     */
    public function __construct(WorkspaceManagerInterface $workspace_manager) {
        $this->workspaceManager = $workspace_manager;
    }
    
    /**
     * Checks access.
     *
     * @param \Symfony\Component\Routing\Route $route
     *   The route to check against.
     *
     * @return \Drupal\Core\Access\AccessResultInterface
     *   The access result.
     */
    public function access(Route $route) {
        if (!$route->hasRequirement('_has_active_workspace')) {
            return AccessResult::neutral();
        }
        $required_value = filter_var($route->getRequirement('_has_active_workspace'), FILTER_VALIDATE_BOOLEAN);
        return AccessResult::allowedIf($required_value === $this->workspaceManager
            ->hasActiveWorkspace())
            ->addCacheContexts([
            'workspace',
        ]);
    }

}

Classes

Title Deprecated Summary
ActiveWorkspaceCheck Determines access to routes based on the presence of an active workspace.

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