diff --git a/core/core.services.yml b/core/core.services.yml index cf68e70..049aae2 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1203,7 +1203,6 @@ services: theme.negotiator_subscriber: class: Drupal\Core\EventSubscriber\ThemeNegotiatorSubscriber parent: container.trait - arguments: ['@theme.negotiator'] tags: - { name: event_subscriber } authentication: diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterThemeNegotiators.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterThemeNegotiators.php index ff9b828..7cf3b21 100644 --- a/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterThemeNegotiators.php +++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterThemeNegotiators.php @@ -28,6 +28,10 @@ public function process(ContainerBuilder $container) { $container ->getDefinition('theme.negotiator') ->addArgument($service_ids); + + $container + ->getDefinition('theme.negotiator_subscriber') + ->addArgument($service_ids); } } diff --git a/core/lib/Drupal/Core/EventSubscriber/ThemeNegotiatorSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ThemeNegotiatorSubscriber.php index 7eabaac..d2ee005 100644 --- a/core/lib/Drupal/Core/EventSubscriber/ThemeNegotiatorSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/ThemeNegotiatorSubscriber.php @@ -10,7 +10,6 @@ use Drupal\Core\Routing\RouteBuildEvent; use Drupal\Core\Routing\RouteMatch; use Drupal\Core\Routing\RoutingEvents; -use Drupal\Core\Theme\ThemeNegotiator; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -22,34 +21,32 @@ class ThemeNegotiatorSubscriber implements EventSubscriberInterface, ContainerAw use ContainerAwareTrait; - /** - * @var ThemeNegotiator - */ - protected $theme_negotiator; + protected $service_ids; /** * Constructs the ThemeNegotiatorSubscriber object. */ - public function __construct($theme_negotiator) { - $this->theme_negotiator = $theme_negotiator; + public function __construct($service_ids) { + // Sort the negotiators by priority. + arsort($service_ids); + $this->service_ids = array_keys($service_ids); } /** */ public function onRouteAlter(RouteBuildEvent $event) { - $all_negotiators = $this->theme_negotiator->getSortedNegotiators(); - foreach ($event->getRouteCollection() as $route_name => $route) { - $route_negotiators = []; - foreach ($all_negotiators as $service_id) { + $service_ids = []; + foreach ($this->service_ids as $service_id) { /** @var \Drupal\Core\Theme\ThemeNegotiator $theme_negotiator */ + $route_match = new RouteMatch($route_name, $route); $theme_negotiator = $this->container->get($service_id); - if ($theme_negotiator->applies($route_name, $route)) { - $route_negotiators[] = $service_id; + if ($theme_negotiator->applies($route_match, $route)) { + $service_ids[] = $service_id; } } - if ($route_negotiators) { - $route->setOption('_theme_negotiators', array_unique($route_negotiators)); + if ($service_ids) { + $route->setOption('_theme_negotiators', array_unique($service_ids)); } } } diff --git a/core/lib/Drupal/Core/Theme/AjaxBasePageNegotiator.php b/core/lib/Drupal/Core/Theme/AjaxBasePageNegotiator.php index eec59af..7e76cff 100644 --- a/core/lib/Drupal/Core/Theme/AjaxBasePageNegotiator.php +++ b/core/lib/Drupal/Core/Theme/AjaxBasePageNegotiator.php @@ -72,7 +72,7 @@ public function __construct(CsrfTokenGenerator $token_generator, ConfigFactoryIn /** * {@inheritdoc} */ - public function applies($route_name = '', Route $route = NULL) { + public function applies($route_name, Route $route) { // Check whether the route was configured to use the base page theme. return $route && $route->hasOption('_theme') diff --git a/core/lib/Drupal/Core/Theme/DefaultNegotiator.php b/core/lib/Drupal/Core/Theme/DefaultNegotiator.php index 83f9e78..8a57019 100644 --- a/core/lib/Drupal/Core/Theme/DefaultNegotiator.php +++ b/core/lib/Drupal/Core/Theme/DefaultNegotiator.php @@ -36,7 +36,7 @@ public function __construct(ConfigFactoryInterface $config_factory) { /** * {@inheritdoc} */ - public function applies($route_name = '', Route $route = NULL) { + public function applies($route_name, Route $route) { return TRUE; } diff --git a/core/lib/Drupal/Core/Theme/ThemeNegotiator.php b/core/lib/Drupal/Core/Theme/ThemeNegotiator.php index 1008f64..358013d 100644 --- a/core/lib/Drupal/Core/Theme/ThemeNegotiator.php +++ b/core/lib/Drupal/Core/Theme/ThemeNegotiator.php @@ -26,34 +26,42 @@ class ThemeNegotiator implements ThemeNegotiatorInterface, ContainerAwareInterfa use ContainerAwareTrait; /** + * Holds arrays of theme negotiators, keyed by priority. + * + * @var array + */ + protected $negotiators = array(); + + /** + * Holds the array of theme negotiators sorted by priority. + * + * Set to NULL if the array needs to be re-calculated. + * + * @var array|NULL + */ + protected $sortedNegotiators; + + /** * The access checker for themes. * * @var \Drupal\Core\Theme\ThemeAccessCheck */ protected $themeAccess; - protected $negotiator_service_ids; - /** * Constructs a new ThemeNegotiator. * * @param \Drupal\Core\Theme\ThemeAccessCheck $theme_access * The access checker for themes. */ - public function __construct(ThemeAccessCheck $theme_access, $negotiator_service_ids) { + public function __construct(ThemeAccessCheck $theme_access) { $this->themeAccess = $theme_access; - arsort($negotiator_service_ids); - $this->negotiator_service_ids = array_keys($negotiator_service_ids); - } - - public function getSortedNegotiators() { - return $this->negotiator_service_ids; } /** * {@inheritdoc} */ - public function applies($route_name = '', Route $route = NULL) { + public function applies($route_name, Route $route) { return TRUE; } @@ -61,19 +69,12 @@ public function applies($route_name = '', Route $route = NULL) { * {@inheritdoc} */ public function determineActiveTheme(RouteMatchInterface $route_match) { - $route = $route_match->getRouteObject() ?: Null; - - // If no route was found (for example on a 404 page), we still need to - // evaluate all available theme negotiators. - $negotiator_service_ids = !empty($route) ? $route->getOption('_theme_negotiators') : $this->negotiator_service_ids; - - foreach ($negotiator_service_ids as $service_id) { + $route = $route_match->getRouteObject(); + foreach ($route->getOption('_theme_negotiators') as $service_id) { $negotiator = $this->container->get($service_id); - if (!empty($route) || $negotiator->applies($route_match->getRouteName(), $route)) { - $theme = $negotiator->determineActiveTheme($route_match); - if ($theme !== NULL && $this->themeAccess->checkAccess($theme)) { - return $theme; - } + $theme = $negotiator->determineActiveTheme($route_match); + if ($theme !== NULL && $this->themeAccess->checkAccess($theme)) { + return $theme; } } } diff --git a/core/lib/Drupal/Core/Theme/ThemeNegotiatorInterface.php b/core/lib/Drupal/Core/Theme/ThemeNegotiatorInterface.php index a4346b8..d59f8dd 100644 --- a/core/lib/Drupal/Core/Theme/ThemeNegotiatorInterface.php +++ b/core/lib/Drupal/Core/Theme/ThemeNegotiatorInterface.php @@ -40,7 +40,7 @@ * TRUE if this negotiator should be used or FALSE to let other negotiators * decide. */ - public function applies($route_name = '', Route $route = NULL); + public function applies($route_name, Route $route); /** * Determine the active theme for the request. diff --git a/core/modules/block/src/Theme/AdminDemoNegotiator.php b/core/modules/block/src/Theme/AdminDemoNegotiator.php index 8796704..fc0e820 100644 --- a/core/modules/block/src/Theme/AdminDemoNegotiator.php +++ b/core/modules/block/src/Theme/AdminDemoNegotiator.php @@ -19,7 +19,7 @@ class AdminDemoNegotiator implements ThemeNegotiatorInterface { /** * {@inheritdoc} */ - public function applies($route_name = '', Route $route = NULL) { + public function applies($route_name, Route $route) { return $route_name == 'block.admin_demo'; } diff --git a/core/modules/system/src/Theme/BatchNegotiator.php b/core/modules/system/src/Theme/BatchNegotiator.php index 2b5d636..f1156a3 100644 --- a/core/modules/system/src/Theme/BatchNegotiator.php +++ b/core/modules/system/src/Theme/BatchNegotiator.php @@ -48,7 +48,7 @@ public function __construct(BatchStorageInterface $batch_storage, RequestStack $ /** * {@inheritdoc} */ - public function applies($route_name = '', Route $route = NULL) { + public function applies($route_name, Route $route) { return $route_name == 'system.batch_page'; } diff --git a/core/modules/system/src/Theme/DbUpdateNegotiator.php b/core/modules/system/src/Theme/DbUpdateNegotiator.php index cc62c8c..e884e08 100644 --- a/core/modules/system/src/Theme/DbUpdateNegotiator.php +++ b/core/modules/system/src/Theme/DbUpdateNegotiator.php @@ -38,7 +38,7 @@ public function __construct(ConfigFactoryInterface $config_factory) { /** * {@inheritdoc} */ - public function applies($route_name = '', Route $route = NULL) { + public function applies($route_name, Route $route) { return $route_name == 'system.db_update'; } diff --git a/core/modules/system/tests/modules/menu_test/src/Theme/TestThemeNegotiator.php b/core/modules/system/tests/modules/menu_test/src/Theme/TestThemeNegotiator.php index 568fe1e..2d6c2c4 100644 --- a/core/modules/system/tests/modules/menu_test/src/Theme/TestThemeNegotiator.php +++ b/core/modules/system/tests/modules/menu_test/src/Theme/TestThemeNegotiator.php @@ -22,7 +22,7 @@ class TestThemeNegotiator implements ThemeNegotiatorInterface { /** * {@inheritdoc} */ - public function applies($route_name = '', Route $route = NULL) { + public function applies($route_name, Route $route) { return (bool) $route_match->getParameter('inherited'); } diff --git a/core/modules/system/tests/modules/theme_test/src/Theme/CustomThemeNegotiator.php b/core/modules/system/tests/modules/theme_test/src/Theme/CustomThemeNegotiator.php index 33af05a..f1fcdac 100644 --- a/core/modules/system/tests/modules/theme_test/src/Theme/CustomThemeNegotiator.php +++ b/core/modules/system/tests/modules/theme_test/src/Theme/CustomThemeNegotiator.php @@ -19,7 +19,7 @@ class CustomThemeNegotiator implements ThemeNegotiatorInterface { /** * {@inheritdoc} */ - public function applies($route_name = '', Route $route = NULL) { + public function applies($route_name, Route $route) { return ($route && $route->hasOption('_custom_theme')); } diff --git a/core/modules/system/tests/modules/theme_test/src/Theme/HighPriorityThemeNegotiator.php b/core/modules/system/tests/modules/theme_test/src/Theme/HighPriorityThemeNegotiator.php index 1081355..0890492 100644 --- a/core/modules/system/tests/modules/theme_test/src/Theme/HighPriorityThemeNegotiator.php +++ b/core/modules/system/tests/modules/theme_test/src/Theme/HighPriorityThemeNegotiator.php @@ -19,8 +19,8 @@ class HighPriorityThemeNegotiator implements ThemeNegotiatorInterface { /** * {@inheritdoc} */ - public function applies($route_name = '', Route $route = NULL) { - return ($route_name == 'theme_test.priority'); + public function applies($route_name, Route $match) { + return ($route_match->getRouteName() == 'theme_test.priority'); } /** diff --git a/core/modules/user/src/Theme/AdminNegotiator.php b/core/modules/user/src/Theme/AdminNegotiator.php index b7ac061..0db6e9c 100644 --- a/core/modules/user/src/Theme/AdminNegotiator.php +++ b/core/modules/user/src/Theme/AdminNegotiator.php @@ -68,7 +68,7 @@ public function __construct(AccountInterface $user, ConfigFactoryInterface $conf /** * {@inheritdoc} */ - public function applies($route_name = '', Route $route = NULL) { + public function applies($route_name, Route $route) { return ($this->entityManager->hasHandler('user_role', 'storage') && $this->adminContext->isAdminRoute($route)); }