ThemeAccessCheck.php 1.46 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\Theme;

5
use Drupal\Core\Access\AccessResult;
6
use Drupal\Core\Extension\ThemeHandlerInterface;
7
use Drupal\Core\Routing\Access\AccessInterface;
8 9

/**
10
 * Provides access checking for themes for routing and theme negotiation.
11
 */
12
class ThemeAccessCheck implements AccessInterface {
13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
  /**
   * The theme handler.
   *
   * @var \Drupal\Core\Extension\ThemeHandlerInterface
   */
  protected $themeHandler;

  /**
   * Constructs a \Drupal\Core\Theme\Registry object.
   *
   * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
   *   The theme handler.
   */
  public function __construct(ThemeHandlerInterface $theme_handler) {
    $this->themeHandler = $theme_handler;
  }
30

31
  /**
32 33 34 35 36
   * Checks access to the theme for routing.
   *
   * @param string $theme
   *   The name of a theme.
   *
37 38
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
39
   */
40
  public function access($theme) {
41 42
    // Cacheable until the theme settings are modified.
    return AccessResult::allowedIf($this->checkAccess($theme))->addCacheTags(['config:' . $theme . '.settings']);
43 44 45
  }

  /**
46
   * Indicates whether the theme is accessible based on whether it is installed.
47 48 49 50 51
   *
   * @param string $theme
   *   The name of a theme.
   *
   * @return bool
52
   *   TRUE if the theme is installed, FALSE otherwise.
53 54
   */
  public function checkAccess($theme) {
55
    $themes = $this->themeHandler->listInfo();
56 57 58 59
    return !empty($themes[$theme]->status);
  }

}