PageRenderTime 202ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/Drupal/Core/Theme/ThemeInitialization.php

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