3 namespace Drupal\Core\Theme;
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Extension\Extension;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Extension\ThemeHandlerInterface;
11 * Provides the theme initialization logic.
13 class ThemeInitialization implements ThemeInitializationInterface {
18 * @var \Drupal\Core\Extension\ThemeHandlerInterface
20 protected $themeHandler;
23 * The cache backend to use for the active theme.
25 * @var \Drupal\Core\Cache\CacheBackendInterface
37 * The extensions that might be attaching assets.
41 protected $extensions;
46 * @var \Drupal\Core\Extension\ModuleHandlerInterface
48 protected $moduleHandler;
51 * Constructs a new ThemeInitialization object.
55 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
57 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
59 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
60 * The module handler to use to load modules.
62 public function __construct($root, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache, ModuleHandlerInterface $module_handler) {
64 $this->themeHandler = $theme_handler;
65 $this->cache = $cache;
66 $this->moduleHandler = $module_handler;
72 public function initTheme($theme_name) {
73 $active_theme = $this->getActiveThemeByName($theme_name);
74 $this->loadActiveTheme($active_theme);
82 public function getActiveThemeByName($theme_name) {
83 if ($cached = $this->cache->get('theme.active_theme.' . $theme_name)) {
86 $themes = $this->themeHandler->listInfo();
88 // If no theme could be negotiated, or if the negotiated theme is not within
89 // the list of installed themes, fall back to the default theme output of
90 // core and modules (like Stark, but without a theme extension at all). This
91 // is possible, because loadActiveTheme() always loads the Twig theme
92 // engine. This is desired, because missing or malformed theme configuration
93 // should not leave the application in a broken state. By falling back to
94 // default output, the user is able to reconfigure the theme through the UI.
95 // Lastly, tests are expected to operate with no theme by default, so as to
96 // only assert the original theme output of modules (unless a test manually
97 // installs a specific theme).
98 if (empty($themes) || !$theme_name || !isset($themes[$theme_name])) {
100 // /core/core.info.yml does not actually exist, but is required because
101 // Extension expects a pathname.
102 $active_theme = $this->getActiveTheme(new Extension($this->root, 'theme', 'core/core.info.yml'));
104 // Early-return and do not set state, because the initialized $theme_name
105 // differs from the original $theme_name.
106 return $active_theme;
109 // Find all our ancestor themes and put them in an array.
111 $ancestor = $theme_name;
112 while ($ancestor && isset($themes[$ancestor]->base_theme)) {
113 $ancestor = $themes[$ancestor]->base_theme;
114 if (!$this->themeHandler->themeExists($ancestor)) {
115 if ($ancestor == 'stable') {
116 // Themes that depend on Stable will be fixed by system_update_8014().
117 // There is no harm in not adding it as an ancestor since at worst
118 // some people might experience slight visual regressions on
122 throw new MissingThemeDependencyException(sprintf('Base theme %s has not been installed.', $ancestor), $ancestor);
124 $base_themes[] = $themes[$ancestor];
127 $active_theme = $this->getActiveTheme($themes[$theme_name], $base_themes);
129 $this->cache->set('theme.active_theme.' . $theme_name, $active_theme);
130 return $active_theme;
136 public function loadActiveTheme(ActiveTheme $active_theme) {
137 // Initialize the theme.
138 if ($theme_engine = $active_theme->getEngine()) {
139 // Include the engine.
140 include_once $this->root . '/' . $active_theme->getOwner();
142 if (function_exists($theme_engine . '_init')) {
143 foreach ($active_theme->getBaseThemes() as $base) {
144 call_user_func($theme_engine . '_init', $base->getExtension());
146 call_user_func($theme_engine . '_init', $active_theme->getExtension());
150 // include non-engine theme files
151 foreach ($active_theme->getBaseThemes() as $base) {
152 // Include the theme file or the engine.
153 if ($base->getOwner()) {
154 include_once $this->root . '/' . $base->getOwner();
157 // and our theme gets one too.
158 if ($active_theme->getOwner()) {
159 include_once $this->root . '/' . $active_theme->getOwner();
163 // Always include Twig as the default theme engine.
164 include_once $this->root . '/core/themes/engines/twig/twig.engine';
170 public function getActiveTheme(Extension $theme, array $base_themes = []) {
171 $theme_path = $theme->getPath();
173 $values['path'] = $theme_path;
174 $values['name'] = $theme->getName();
176 // Use the logo declared in this themes info file, otherwise use logo.svg
177 // from the themes root.
178 if (!empty($theme->info['logo'])) {
179 $values['logo'] = $theme->getPath() . '/' . $theme->info['logo'];
182 $values['logo'] = $theme->getPath() . '/logo.svg';
185 // @todo Remove in Drupal 9.0.x.
186 $values['stylesheets_remove'] = $this->prepareStylesheetsRemove($theme, $base_themes);
188 // Prepare libraries overrides from this theme and ancestor themes. This
189 // allows child themes to easily remove CSS files from base themes and
191 $values['libraries_override'] = [];
193 // Get libraries overrides declared by base themes.
194 foreach ($base_themes as $base) {
195 if (!empty($base->info['libraries-override'])) {
196 foreach ($base->info['libraries-override'] as $library => $override) {
197 $values['libraries_override'][$base->getPath()][$library] = $override;
202 // Add libraries overrides declared by this theme.
203 if (!empty($theme->info['libraries-override'])) {
204 foreach ($theme->info['libraries-override'] as $library => $override) {
205 $values['libraries_override'][$theme->getPath()][$library] = $override;
209 // Get libraries extensions declared by base themes.
210 foreach ($base_themes as $base) {
211 if (!empty($base->info['libraries-extend'])) {
212 foreach ($base->info['libraries-extend'] as $library => $extend) {
213 if (isset($values['libraries_extend'][$library])) {
214 // Merge if libraries-extend has already been defined for this
216 $values['libraries_extend'][$library] = array_merge($values['libraries_extend'][$library], $extend);
219 $values['libraries_extend'][$library] = $extend;
224 // Add libraries extensions declared by this theme.
225 if (!empty($theme->info['libraries-extend'])) {
226 foreach ($theme->info['libraries-extend'] as $library => $extend) {
227 if (isset($values['libraries_extend'][$library])) {
228 // Merge if libraries-extend has already been defined for this
230 $values['libraries_extend'][$library] = array_merge($values['libraries_extend'][$library], $extend);
233 $values['libraries_extend'][$library] = $extend;
238 // Do basically the same as the above for libraries
239 $values['libraries'] = [];
241 // Grab libraries from base theme
242 foreach ($base_themes as $base) {
243 if (!empty($base->libraries)) {
244 foreach ($base->libraries as $library) {
245 $values['libraries'][] = $library;
250 // Add libraries used by this theme.
251 if (!empty($theme->libraries)) {
252 foreach ($theme->libraries as $library) {
253 $values['libraries'][] = $library;
257 $values['engine'] = isset($theme->engine) ? $theme->engine : NULL;
258 $values['owner'] = isset($theme->owner) ? $theme->owner : NULL;
259 $values['extension'] = $theme;
261 $base_active_themes = [];
262 foreach ($base_themes as $base_theme) {
263 $base_active_themes[$base_theme->getName()] = $this->getActiveTheme($base_theme, array_slice($base_themes, 1));
266 $values['base_themes'] = $base_active_themes;
267 if (!empty($theme->info['regions'])) {
268 $values['regions'] = $theme->info['regions'];
271 return new ActiveTheme($values);
275 * Gets all extensions.
279 protected function getExtensions() {
280 if (!isset($this->extensions)) {
281 $this->extensions = array_merge($this->moduleHandler->getModuleList(), $this->themeHandler->listInfo());
283 return $this->extensions;
287 * Gets CSS file where tokens have been resolved.
289 * @param string $css_file
290 * CSS file which may contain tokens.
293 * CSS file where placeholders are replaced.
295 * @todo Remove in Drupal 9.0.x.
297 protected function resolveStyleSheetPlaceholders($css_file) {
298 $token_candidate = explode('/', $css_file)[0];
299 if (!preg_match('/@[A-z0-9_-]+/', $token_candidate)) {
303 $token = substr($token_candidate, 1);
306 $extensions = $this->getExtensions();
307 if (isset($extensions[$token])) {
308 return str_replace($token_candidate, $extensions[$token]->getPath(), $css_file);
313 * Prepares stylesheets-remove specified in the *.info.yml file.
315 * @param \Drupal\Core\Extension\Extension $theme
316 * The theme extension object.
317 * @param \Drupal\Core\Extension\Extension[] $base_themes
318 * An array of base themes.
321 * The list of stylesheets-remove specified in the *.info.yml file.
323 * @todo Remove in Drupal 9.0.x.
325 protected function prepareStylesheetsRemove(Extension $theme, $base_themes) {
326 // Prepare stylesheets from this theme as well as all ancestor themes.
327 // We work it this way so that we can have child themes remove CSS files
328 // easily from parent.
329 $stylesheets_remove = [];
330 // Grab stylesheets from base theme.
331 foreach ($base_themes as $base) {
332 if (!empty($base->info['stylesheets-remove'])) {
333 foreach ($base->info['stylesheets-remove'] as $css_file) {
334 $css_file = $this->resolveStyleSheetPlaceholders($css_file);
335 $stylesheets_remove[$css_file] = $css_file;
340 // Add stylesheets used by this theme.
341 if (!empty($theme->info['stylesheets-remove'])) {
342 foreach ($theme->info['stylesheets-remove'] as $css_file) {
343 $css_file = $this->resolveStyleSheetPlaceholders($css_file);
344 $stylesheets_remove[$css_file] = $css_file;
347 return $stylesheets_remove;