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