ThemeInitialization.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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->getBaseThemes() as $base) {
  125. call_user_func($theme_engine . '_init', $base->getExtension());
  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->getBaseThemes() as $base) {
  133. // Include the theme file or the engine.
  134. if ($base->getOwner()) {
  135. include_once $this->root . '/' . $base->getOwner();
  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. // @todo Remove in Drupal 9.0.x.
  154. $values['stylesheets_remove'] = $this->prepareStylesheetsRemove($theme, $base_themes);
  155. // Prepare libraries overrides from this theme and ancestor themes. This
  156. // allows child themes to easily remove CSS files from base themes and
  157. // modules.
  158. $values['libraries_override'] = [];
  159. // Get libraries overrides declared by base themes.
  160. foreach ($base_themes as $base) {
  161. if (!empty($base->info['libraries-override'])) {
  162. foreach ($base->info['libraries-override'] as $library => $override) {
  163. $values['libraries_override'][$base->getPath()][$library] = $override;
  164. }
  165. }
  166. }
  167. // Add libraries overrides declared by this theme.
  168. if (!empty($theme->info['libraries-override'])) {
  169. foreach ($theme->info['libraries-override'] as $library => $override) {
  170. $values['libraries_override'][$theme->getPath()][$library] = $override;
  171. }
  172. }
  173. // Get libraries extensions declared by base themes.
  174. foreach ($base_themes as $base) {
  175. if (!empty($base->info['libraries-extend'])) {
  176. foreach ($base->info['libraries-extend'] as $library => $extend) {
  177. if (isset($values['libraries_extend'][$library])) {
  178. // Merge if libraries-extend has already been defined for this
  179. // library.
  180. $values['libraries_extend'][$library] = array_merge($values['libraries_extend'][$library], $extend);
  181. }
  182. else {
  183. $values['libraries_extend'][$library] = $extend;
  184. }
  185. }
  186. }
  187. }
  188. // Add libraries extensions declared by this theme.
  189. if (!empty($theme->info['libraries-extend'])) {
  190. foreach ($theme->info['libraries-extend'] as $library => $extend) {
  191. if (isset($values['libraries_extend'][$library])) {
  192. // Merge if libraries-extend has already been defined for this
  193. // library.
  194. $values['libraries_extend'][$library] = array_merge($values['libraries_extend'][$library], $extend);
  195. }
  196. else {
  197. $values['libraries_extend'][$library] = $extend;
  198. }
  199. }
  200. }
  201. // Do basically the same as the above for libraries
  202. $values['libraries'] = [];
  203. // Grab libraries from base theme
  204. foreach ($base_themes as $base) {
  205. if (!empty($base->libraries)) {
  206. foreach ($base->libraries as $library) {
  207. $values['libraries'][] = $library;
  208. }
  209. }
  210. }
  211. // Add libraries used by this theme.
  212. if (!empty($theme->libraries)) {
  213. foreach ($theme->libraries as $library) {
  214. $values['libraries'][] = $library;
  215. }
  216. }
  217. $values['engine'] = isset($theme->engine) ? $theme->engine : NULL;
  218. $values['owner'] = isset($theme->owner) ? $theme->owner : NULL;
  219. $values['extension'] = $theme;
  220. $base_active_themes = [];
  221. foreach ($base_themes as $base_theme) {
  222. $base_active_themes[$base_theme->getName()] = $this->getActiveTheme($base_theme, array_slice($base_themes, 1));
  223. }
  224. $values['base_themes'] = $base_active_themes;
  225. if (!empty($theme->info['regions'])) {
  226. $values['regions'] = $theme->info['regions'];
  227. }
  228. return new ActiveTheme($values);
  229. }
  230. /**
  231. * Gets all extensions.
  232. *
  233. * @return array
  234. */
  235. protected function getExtensions() {
  236. if (!isset($this->extensions)) {
  237. $this->extensions = array_merge($this->moduleHandler->getModuleList(), $this->themeHandler->listInfo());
  238. }
  239. return $this->extensions;
  240. }
  241. /**
  242. * Gets CSS file where tokens have been resolved.
  243. *
  244. * @param string $css_file
  245. * CSS file which may contain tokens.
  246. *
  247. * @return string
  248. * CSS file where placeholders are replaced.
  249. *
  250. * @todo Remove in Drupal 9.0.x.
  251. */
  252. protected function resolveStyleSheetPlaceholders($css_file) {
  253. $token_candidate = explode('/', $css_file)[0];
  254. if (!preg_match('/@[A-z0-9_-]+/', $token_candidate)) {
  255. return $css_file;
  256. }
  257. $token = substr($token_candidate, 1);
  258. // Prime extensions.
  259. $extensions = $this->getExtensions();
  260. if (isset($extensions[$token])) {
  261. return str_replace($token_candidate, $extensions[$token]->getPath(), $css_file);
  262. }
  263. }
  264. /**
  265. * Prepares stylesheets-remove specified in the *.info.yml file.
  266. *
  267. * @param \Drupal\Core\Extension\Extension $theme
  268. * The theme extension object.
  269. * @param \Drupal\Core\Extension\Extension[] $base_themes
  270. * An array of base themes.
  271. *
  272. * @return string[]
  273. * The list of stylesheets-remove specified in the *.info.yml file.
  274. *
  275. * @todo Remove in Drupal 9.0.x.
  276. */
  277. protected function prepareStylesheetsRemove(Extension $theme, $base_themes) {
  278. // Prepare stylesheets from this theme as well as all ancestor themes.
  279. // We work it this way so that we can have child themes remove CSS files
  280. // easily from parent.
  281. $stylesheets_remove = [];
  282. // Grab stylesheets from base theme.
  283. foreach ($base_themes as $base) {
  284. if (!empty($base->info['stylesheets-remove'])) {
  285. foreach ($base->info['stylesheets-remove'] as $css_file) {
  286. $css_file = $this->resolveStyleSheetPlaceholders($css_file);
  287. $stylesheets_remove[$css_file] = $css_file;
  288. }
  289. }
  290. }
  291. // Add stylesheets used by this theme.
  292. if (!empty($theme->info['stylesheets-remove'])) {
  293. foreach ($theme->info['stylesheets-remove'] as $css_file) {
  294. $css_file = $this->resolveStyleSheetPlaceholders($css_file);
  295. $stylesheets_remove[$css_file] = $css_file;
  296. }
  297. }
  298. return $stylesheets_remove;
  299. }
  300. }