ThemeNegotiator.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Drupal\Core\Theme;
  3. use Drupal\Core\DependencyInjection\ClassResolverInterface;
  4. use Drupal\Core\Routing\RouteMatchInterface;
  5. /**
  6. * Provides a class which determines the active theme of the page.
  7. *
  8. * It therefore uses ThemeNegotiatorInterface objects which are passed in
  9. * using the 'theme_negotiator' tag.
  10. */
  11. class ThemeNegotiator implements ThemeNegotiatorInterface {
  12. /**
  13. * Holds an array of theme negotiator IDs, sorted by priority.
  14. *
  15. * @var string[]
  16. */
  17. protected $negotiators = [];
  18. /**
  19. * The access checker for themes.
  20. *
  21. * @var \Drupal\Core\Theme\ThemeAccessCheck
  22. */
  23. protected $themeAccess;
  24. /**
  25. * The class resolver.
  26. *
  27. * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
  28. */
  29. protected $classResolver;
  30. /**
  31. * Constructs a new ThemeNegotiator.
  32. *
  33. * @param \Drupal\Core\Theme\ThemeAccessCheck $theme_access
  34. * The access checker for themes.
  35. * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
  36. * The class resolver.
  37. * @param string[] $negotiators
  38. * An array of negotiator IDs.
  39. */
  40. public function __construct(ThemeAccessCheck $theme_access, ClassResolverInterface $class_resolver, array $negotiators) {
  41. $this->themeAccess = $theme_access;
  42. $this->negotiators = $negotiators;
  43. $this->classResolver = $class_resolver;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function applies(RouteMatchInterface $route_match) {
  49. return TRUE;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function determineActiveTheme(RouteMatchInterface $route_match) {
  55. foreach ($this->negotiators as $negotiator_id) {
  56. $negotiator = $this->classResolver->getInstanceFromDefinition($negotiator_id);
  57. if ($negotiator->applies($route_match)) {
  58. $theme = $negotiator->determineActiveTheme($route_match);
  59. if ($theme !== NULL && $this->themeAccess->checkAccess($theme)) {
  60. return $theme;
  61. }
  62. }
  63. }
  64. }
  65. }