3 namespace Drupal\Core\Theme;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Extension\ThemeHandlerInterface;
7 use Drupal\Core\Routing\Access\AccessInterface;
10 * Provides access checking for themes for routing and theme negotiation.
12 class ThemeAccessCheck implements AccessInterface {
17 * @var \Drupal\Core\Extension\ThemeHandlerInterface
19 protected $themeHandler;
22 * Constructs a \Drupal\Core\Theme\Registry object.
24 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
27 public function __construct(ThemeHandlerInterface $theme_handler) {
28 $this->themeHandler = $theme_handler;
32 * Checks access to the theme for routing.
34 * @param string $theme
35 * The name of a theme.
37 * @return \Drupal\Core\Access\AccessResultInterface
40 public function access($theme) {
41 // Cacheable until the theme settings are modified.
42 return AccessResult::allowedIf($this->checkAccess($theme))->addCacheTags(['config:' . $theme . '.settings']);
46 * Indicates whether the theme is accessible based on whether it is installed.
48 * @param string $theme
49 * The name of a theme.
52 * TRUE if the theme is installed, FALSE otherwise.
54 public function checkAccess($theme) {
55 $themes = $this->themeHandler->listInfo();
56 return !empty($themes[$theme]->status);