Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\system\Controller;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Config\ConfigFactoryInterface;
|
Chris@0
|
6 use Drupal\Core\Config\PreExistingConfigException;
|
Chris@0
|
7 use Drupal\Core\Config\UnmetDependenciesException;
|
Chris@0
|
8 use Drupal\Core\Controller\ControllerBase;
|
Chris@0
|
9 use Drupal\Core\Extension\ThemeHandlerInterface;
|
Chris@0
|
10 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
11 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
12 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Controller for theme handling.
|
Chris@0
|
16 */
|
Chris@0
|
17 class ThemeController extends ControllerBase {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * The theme handler service.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var \Drupal\Core\Extension\ThemeHandlerInterface
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $themeHandler;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * Constructs a new ThemeController.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
|
Chris@0
|
30 * The theme handler.
|
Chris@0
|
31 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
Chris@0
|
32 * The config factory.
|
Chris@0
|
33 */
|
Chris@0
|
34 public function __construct(ThemeHandlerInterface $theme_handler, ConfigFactoryInterface $config_factory) {
|
Chris@0
|
35 $this->themeHandler = $theme_handler;
|
Chris@0
|
36 $this->configFactory = $config_factory;
|
Chris@0
|
37 }
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * {@inheritdoc}
|
Chris@0
|
41 */
|
Chris@0
|
42 public static function create(ContainerInterface $container) {
|
Chris@0
|
43 return new static(
|
Chris@0
|
44 $container->get('theme_handler'),
|
Chris@0
|
45 $container->get('config.factory')
|
Chris@0
|
46 );
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * Uninstalls a theme.
|
Chris@0
|
51 *
|
Chris@0
|
52 * @param \Symfony\Component\HttpFoundation\Request $request
|
Chris@0
|
53 * A request object containing a theme name and a valid token.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @return \Symfony\Component\HttpFoundation\RedirectResponse
|
Chris@0
|
56 * Redirects back to the appearance admin page.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
|
Chris@0
|
59 * Throws access denied when no theme or token is set in the request or when
|
Chris@0
|
60 * the token is invalid.
|
Chris@0
|
61 */
|
Chris@0
|
62 public function uninstall(Request $request) {
|
Chris@0
|
63 $theme = $request->query->get('theme');
|
Chris@0
|
64 $config = $this->config('system.theme');
|
Chris@0
|
65
|
Chris@0
|
66 if (isset($theme)) {
|
Chris@0
|
67 // Get current list of themes.
|
Chris@0
|
68 $themes = $this->themeHandler->listInfo();
|
Chris@0
|
69
|
Chris@0
|
70 // Check if the specified theme is one recognized by the system.
|
Chris@0
|
71 if (!empty($themes[$theme])) {
|
Chris@0
|
72 // Do not uninstall the default or admin theme.
|
Chris@0
|
73 if ($theme === $config->get('default') || $theme === $config->get('admin')) {
|
Chris@17
|
74 $this->messenger()->addError($this->t('%theme is the default theme and cannot be uninstalled.', ['%theme' => $themes[$theme]->info['name']]));
|
Chris@0
|
75 }
|
Chris@0
|
76 else {
|
Chris@0
|
77 $this->themeHandler->uninstall([$theme]);
|
Chris@17
|
78 $this->messenger()->addStatus($this->t('The %theme theme has been uninstalled.', ['%theme' => $themes[$theme]->info['name']]));
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|
Chris@0
|
81 else {
|
Chris@17
|
82 $this->messenger()->addError($this->t('The %theme theme was not found.', ['%theme' => $theme]));
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 return $this->redirect('system.themes_page');
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 throw new AccessDeniedHttpException();
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Installs a theme.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @param \Symfony\Component\HttpFoundation\Request $request
|
Chris@0
|
95 * A request object containing a theme name and a valid token.
|
Chris@0
|
96 *
|
Chris@0
|
97 * @return \Symfony\Component\HttpFoundation\RedirectResponse
|
Chris@0
|
98 * Redirects back to the appearance admin page.
|
Chris@0
|
99 *
|
Chris@0
|
100 * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
|
Chris@0
|
101 * Throws access denied when no theme or token is set in the request or when
|
Chris@0
|
102 * the token is invalid.
|
Chris@0
|
103 */
|
Chris@0
|
104 public function install(Request $request) {
|
Chris@0
|
105 $theme = $request->query->get('theme');
|
Chris@0
|
106
|
Chris@0
|
107 if (isset($theme)) {
|
Chris@0
|
108 try {
|
Chris@0
|
109 if ($this->themeHandler->install([$theme])) {
|
Chris@0
|
110 $themes = $this->themeHandler->listInfo();
|
Chris@17
|
111 $this->messenger()->addStatus($this->t('The %theme theme has been installed.', ['%theme' => $themes[$theme]->info['name']]));
|
Chris@0
|
112 }
|
Chris@0
|
113 else {
|
Chris@17
|
114 $this->messenger()->addError($this->t('The %theme theme was not found.', ['%theme' => $theme]));
|
Chris@0
|
115 }
|
Chris@0
|
116 }
|
Chris@0
|
117 catch (PreExistingConfigException $e) {
|
Chris@0
|
118 $config_objects = $e->flattenConfigObjects($e->getConfigObjects());
|
Chris@17
|
119 $this->messenger()->addError(
|
Chris@0
|
120 $this->formatPlural(
|
Chris@0
|
121 count($config_objects),
|
Chris@0
|
122 'Unable to install @extension, %config_names already exists in active configuration.',
|
Chris@0
|
123 'Unable to install @extension, %config_names already exist in active configuration.',
|
Chris@0
|
124 [
|
Chris@0
|
125 '%config_names' => implode(', ', $config_objects),
|
Chris@0
|
126 '@extension' => $theme,
|
Chris@17
|
127 ])
|
Chris@0
|
128 );
|
Chris@0
|
129 }
|
Chris@0
|
130 catch (UnmetDependenciesException $e) {
|
Chris@17
|
131 $this->messenger()->addError($e->getTranslatedMessage($this->getStringTranslation(), $theme));
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 return $this->redirect('system.themes_page');
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 throw new AccessDeniedHttpException();
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 /**
|
Chris@0
|
141 * Set the default theme.
|
Chris@0
|
142 *
|
Chris@0
|
143 * @param \Symfony\Component\HttpFoundation\Request $request
|
Chris@0
|
144 * A request object containing a theme name.
|
Chris@0
|
145 *
|
Chris@0
|
146 * @return \Symfony\Component\HttpFoundation\RedirectResponse
|
Chris@0
|
147 * Redirects back to the appearance admin page.
|
Chris@0
|
148 *
|
Chris@0
|
149 * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
|
Chris@0
|
150 * Throws access denied when no theme is set in the request.
|
Chris@0
|
151 */
|
Chris@0
|
152 public function setDefaultTheme(Request $request) {
|
Chris@0
|
153 $config = $this->configFactory->getEditable('system.theme');
|
Chris@0
|
154 $theme = $request->query->get('theme');
|
Chris@0
|
155
|
Chris@0
|
156 if (isset($theme)) {
|
Chris@0
|
157 // Get current list of themes.
|
Chris@0
|
158 $themes = $this->themeHandler->listInfo();
|
Chris@0
|
159
|
Chris@0
|
160 // Check if the specified theme is one recognized by the system.
|
Chris@0
|
161 // Or try to install the theme.
|
Chris@0
|
162 if (isset($themes[$theme]) || $this->themeHandler->install([$theme])) {
|
Chris@0
|
163 $themes = $this->themeHandler->listInfo();
|
Chris@0
|
164
|
Chris@0
|
165 // Set the default theme.
|
Chris@0
|
166 $config->set('default', $theme)->save();
|
Chris@0
|
167
|
Chris@0
|
168 // The status message depends on whether an admin theme is currently in
|
Chris@0
|
169 // use: a value of 0 means the admin theme is set to be the default
|
Chris@0
|
170 // theme.
|
Chris@0
|
171 $admin_theme = $config->get('admin');
|
Chris@0
|
172 if ($admin_theme != 0 && $admin_theme != $theme) {
|
Chris@17
|
173 $this->messenger()
|
Chris@17
|
174 ->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
|
175 '%admin_theme' => $themes[$admin_theme]->info['name'],
|
Chris@17
|
176 '%selected_theme' => $themes[$theme]->info['name'],
|
Chris@17
|
177 ]));
|
Chris@0
|
178 }
|
Chris@0
|
179 else {
|
Chris@17
|
180 $this->messenger()->addStatus($this->t('%theme is now the default theme.', ['%theme' => $themes[$theme]->info['name']]));
|
Chris@0
|
181 }
|
Chris@0
|
182 }
|
Chris@0
|
183 else {
|
Chris@17
|
184 $this->messenger()->addError($this->t('The %theme theme was not found.', ['%theme' => $theme]));
|
Chris@0
|
185 }
|
Chris@0
|
186
|
Chris@0
|
187 return $this->redirect('system.themes_page');
|
Chris@0
|
188
|
Chris@0
|
189 }
|
Chris@0
|
190 throw new AccessDeniedHttpException();
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 }
|