ThemeAccessCheck.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Drupal\Core\Theme;
  3. use Drupal\Core\Access\AccessResult;
  4. use Drupal\Core\Extension\ThemeHandlerInterface;
  5. use Drupal\Core\Routing\Access\AccessInterface;
  6. /**
  7. * Provides access checking for themes for routing and theme negotiation.
  8. */
  9. class ThemeAccessCheck implements AccessInterface {
  10. /**
  11. * The theme handler.
  12. *
  13. * @var \Drupal\Core\Extension\ThemeHandlerInterface
  14. */
  15. protected $themeHandler;
  16. /**
  17. * Constructs a \Drupal\Core\Theme\Registry object.
  18. *
  19. * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
  20. * The theme handler.
  21. */
  22. public function __construct(ThemeHandlerInterface $theme_handler) {
  23. $this->themeHandler = $theme_handler;
  24. }
  25. /**
  26. * Checks access to the theme for routing.
  27. *
  28. * @param string $theme
  29. * The name of a theme.
  30. *
  31. * @return \Drupal\Core\Access\AccessResultInterface
  32. * The access result.
  33. */
  34. public function access($theme) {
  35. // Cacheable until the theme settings are modified.
  36. return AccessResult::allowedIf($this->checkAccess($theme))->addCacheTags(['config:' . $theme . '.settings']);
  37. }
  38. /**
  39. * Indicates whether the theme is accessible based on whether it is installed.
  40. *
  41. * @param string $theme
  42. * The name of a theme.
  43. *
  44. * @return bool
  45. * TRUE if the theme is installed, FALSE otherwise.
  46. */
  47. public function checkAccess($theme) {
  48. $themes = $this->themeHandler->listInfo();
  49. return !empty($themes[$theme]->status);
  50. }
  51. }