diff --git a/core/core.services.yml b/core/core.services.yml index 8a24ee7..7e90470 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -152,6 +152,19 @@ services: calls: - [addSubscriber, ['@http_client_simpletest_subscriber']] - [setUserAgent, ['Drupal (+http://drupal.org/)']] + theme.negotiator: + class: Drupal\Core\Theme\ThemeNegotiator + arguments: ['@request'] + theme.negotiator.default: + class: Drupal\Core\Theme\DefaultNegotiator + arguments: ['@config.factory'] + tags: + - { name: theme_negotiator, priority: -100 } + theme.negotiator.ajax_base_page: + class: Drupal\Core\Theme\AjaxBasePageNegotiator + arguments: ['@csrf_token'] + tags: + - { name: theme_negotiator } container.namespaces: class: ArrayObject arguments: [ '%container.namespaces%' ] diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 122cc63..ebe406c 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -93,16 +93,24 @@ function drupal_theme_initialize() { } drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE); - $themes = list_themes(); - // Only select the user selected theme if it is available in the // list of themes that can be accessed. - $theme = !empty($user->theme) && drupal_theme_access($user->theme) ? $user->theme : Drupal::config('system.theme')->get('default'); + $themes = list_themes(); + // @todo Let the theme.negotiator listen to the kernel request event. + $request = Drupal::request(); + Drupal::service('theme.negotiator')->determineActiveTheme($request); + + // @todo Convert to a theme negotiator. + // service. // Allow modules to override the theme. Validation has already been performed // inside menu_get_custom_theme(), so we do not need to check it again here. $custom_theme = menu_get_custom_theme(); - $theme = !empty($custom_theme) ? $custom_theme : $theme; + if ($custom_theme) { + $request->attributes->set('_theme_active', $custom_theme); + } + + $theme = $request->attributes->get('_theme_active'); // Store the identifier for retrieving theme settings with. $theme_key = $theme; @@ -117,7 +125,7 @@ function drupal_theme_initialize() { _drupal_theme_initialize($themes[$theme], array_reverse($base_theme)); // Themes can have alter functions, so reset the drupal_alter() cache. - drupal_static_reset('drupal_alter'); + Drupal::moduleHandler()->resetImplementations(); } /** diff --git a/core/lib/Drupal/Core/CoreServiceProvider.php b/core/lib/Drupal/Core/CoreServiceProvider.php index a6cb957..c3c6df5 100644 --- a/core/lib/Drupal/Core/CoreServiceProvider.php +++ b/core/lib/Drupal/Core/CoreServiceProvider.php @@ -22,6 +22,7 @@ use Drupal\Core\DependencyInjection\Compiler\RegisterBreadcrumbBuilderPass; use Drupal\Core\DependencyInjection\Compiler\RegisterAuthenticationPass; use Drupal\Core\DependencyInjection\Compiler\RegisterTwigExtensionsPass; +use Drupal\Core\Theme\ThemeNegotiatorPass; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Definition; @@ -68,6 +69,9 @@ public function register(ContainerBuilder $container) { // Add the compiler pass that will process the tagged breadcrumb builder // services. $container->addCompilerPass(new RegisterBreadcrumbBuilderPass()); + // Add the compiler pass that will process the tagged theme negotiator + // service. + $container->addCompilerPass(new ThemeNegotiatorPass()); // Add the compiler pass that lets service providers modify existing // service definitions. $container->addCompilerPass(new ModifyServiceDefinitionsPass()); diff --git a/core/lib/Drupal/Core/Theme/AjaxBasePageNegotiator.php b/core/lib/Drupal/Core/Theme/AjaxBasePageNegotiator.php new file mode 100644 index 0000000..2cecad9 --- /dev/null +++ b/core/lib/Drupal/Core/Theme/AjaxBasePageNegotiator.php @@ -0,0 +1,69 @@ +csrfGenerator = $token_generator; + } + + /** + * {@inheritdoc} + */ + public function determineActiveTheme(Request $request) { + // Check whether the route was configured to use the base page theme. + if (!(($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) && $route->hasOption('_ajax_base_page_theme'))) { + return NULL; + } + if (($ajax_page_state = $request->request->get('ajax_page_state')) && !empty($ajax_page_state['theme']) && !empty($ajax_page_state['theme_token'])) { + $theme = $ajax_page_state['theme']; + $token = $ajax_page_state['theme_token']; + + // Ensure that the user only access a theme they are allowed to see. + if ($this->csrfGenerator->validate($token, $theme)) { + return $theme; + } + } + } + +} diff --git a/core/lib/Drupal/Core/Theme/DefaultNegotiator.php b/core/lib/Drupal/Core/Theme/DefaultNegotiator.php new file mode 100644 index 0000000..f155b0d --- /dev/null +++ b/core/lib/Drupal/Core/Theme/DefaultNegotiator.php @@ -0,0 +1,35 @@ +config = $config_factory->get('system.theme'); + } + + /** + * {@inheritdoc} + */ + public function determineActiveTheme(Request $request) { + return $this->config->get('default'); + } + +} diff --git a/core/lib/Drupal/Core/Theme/ThemeNegotiator.php b/core/lib/Drupal/Core/Theme/ThemeNegotiator.php new file mode 100644 index 0000000..5db1885 --- /dev/null +++ b/core/lib/Drupal/Core/Theme/ThemeNegotiator.php @@ -0,0 +1,115 @@ +request = $request; + } + + /** + * Adds a active theme negotiation service. + * + * @param \Drupal\Core\Theme\ThemeNegotiatorInterface $negotiator + * The theme negotiator to add. + * @param int $priority + * Priority of the breadcrumb builder. + */ + public function addNegotiator(ThemeNegotiatorInterface $negotiator, $priority) { + $this->negotiators[$priority][] = $negotiator; + // Force the negotiators to be re-sorted. + $this->sortedNegotiators = NULL; + } + + /** + * Returns the sorted array of theme negotiators. + * + * @return array|\Drupal\Core\Theme\ThemeNegotiatorInterface[] + * An array of breadcrumb builder objects. + */ + protected function getSortedNegotiators() { + if (!isset($this->sortedNegotiators)) { + // Sort the negotiators according to priority. + krsort($this->negotiators); + // Merge nested negotiators from $this->negotiators into + // $this->sortedNegotiators. + $this->sortedNegotiators = array(); + foreach ($this->negotiators as $builders) { + $this->sortedNegotiators = array_merge($this->sortedNegotiators, $builders); + } + } + return $this->sortedNegotiators; + } + + /** + * Get the current active theme. + * + * @return string + * The current active string. + */ + public function getActiveTheme() { + if (!$this->request->attributes->has('_theme_active')) { + $this->determineActiveTheme($this->request); + } + return $this->request->attributes->get('_theme_active'); + } + + /** + * {@inheritdoc} + */ + public function determineActiveTheme(Request $request) { + foreach ($this->getSortedNegotiators() as $negotiator) { + if (($active_theme = $negotiator->determineActiveTheme($request))) { + $request->attributes->set('_theme_active', $active_theme); + } + } + return $request->attributes->get('_theme_active'); + } + +} diff --git a/core/lib/Drupal/Core/Theme/ThemeNegotiatorInterface.php b/core/lib/Drupal/Core/Theme/ThemeNegotiatorInterface.php new file mode 100644 index 0000000..9bb80b1 --- /dev/null +++ b/core/lib/Drupal/Core/Theme/ThemeNegotiatorInterface.php @@ -0,0 +1,28 @@ +hasDefinition('theme.negotiator')) { + return; + } + $manager = $container->getDefinition('theme.negotiator'); + foreach ($container->findTaggedServiceIds('theme_negotiator') as $id => $attributes) { + $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; + $manager->addMethodCall('addNegotiator', array(new Reference($id), $priority)); + } + } + +} diff --git a/core/misc/ajax.js b/core/misc/ajax.js index bdff14b..67a8a4e 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -382,7 +382,7 @@ Drupal.ajax.prototype.beforeSerialize = function (element, options) { // Allow Drupal to return new JavaScript and CSS files to load without // returning the ones already loaded. - // @see ajax_base_page_theme() + // @see \Drupal\Core\Theme\AjaxBasePageNegotiator // @see drupal_get_css() // @see drupal_get_js() var pageState = Drupal.settings.ajaxPageState; diff --git a/core/modules/contextual/contextual.module b/core/modules/contextual/contextual.module index 10d57b1..a8bee3a 100644 --- a/core/modules/contextual/contextual.module +++ b/core/modules/contextual/contextual.module @@ -6,20 +6,6 @@ */ /** - * Implements hook_menu(). - */ -function contextual_menu() { - // @todo Remove this menu item in http://drupal.org/node/1954892 when theme - // callbacks are replaced with something else. - $items['contextual/render'] = array( - 'route_name' => 'contextual_render', - 'theme callback' => 'ajax_base_page_theme', - ); - - return $items; -} - -/** * Implements hook_toolbar(). */ function contextual_toolbar() { diff --git a/core/modules/contextual/contextual.routing.yml b/core/modules/contextual/contextual.routing.yml index 5dd4457..0286cad 100644 --- a/core/modules/contextual/contextual.routing.yml +++ b/core/modules/contextual/contextual.routing.yml @@ -1,5 +1,7 @@ contextual_render: pattern: '/contextual/render' + options: + _ajax_base_page_theme: 'TRUE' defaults: _controller: '\Drupal\contextual\ContextualController::render' requirements: diff --git a/core/modules/edit/edit.module b/core/modules/edit/edit.module index a09889c..f0802eb 100644 --- a/core/modules/edit/edit.module +++ b/core/modules/edit/edit.module @@ -18,24 +18,6 @@ use Drupal\user\TempStoreFactory; /** - * Implements hook_menu(). - */ -function edit_menu() { - // @todo Remove these menu items in http://drupal.org/node/1954892 when theme - // callbacks are replaced with something else. - $items['edit/metadata'] = array( - 'route_name' => 'edit_metadata', - 'theme callback' => 'ajax_base_page_theme', - ); - $items['edit/form/%/%/%/%/%'] = array( - 'route_name' => 'edit_field_form', - 'theme callback' => 'ajax_base_page_theme', - ); - - return $items; -} - -/** * Implements hook_permission(). */ function edit_permission() { diff --git a/core/modules/edit/edit.routing.yml b/core/modules/edit/edit.routing.yml index b709d0a..d310d45 100644 --- a/core/modules/edit/edit.routing.yml +++ b/core/modules/edit/edit.routing.yml @@ -1,5 +1,7 @@ edit_metadata: pattern: '/edit/metadata' + options: + _ajax_base_page_theme: 'TRUE' defaults: _controller: '\Drupal\edit\EditController::metadata' requirements: @@ -14,6 +16,8 @@ edit_attachments: edit_field_form: pattern: '/edit/form/{entity_type}/{entity}/{field_name}/{langcode}/{view_mode_id}' + options: + _ajax_base_page_theme: 'TRUE' defaults: _controller: '\Drupal\edit\EditController::fieldForm' requirements: diff --git a/core/modules/editor/editor.module b/core/modules/editor/editor.module index e9a44f0..44526ea 100644 --- a/core/modules/editor/editor.module +++ b/core/modules/editor/editor.module @@ -137,20 +137,6 @@ function editor_library_info() { } /** - * Implements hook_menu(). - */ -function editor_menu() { - // @todo Remove this menu item in http://drupal.org/node/1954892 when theme - // callbacks are replaced with something else. - $items['editor/%/%/%/%/%'] = array( - 'route_name' => 'editor_field_untransformed_text', - 'theme callback' => 'ajax_base_page_theme', - ); - - return $items; -} - -/** * Implements hook_form_FORM_ID_alter(). */ function editor_form_filter_admin_overview_alter(&$form, $form_state) { diff --git a/core/modules/editor/editor.routing.yml b/core/modules/editor/editor.routing.yml index f905634..783ff30 100644 --- a/core/modules/editor/editor.routing.yml +++ b/core/modules/editor/editor.routing.yml @@ -4,6 +4,7 @@ editor_field_untransformed_text: _controller: '\Drupal\editor\EditorController::getUntransformedText' options: _access_mode: 'ALL' + _ajax_base_page_theme: 'TRUE' requirements: _permission: 'access in-place editing' _access_edit_entity_field: 'TRUE' diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 3bf9f7b..99cff1a 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -39,26 +39,6 @@ function file_help($path, $arg) { } /** - * Implements hook_menu(). - */ -function file_menu() { - $items = array(); - - $items['file/ajax'] = array( - 'route_name' => 'file.ajax.upload', - 'theme callback' => 'ajax_base_page_theme', - 'type' => MENU_CALLBACK, - ); - $items['file/progress'] = array( - 'route name' => 'file.ajax.progress', - 'theme callback' => 'ajax_base_page_theme', - 'type' => MENU_CALLBACK, - ); - - return $items; -} - -/** * Implements hook_element_info(). * * The managed file element may be used anywhere in Drupal. diff --git a/core/modules/file/file.routing.yml b/core/modules/file/file.routing.yml index c2a9efd..bd566fb 100644 --- a/core/modules/file/file.routing.yml +++ b/core/modules/file/file.routing.yml @@ -1,11 +1,15 @@ file.ajax.upload: pattern: '/file/ajax' + options: + _ajax_base_page_theme: 'TRUE' defaults: _controller: '\Drupal\file\Controller\FileWidgetAjaxController::upload' requirements: _permission: 'access content' file.ajax.progress: pattern: '/file/progress' + options: + _ajax_base_page_theme: 'TRUE' defaults: _controller: '\Drupal\file\Controller\FileWidgetAjaxController::progress' requirements: diff --git a/core/modules/system/system.module b/core/modules/system/system.module index bd58f93..791f22d 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -613,12 +613,6 @@ function system_menu() { 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); - $items['system/ajax'] = array( - 'title' => 'AHAH callback', - 'route_name' => 'system.ajax', - 'theme callback' => 'ajax_base_page_theme', - 'type' => MENU_CALLBACK, - ); $items['admin'] = array( 'title' => 'Administration', 'access arguments' => array('access administration pages'), diff --git a/core/modules/system/system.routing.yml b/core/modules/system/system.routing.yml index f12ba8a..b9edac9 100644 --- a/core/modules/system/system.routing.yml +++ b/core/modules/system/system.routing.yml @@ -1,5 +1,7 @@ system.ajax: pattern: '/system/ajax' + options: + _ajax_base_page_theme: 'TRUE' defaults: _controller: '\Drupal\system\Controller\FormAjaxController::content' requirements: diff --git a/core/modules/user/lib/Drupal/user/Theme/UserNegotiator.php b/core/modules/user/lib/Drupal/user/Theme/UserNegotiator.php new file mode 100644 index 0000000..17c1e33 --- /dev/null +++ b/core/modules/user/lib/Drupal/user/Theme/UserNegotiator.php @@ -0,0 +1,61 @@ +userStorageController = $entity_manager->getStorageController('user'); + $this->currentUser = $current_user; + } + + /** + * {@inheritdoc} + */ + public function determineActiveTheme(Request $request) { + if ($user = $this->userStorageController->load($this->currentUser->id())) {; + // Only select the user selected theme if it is available in the + // list of themes that can be accessed. + if (!empty($user->theme) && drupal_theme_access($user->theme)) { + return $user->theme; + } + } + } + +} diff --git a/core/modules/user/user.services.yml b/core/modules/user/user.services.yml index 6fb7d47..c70cad1 100644 --- a/core/modules/user/user.services.yml +++ b/core/modules/user/user.services.yml @@ -25,3 +25,9 @@ services: class: Drupal\user\EventSubscriber\MaintenanceModeSubscriber tags: - { name: event_subscriber } + theme.negotiator.user: + class: Drupal\user\Theme\UserNegotiator + arguments: ['@plugin.manager.entity', '@current_user'] + tags: + - { name: theme_negotiator, priority: 50 } + diff --git a/core/modules/views/lib/Drupal/views/ViewExecutable.php b/core/modules/views/lib/Drupal/views/ViewExecutable.php index 887c600..48ba1e1 100644 --- a/core/modules/views/lib/Drupal/views/ViewExecutable.php +++ b/core/modules/views/lib/Drupal/views/ViewExecutable.php @@ -1277,7 +1277,6 @@ public function render($display_id = NULL) { return; } - drupal_theme_initialize(); $config = \Drupal::config('views.settings'); $exposed_form = $this->display_handler->getPlugin('exposed_form'); @@ -1341,11 +1340,13 @@ public function render($display_id = NULL) { $module_handler->invokeAll('views_pre_render', array($this)); // Let the themes play too, because pre render is a very themey thing. - foreach ($GLOBALS['base_theme_info'] as $base) { - $module_handler->invoke($base, 'views_pre_render', array($this)); - } + if (isset($GLOBALS['base_theme_info']) && isset($GLOBALS['theme'])) { + foreach ($GLOBALS['base_theme_info'] as $base) { + $module_handler->invoke($base, 'views_pre_render', array($this)); + } - $module_handler->invoke($GLOBALS['theme'], 'views_pre_render', array($this)); + $module_handler->invoke($GLOBALS['theme'], 'views_pre_render', array($this)); + } $this->display_handler->output = $this->display_handler->render(); if ($cache) { @@ -1363,11 +1364,13 @@ public function render($display_id = NULL) { $module_handler->invokeAll('views_post_render', array($this, $this->display_handler->output, $cache)); // Let the themes play too, because post render is a very themey thing. - foreach ($GLOBALS['base_theme_info'] as $base) { - $module_handler->invoke($base, 'views_post_render', array($this)); - } + if (isset($GLOBALS['base_theme_info']) && isset($GLOBALS['theme'])) { + foreach ($GLOBALS['base_theme_info'] as $base) { + $module_handler->invoke($base, 'views_post_render', array($this)); + } - $module_handler->invoke($GLOBALS['theme'], 'views_post_render', array($this)); + $module_handler->invoke($GLOBALS['theme'], 'views_post_render', array($this)); + } return $this->display_handler->output; } diff --git a/core/modules/views/views.module b/core/modules/views/views.module index 26a9dbb..6219a7e 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -316,12 +316,6 @@ function views_menu() { // Views data is updated -- module changes, profile changes, etc. views_invalidate_cache(); $items = array(); - $items['views/ajax'] = array( - 'title' => 'Views', - 'theme callback' => 'ajax_base_page_theme', - 'route_name' => 'views_ajax', - 'type' => MENU_CALLBACK, - ); // Define another taxonomy autocomplete because the default one of drupal // does not support a vid a argument anymore $items['admin/views/ajax/autocomplete/taxonomy/%'] = array( diff --git a/core/modules/views/views.routing.yml b/core/modules/views/views.routing.yml index 016b2a5..0e331ca 100644 --- a/core/modules/views/views.routing.yml +++ b/core/modules/views/views.routing.yml @@ -1,5 +1,7 @@ views_ajax: pattern: '/views/ajax' + options: + _ajax_base_page_theme: 'TRUE' defaults: _controller: '\Drupal\views\Controller\ViewAjaxController::ajaxView' requirements: