Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Theme;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\CacheBackendInterface;
|
Chris@0
|
6 use Drupal\Core\Extension\Extension;
|
Chris@0
|
7 use Drupal\Core\Extension\ModuleHandlerInterface;
|
Chris@0
|
8 use Drupal\Core\Extension\ThemeHandlerInterface;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Provides the theme initialization logic.
|
Chris@0
|
12 */
|
Chris@0
|
13 class ThemeInitialization implements ThemeInitializationInterface {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * The theme handler.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var \Drupal\Core\Extension\ThemeHandlerInterface
|
Chris@0
|
19 */
|
Chris@0
|
20 protected $themeHandler;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * The cache backend to use for the active theme.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var \Drupal\Core\Cache\CacheBackendInterface
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $cache;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * The app root.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var string
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $root;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * The extensions that might be attaching assets.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @var array
|
Chris@0
|
40 */
|
Chris@0
|
41 protected $extensions;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Constructs a new ThemeInitialization object.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @param string $root
|
Chris@0
|
47 * The app root.
|
Chris@0
|
48 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
|
Chris@0
|
49 * The theme handler.
|
Chris@0
|
50 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
|
Chris@0
|
51 * The cache backend.
|
Chris@0
|
52 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
Chris@0
|
53 * The module handler to use to load modules.
|
Chris@0
|
54 */
|
Chris@0
|
55 public function __construct($root, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache, ModuleHandlerInterface $module_handler) {
|
Chris@0
|
56 $this->root = $root;
|
Chris@0
|
57 $this->themeHandler = $theme_handler;
|
Chris@0
|
58 $this->cache = $cache;
|
Chris@0
|
59 $this->moduleHandler = $module_handler;
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * {@inheritdoc}
|
Chris@0
|
64 */
|
Chris@0
|
65 public function initTheme($theme_name) {
|
Chris@0
|
66 $active_theme = $this->getActiveThemeByName($theme_name);
|
Chris@0
|
67 $this->loadActiveTheme($active_theme);
|
Chris@0
|
68
|
Chris@0
|
69 return $active_theme;
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * {@inheritdoc}
|
Chris@0
|
74 */
|
Chris@0
|
75 public function getActiveThemeByName($theme_name) {
|
Chris@0
|
76 if ($cached = $this->cache->get('theme.active_theme.' . $theme_name)) {
|
Chris@0
|
77 return $cached->data;
|
Chris@0
|
78 }
|
Chris@0
|
79 $themes = $this->themeHandler->listInfo();
|
Chris@0
|
80
|
Chris@0
|
81 // If no theme could be negotiated, or if the negotiated theme is not within
|
Chris@0
|
82 // the list of installed themes, fall back to the default theme output of
|
Chris@0
|
83 // core and modules (like Stark, but without a theme extension at all). This
|
Chris@0
|
84 // is possible, because loadActiveTheme() always loads the Twig theme
|
Chris@0
|
85 // engine. This is desired, because missing or malformed theme configuration
|
Chris@0
|
86 // should not leave the application in a broken state. By falling back to
|
Chris@0
|
87 // default output, the user is able to reconfigure the theme through the UI.
|
Chris@0
|
88 // Lastly, tests are expected to operate with no theme by default, so as to
|
Chris@0
|
89 // only assert the original theme output of modules (unless a test manually
|
Chris@0
|
90 // installs a specific theme).
|
Chris@0
|
91 if (empty($themes) || !$theme_name || !isset($themes[$theme_name])) {
|
Chris@0
|
92 $theme_name = 'core';
|
Chris@0
|
93 // /core/core.info.yml does not actually exist, but is required because
|
Chris@0
|
94 // Extension expects a pathname.
|
Chris@0
|
95 $active_theme = $this->getActiveTheme(new Extension($this->root, 'theme', 'core/core.info.yml'));
|
Chris@0
|
96
|
Chris@0
|
97 // Early-return and do not set state, because the initialized $theme_name
|
Chris@0
|
98 // differs from the original $theme_name.
|
Chris@0
|
99 return $active_theme;
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 // Find all our ancestor themes and put them in an array.
|
Chris@0
|
103 $base_themes = [];
|
Chris@0
|
104 $ancestor = $theme_name;
|
Chris@0
|
105 while ($ancestor && isset($themes[$ancestor]->base_theme)) {
|
Chris@0
|
106 $ancestor = $themes[$ancestor]->base_theme;
|
Chris@0
|
107 if (!$this->themeHandler->themeExists($ancestor)) {
|
Chris@0
|
108 if ($ancestor == 'stable') {
|
Chris@0
|
109 // Themes that depend on Stable will be fixed by system_update_8014().
|
Chris@0
|
110 // There is no harm in not adding it as an ancestor since at worst
|
Chris@0
|
111 // some people might experience slight visual regressions on
|
Chris@0
|
112 // update.php.
|
Chris@0
|
113 continue;
|
Chris@0
|
114 }
|
Chris@0
|
115 throw new MissingThemeDependencyException(sprintf('Base theme %s has not been installed.', $ancestor), $ancestor);
|
Chris@0
|
116 }
|
Chris@0
|
117 $base_themes[] = $themes[$ancestor];
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 $active_theme = $this->getActiveTheme($themes[$theme_name], $base_themes);
|
Chris@0
|
121
|
Chris@0
|
122 $this->cache->set('theme.active_theme.' . $theme_name, $active_theme);
|
Chris@0
|
123 return $active_theme;
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 /**
|
Chris@0
|
127 * {@inheritdoc}
|
Chris@0
|
128 */
|
Chris@0
|
129 public function loadActiveTheme(ActiveTheme $active_theme) {
|
Chris@0
|
130 // Initialize the theme.
|
Chris@0
|
131 if ($theme_engine = $active_theme->getEngine()) {
|
Chris@0
|
132 // Include the engine.
|
Chris@0
|
133 include_once $this->root . '/' . $active_theme->getOwner();
|
Chris@0
|
134
|
Chris@0
|
135 if (function_exists($theme_engine . '_init')) {
|
Chris@0
|
136 foreach ($active_theme->getBaseThemes() as $base) {
|
Chris@0
|
137 call_user_func($theme_engine . '_init', $base->getExtension());
|
Chris@0
|
138 }
|
Chris@0
|
139 call_user_func($theme_engine . '_init', $active_theme->getExtension());
|
Chris@0
|
140 }
|
Chris@0
|
141 }
|
Chris@0
|
142 else {
|
Chris@0
|
143 // include non-engine theme files
|
Chris@0
|
144 foreach ($active_theme->getBaseThemes() as $base) {
|
Chris@0
|
145 // Include the theme file or the engine.
|
Chris@0
|
146 if ($base->getOwner()) {
|
Chris@0
|
147 include_once $this->root . '/' . $base->getOwner();
|
Chris@0
|
148 }
|
Chris@0
|
149 }
|
Chris@0
|
150 // and our theme gets one too.
|
Chris@0
|
151 if ($active_theme->getOwner()) {
|
Chris@0
|
152 include_once $this->root . '/' . $active_theme->getOwner();
|
Chris@0
|
153 }
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 // Always include Twig as the default theme engine.
|
Chris@0
|
157 include_once $this->root . '/core/themes/engines/twig/twig.engine';
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@0
|
160 /**
|
Chris@0
|
161 * {@inheritdoc}
|
Chris@0
|
162 */
|
Chris@0
|
163 public function getActiveTheme(Extension $theme, array $base_themes = []) {
|
Chris@0
|
164 $theme_path = $theme->getPath();
|
Chris@0
|
165
|
Chris@0
|
166 $values['path'] = $theme_path;
|
Chris@0
|
167 $values['name'] = $theme->getName();
|
Chris@0
|
168
|
Chris@0
|
169 // @todo Remove in Drupal 9.0.x.
|
Chris@0
|
170 $values['stylesheets_remove'] = $this->prepareStylesheetsRemove($theme, $base_themes);
|
Chris@0
|
171
|
Chris@0
|
172 // Prepare libraries overrides from this theme and ancestor themes. This
|
Chris@0
|
173 // allows child themes to easily remove CSS files from base themes and
|
Chris@0
|
174 // modules.
|
Chris@0
|
175 $values['libraries_override'] = [];
|
Chris@0
|
176
|
Chris@0
|
177 // Get libraries overrides declared by base themes.
|
Chris@0
|
178 foreach ($base_themes as $base) {
|
Chris@0
|
179 if (!empty($base->info['libraries-override'])) {
|
Chris@0
|
180 foreach ($base->info['libraries-override'] as $library => $override) {
|
Chris@0
|
181 $values['libraries_override'][$base->getPath()][$library] = $override;
|
Chris@0
|
182 }
|
Chris@0
|
183 }
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 // Add libraries overrides declared by this theme.
|
Chris@0
|
187 if (!empty($theme->info['libraries-override'])) {
|
Chris@0
|
188 foreach ($theme->info['libraries-override'] as $library => $override) {
|
Chris@0
|
189 $values['libraries_override'][$theme->getPath()][$library] = $override;
|
Chris@0
|
190 }
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 // Get libraries extensions declared by base themes.
|
Chris@0
|
194 foreach ($base_themes as $base) {
|
Chris@0
|
195 if (!empty($base->info['libraries-extend'])) {
|
Chris@0
|
196 foreach ($base->info['libraries-extend'] as $library => $extend) {
|
Chris@0
|
197 if (isset($values['libraries_extend'][$library])) {
|
Chris@0
|
198 // Merge if libraries-extend has already been defined for this
|
Chris@0
|
199 // library.
|
Chris@0
|
200 $values['libraries_extend'][$library] = array_merge($values['libraries_extend'][$library], $extend);
|
Chris@0
|
201 }
|
Chris@0
|
202 else {
|
Chris@0
|
203 $values['libraries_extend'][$library] = $extend;
|
Chris@0
|
204 }
|
Chris@0
|
205 }
|
Chris@0
|
206 }
|
Chris@0
|
207 }
|
Chris@0
|
208 // Add libraries extensions declared by this theme.
|
Chris@0
|
209 if (!empty($theme->info['libraries-extend'])) {
|
Chris@0
|
210 foreach ($theme->info['libraries-extend'] as $library => $extend) {
|
Chris@0
|
211 if (isset($values['libraries_extend'][$library])) {
|
Chris@0
|
212 // Merge if libraries-extend has already been defined for this
|
Chris@0
|
213 // library.
|
Chris@0
|
214 $values['libraries_extend'][$library] = array_merge($values['libraries_extend'][$library], $extend);
|
Chris@0
|
215 }
|
Chris@0
|
216 else {
|
Chris@0
|
217 $values['libraries_extend'][$library] = $extend;
|
Chris@0
|
218 }
|
Chris@0
|
219 }
|
Chris@0
|
220 }
|
Chris@0
|
221
|
Chris@0
|
222 // Do basically the same as the above for libraries
|
Chris@0
|
223 $values['libraries'] = [];
|
Chris@0
|
224
|
Chris@0
|
225 // Grab libraries from base theme
|
Chris@0
|
226 foreach ($base_themes as $base) {
|
Chris@0
|
227 if (!empty($base->libraries)) {
|
Chris@0
|
228 foreach ($base->libraries as $library) {
|
Chris@0
|
229 $values['libraries'][] = $library;
|
Chris@0
|
230 }
|
Chris@0
|
231 }
|
Chris@0
|
232 }
|
Chris@0
|
233
|
Chris@0
|
234 // Add libraries used by this theme.
|
Chris@0
|
235 if (!empty($theme->libraries)) {
|
Chris@0
|
236 foreach ($theme->libraries as $library) {
|
Chris@0
|
237 $values['libraries'][] = $library;
|
Chris@0
|
238 }
|
Chris@0
|
239 }
|
Chris@0
|
240
|
Chris@0
|
241 $values['engine'] = isset($theme->engine) ? $theme->engine : NULL;
|
Chris@0
|
242 $values['owner'] = isset($theme->owner) ? $theme->owner : NULL;
|
Chris@0
|
243 $values['extension'] = $theme;
|
Chris@0
|
244
|
Chris@0
|
245 $base_active_themes = [];
|
Chris@0
|
246 foreach ($base_themes as $base_theme) {
|
Chris@0
|
247 $base_active_themes[$base_theme->getName()] = $this->getActiveTheme($base_theme, array_slice($base_themes, 1));
|
Chris@0
|
248 }
|
Chris@0
|
249
|
Chris@0
|
250 $values['base_themes'] = $base_active_themes;
|
Chris@0
|
251 if (!empty($theme->info['regions'])) {
|
Chris@0
|
252 $values['regions'] = $theme->info['regions'];
|
Chris@0
|
253 }
|
Chris@0
|
254
|
Chris@0
|
255 return new ActiveTheme($values);
|
Chris@0
|
256 }
|
Chris@0
|
257
|
Chris@0
|
258 /**
|
Chris@0
|
259 * Gets all extensions.
|
Chris@0
|
260 *
|
Chris@0
|
261 * @return array
|
Chris@0
|
262 */
|
Chris@0
|
263 protected function getExtensions() {
|
Chris@0
|
264 if (!isset($this->extensions)) {
|
Chris@0
|
265 $this->extensions = array_merge($this->moduleHandler->getModuleList(), $this->themeHandler->listInfo());
|
Chris@0
|
266 }
|
Chris@0
|
267 return $this->extensions;
|
Chris@0
|
268 }
|
Chris@0
|
269
|
Chris@0
|
270 /**
|
Chris@0
|
271 * Gets CSS file where tokens have been resolved.
|
Chris@0
|
272 *
|
Chris@0
|
273 * @param string $css_file
|
Chris@0
|
274 * CSS file which may contain tokens.
|
Chris@0
|
275 *
|
Chris@0
|
276 * @return string
|
Chris@0
|
277 * CSS file where placeholders are replaced.
|
Chris@0
|
278 *
|
Chris@0
|
279 * @todo Remove in Drupal 9.0.x.
|
Chris@0
|
280 */
|
Chris@0
|
281 protected function resolveStyleSheetPlaceholders($css_file) {
|
Chris@0
|
282 $token_candidate = explode('/', $css_file)[0];
|
Chris@0
|
283 if (!preg_match('/@[A-z0-9_-]+/', $token_candidate)) {
|
Chris@0
|
284 return $css_file;
|
Chris@0
|
285 }
|
Chris@0
|
286
|
Chris@0
|
287 $token = substr($token_candidate, 1);
|
Chris@0
|
288
|
Chris@0
|
289 // Prime extensions.
|
Chris@0
|
290 $extensions = $this->getExtensions();
|
Chris@0
|
291 if (isset($extensions[$token])) {
|
Chris@0
|
292 return str_replace($token_candidate, $extensions[$token]->getPath(), $css_file);
|
Chris@0
|
293 }
|
Chris@0
|
294 }
|
Chris@0
|
295
|
Chris@0
|
296 /**
|
Chris@0
|
297 * Prepares stylesheets-remove specified in the *.info.yml file.
|
Chris@0
|
298 *
|
Chris@0
|
299 * @param \Drupal\Core\Extension\Extension $theme
|
Chris@0
|
300 * The theme extension object.
|
Chris@0
|
301 * @param \Drupal\Core\Extension\Extension[] $base_themes
|
Chris@0
|
302 * An array of base themes.
|
Chris@0
|
303 *
|
Chris@0
|
304 * @return string[]
|
Chris@0
|
305 * The list of stylesheets-remove specified in the *.info.yml file.
|
Chris@0
|
306 *
|
Chris@0
|
307 * @todo Remove in Drupal 9.0.x.
|
Chris@0
|
308 */
|
Chris@0
|
309 protected function prepareStylesheetsRemove(Extension $theme, $base_themes) {
|
Chris@0
|
310 // Prepare stylesheets from this theme as well as all ancestor themes.
|
Chris@0
|
311 // We work it this way so that we can have child themes remove CSS files
|
Chris@0
|
312 // easily from parent.
|
Chris@0
|
313 $stylesheets_remove = [];
|
Chris@0
|
314 // Grab stylesheets from base theme.
|
Chris@0
|
315 foreach ($base_themes as $base) {
|
Chris@0
|
316 if (!empty($base->info['stylesheets-remove'])) {
|
Chris@0
|
317 foreach ($base->info['stylesheets-remove'] as $css_file) {
|
Chris@0
|
318 $css_file = $this->resolveStyleSheetPlaceholders($css_file);
|
Chris@0
|
319 $stylesheets_remove[$css_file] = $css_file;
|
Chris@0
|
320 }
|
Chris@0
|
321 }
|
Chris@0
|
322 }
|
Chris@0
|
323
|
Chris@0
|
324 // Add stylesheets used by this theme.
|
Chris@0
|
325 if (!empty($theme->info['stylesheets-remove'])) {
|
Chris@0
|
326 foreach ($theme->info['stylesheets-remove'] as $css_file) {
|
Chris@0
|
327 $css_file = $this->resolveStyleSheetPlaceholders($css_file);
|
Chris@0
|
328 $stylesheets_remove[$css_file] = $css_file;
|
Chris@0
|
329 }
|
Chris@0
|
330 }
|
Chris@0
|
331 return $stylesheets_remove;
|
Chris@0
|
332 }
|
Chris@0
|
333
|
Chris@0
|
334 }
|