Chris@0: themeHandler = $theme_handler; Chris@0: $this->configFactory = $config_factory; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@0: $container->get('theme_handler'), Chris@0: $container->get('config.factory') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Uninstalls a theme. Chris@0: * Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request Chris@0: * A request object containing a theme name and a valid token. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\RedirectResponse Chris@0: * Redirects back to the appearance admin page. Chris@0: * Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException Chris@0: * Throws access denied when no theme or token is set in the request or when Chris@0: * the token is invalid. Chris@0: */ Chris@0: public function uninstall(Request $request) { Chris@0: $theme = $request->query->get('theme'); Chris@0: $config = $this->config('system.theme'); Chris@0: Chris@0: if (isset($theme)) { Chris@0: // Get current list of themes. Chris@0: $themes = $this->themeHandler->listInfo(); Chris@0: Chris@0: // Check if the specified theme is one recognized by the system. Chris@0: if (!empty($themes[$theme])) { Chris@0: // Do not uninstall the default or admin theme. Chris@0: if ($theme === $config->get('default') || $theme === $config->get('admin')) { Chris@17: $this->messenger()->addError($this->t('%theme is the default theme and cannot be uninstalled.', ['%theme' => $themes[$theme]->info['name']])); Chris@0: } Chris@0: else { Chris@0: $this->themeHandler->uninstall([$theme]); Chris@17: $this->messenger()->addStatus($this->t('The %theme theme has been uninstalled.', ['%theme' => $themes[$theme]->info['name']])); Chris@0: } Chris@0: } Chris@0: else { Chris@17: $this->messenger()->addError($this->t('The %theme theme was not found.', ['%theme' => $theme])); Chris@0: } Chris@0: Chris@0: return $this->redirect('system.themes_page'); Chris@0: } Chris@0: Chris@0: throw new AccessDeniedHttpException(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Installs a theme. Chris@0: * Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request Chris@0: * A request object containing a theme name and a valid token. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\RedirectResponse Chris@0: * Redirects back to the appearance admin page. Chris@0: * Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException Chris@0: * Throws access denied when no theme or token is set in the request or when Chris@0: * the token is invalid. Chris@0: */ Chris@0: public function install(Request $request) { Chris@0: $theme = $request->query->get('theme'); Chris@0: Chris@0: if (isset($theme)) { Chris@0: try { Chris@0: if ($this->themeHandler->install([$theme])) { Chris@0: $themes = $this->themeHandler->listInfo(); Chris@17: $this->messenger()->addStatus($this->t('The %theme theme has been installed.', ['%theme' => $themes[$theme]->info['name']])); Chris@0: } Chris@0: else { Chris@17: $this->messenger()->addError($this->t('The %theme theme was not found.', ['%theme' => $theme])); Chris@0: } Chris@0: } Chris@0: catch (PreExistingConfigException $e) { Chris@0: $config_objects = $e->flattenConfigObjects($e->getConfigObjects()); Chris@17: $this->messenger()->addError( Chris@0: $this->formatPlural( Chris@0: count($config_objects), Chris@0: 'Unable to install @extension, %config_names already exists in active configuration.', Chris@0: 'Unable to install @extension, %config_names already exist in active configuration.', Chris@0: [ Chris@0: '%config_names' => implode(', ', $config_objects), Chris@0: '@extension' => $theme, Chris@17: ]) Chris@0: ); Chris@0: } Chris@0: catch (UnmetDependenciesException $e) { Chris@17: $this->messenger()->addError($e->getTranslatedMessage($this->getStringTranslation(), $theme)); Chris@0: } Chris@0: Chris@0: return $this->redirect('system.themes_page'); Chris@0: } Chris@0: Chris@0: throw new AccessDeniedHttpException(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the default theme. Chris@0: * Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request Chris@0: * A request object containing a theme name. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\RedirectResponse Chris@0: * Redirects back to the appearance admin page. Chris@0: * Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException Chris@0: * Throws access denied when no theme is set in the request. Chris@0: */ Chris@0: public function setDefaultTheme(Request $request) { Chris@0: $config = $this->configFactory->getEditable('system.theme'); Chris@0: $theme = $request->query->get('theme'); Chris@0: Chris@0: if (isset($theme)) { Chris@0: // Get current list of themes. Chris@0: $themes = $this->themeHandler->listInfo(); Chris@0: Chris@0: // Check if the specified theme is one recognized by the system. Chris@0: // Or try to install the theme. Chris@0: if (isset($themes[$theme]) || $this->themeHandler->install([$theme])) { Chris@0: $themes = $this->themeHandler->listInfo(); Chris@0: Chris@0: // Set the default theme. Chris@0: $config->set('default', $theme)->save(); Chris@0: Chris@0: // The status message depends on whether an admin theme is currently in Chris@0: // use: a value of 0 means the admin theme is set to be the default Chris@0: // theme. Chris@0: $admin_theme = $config->get('admin'); Chris@0: if ($admin_theme != 0 && $admin_theme != $theme) { Chris@17: $this->messenger() Chris@17: ->addStatus($this->t('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.', [ Chris@17: '%admin_theme' => $themes[$admin_theme]->info['name'], Chris@17: '%selected_theme' => $themes[$theme]->info['name'], Chris@17: ])); Chris@0: } Chris@0: else { Chris@17: $this->messenger()->addStatus($this->t('%theme is now the default theme.', ['%theme' => $themes[$theme]->info['name']])); Chris@0: } Chris@0: } Chris@0: else { Chris@17: $this->messenger()->addError($this->t('The %theme theme was not found.', ['%theme' => $theme])); Chris@0: } Chris@0: Chris@0: return $this->redirect('system.themes_page'); Chris@0: Chris@0: } Chris@0: throw new AccessDeniedHttpException(); Chris@0: } Chris@0: Chris@0: }