diff --git a/core/modules/system/lib/Drupal/system/Controller/ThemeController.php b/core/modules/system/lib/Drupal/system/Controller/ThemeController.php index 0c163b8..3cde841 100644 --- a/core/modules/system/lib/Drupal/system/Controller/ThemeController.php +++ b/core/modules/system/lib/Drupal/system/Controller/ThemeController.php @@ -9,6 +9,8 @@ use Drupal\Core\Config\Config; use Drupal\Core\Controller\ControllerInterface; +use Drupal\Core\Routing\PathBasedGeneratorInterface; +use Drupal\Core\StringTranslation\TranslationManager; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -27,13 +29,31 @@ class ThemeController implements ControllerInterface { protected $config; /** + * The url generator. + * + * @var \Drupal\Core\Routing\PathBasedGeneratorInterface + */ + protected $urlGenerator; + + /** + * The translation manager service. + * + * @var \Drupal\Core\StringTranslation\TranslationManager + */ + protected $translationManager; + + /** * Constructs a ThemeController object. * * @param \Drupal\Core\Config\Config $config * The config. + * @param \Drupal\Core\Routing\PathBasedGeneratorInterface + * The url generator. */ - public function __construct(Config $config) { + public function __construct(Config $config, PathBasedGeneratorInterface $url_generator, TranslationManager $translation_manager) { $this->config = $config; + $this->urlGenerator = $url_generator; + $this->translationManager = $translation_manager; } /** @@ -41,7 +61,9 @@ public function __construct(Config $config) { */ public static function create(ContainerInterface $container) { return new static( - $container->get('config.factory')->get('system.theme') + $container->get('config.factory')->get('system.theme'), + $container->get('url_generator'), + $container->get('string_translation') ); } @@ -70,18 +92,18 @@ public function disable(Request $request) { if (!empty($themes[$theme])) { // Do not disable the default or admin theme. if ($theme === $this->config->get('default') || $theme === $this->config->get('admin')) { - drupal_set_message(t('%theme is the default theme and cannot be disabled.', array('%theme' => $themes[$theme]->info['name'])), 'error'); + drupal_set_message($this->translationManager->translate('%theme is the default theme and cannot be disabled.', array('%theme' => $themes[$theme]->info['name'])), 'error'); } else { theme_disable(array($theme)); - drupal_set_message(t('The %theme theme has been disabled.', array('%theme' => $themes[$theme]->info['name']))); + drupal_set_message($this->translationManager->translate('The %theme theme has been disabled.', array('%theme' => $themes[$theme]->info['name']))); } } else { - drupal_set_message(t('The %theme theme was not found.', array('%theme' => $theme)), 'error'); + drupal_set_message($this->translationManager->translate('The %theme theme was not found.', array('%theme' => $theme)), 'error'); } - return new RedirectResponse(url('admin/appearance', array('absolute' => TRUE))); + return new RedirectResponse($this->urlGenerator->generateFromPath('admin/appearance', array('absolute' => TRUE))); } throw new AccessDeniedHttpException(); @@ -111,16 +133,79 @@ public function enable(Request $request) { // Check if the specified theme is one recognized by the system. if (!empty($themes[$theme])) { theme_enable(array($theme)); - drupal_set_message(t('The %theme theme has been enabled.', array('%theme' => $themes[$theme]->info['name']))); + drupal_set_message($this->translationManager->translate('The %theme theme has been enabled.', array('%theme' => $themes[$theme]->info['name']))); } else { - drupal_set_message(t('The %theme theme was not found.', array('%theme' => $theme)), 'error'); + drupal_set_message($this->translationManager->translate('The %theme theme was not found.', array('%theme' => $theme)), 'error'); } - return new RedirectResponse(url('admin/appearance', array('absolute' => TRUE))); + return new RedirectResponse($this->urlGenerator->generateFromPath('admin/appearance', array('absolute' => TRUE))); } throw new AccessDeniedHttpException(); } + /** + * Set the default theme. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * A request object containing a theme name and a valid token. + * + * @return \Symfony\Component\HttpFoundation\RedirectResponse + * Redirects back to the appearance admin page. + * + * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException + * Throws access denied when no theme or token is set in the request or when + * the token is invalid. + */ + public function defaultTheme(Request $request) { + // Set the page title. + drupal_set_title($this->translationManager->translate('Set default theme')); + + $theme = $request->get('theme'); + $token = $request->get('token'); + if (isset($theme) && isset($token) && drupal_valid_token($token, 'system-theme-operation-link')) { + // Get current list of themes. + $themes = list_themes(); + + // Check if the specified theme is one recognized by the system. + if (!empty($themes[$theme])) { + // Enable the theme if it is currently disabled. + if (empty($themes[$theme]->status)) { + theme_enable(array($theme)); + } + + // Set the default theme. + $this->config->set('default', $theme) + ->save(); + + // Rebuild the menu. This duplicates the menu_router_rebuild() in + // theme_enable(). However, modules must know the current default theme in + // order to use this information in hook_menu() or hook_menu_alter() + // implementations, and doing the variable_set() before the theme_enable() + // could result in a race condition where the theme is default but not + // enabled. + menu_router_rebuild(); + + // The status message depends on whether an admin theme is currently in use: + // a value of 0 means the admin theme is set to be the default theme. + $admin_theme = $this->config->get('admin'); + if ($admin_theme != 0 && $admin_theme != $theme) { + drupal_set_message($this->translationManager->translate('Please note that the administration theme is still set to the %admin_theme theme; consequently, the theme on this page remains unchanged. All non-administrative sections of the site, however, will show the selected %selected_theme theme by default.', array( + '%admin_theme' => $themes[$admin_theme]->info['name'], + '%selected_theme' => $themes[$theme]->info['name'], + ))); + } + else { + drupal_set_message($this->translationManager->translate('%theme is now the default theme.', array('%theme' => $themes[$theme]->info['name']))); + } + } + else { + drupal_set_message($this->translationManager->translate('The %theme theme was not found.', array('%theme' => $theme)), 'error'); + } + return new RedirectResponse($this->urlGenerator->generateFromPath('admin/appearance', array('absolute' => TRUE))); + } + throw new AccessDeniedHttpException(); + } + } diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 1935f5c..0df0b7f 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -676,13 +676,6 @@ function system_menu() { 'type' => MENU_DEFAULT_LOCAL_TASK, 'file' => 'system.admin.inc', ); - $items['admin/appearance/default'] = array( - 'title' => 'Set default theme', - 'page callback' => 'system_theme_default', - 'access arguments' => array('administer themes'), - 'type' => MENU_CALLBACK, - 'file' => 'system.admin.inc', - ); $items['admin/appearance/settings'] = array( 'title' => 'Settings', 'description' => 'Configure default and theme specific settings.', diff --git a/core/modules/system/system.routing.yml b/core/modules/system/system.routing.yml index 017d486..811fba9 100644 --- a/core/modules/system/system.routing.yml +++ b/core/modules/system/system.routing.yml @@ -207,6 +207,13 @@ system_modules_uninstall_confirm: requirements: _permission: 'administer modules' +system_theme_default: + pattern: '/admin/appearance/default' + defaults: + _controller: 'Drupal\system\Controller\ThemeController::defaultTheme' + requirements: + _permission: 'administer themes' + system_timezone: pattern: '/system/timezone' defaults: