ActiveTheme.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace Drupal\Core\Theme;
  3. /**
  4. * Defines a theme and its information needed at runtime.
  5. *
  6. * The theme manager will store the active theme object.
  7. *
  8. * @see \Drupal\Core\Theme\ThemeManager
  9. * @see \Drupal\Core\Theme\ThemeInitialization
  10. */
  11. class ActiveTheme {
  12. /**
  13. * The machine name of the active theme.
  14. *
  15. * @var string
  16. */
  17. protected $name;
  18. /**
  19. * The path to the logo.
  20. *
  21. * @var string
  22. */
  23. protected $logo;
  24. /**
  25. * The path to the theme.
  26. *
  27. * @var string
  28. */
  29. protected $path;
  30. /**
  31. * The engine of the theme.
  32. *
  33. * @var string
  34. */
  35. protected $engine;
  36. /**
  37. * The path to the theme engine for root themes.
  38. *
  39. * @var string
  40. */
  41. protected $owner;
  42. /**
  43. * An array of base theme active theme objects keyed by name.
  44. *
  45. * @var static[]
  46. *
  47. * @deprecated in drupal:8.7.0 and is removed from drupal:9.0.0. Use
  48. * $this->baseThemeExtensions instead.
  49. *
  50. * @see https://www.drupal.org/node/3019948
  51. */
  52. protected $baseThemes;
  53. /**
  54. * An array of base theme extension objects keyed by name.
  55. *
  56. * @var \Drupal\Core\Extension\Extension[]
  57. */
  58. protected $baseThemeExtensions = [];
  59. /**
  60. * The extension object.
  61. *
  62. * @var \Drupal\Core\Extension\Extension
  63. */
  64. protected $extension;
  65. /**
  66. * The stylesheets which are set to be removed by the theme.
  67. *
  68. * @var array
  69. */
  70. protected $styleSheetsRemove;
  71. /**
  72. * The libraries provided by the theme.
  73. *
  74. * @var array
  75. */
  76. protected $libraries;
  77. /**
  78. * The regions provided by the theme.
  79. *
  80. * @var array
  81. */
  82. protected $regions;
  83. /**
  84. * The libraries or library assets overridden by the theme.
  85. *
  86. * @var array
  87. */
  88. protected $librariesOverride;
  89. /**
  90. * The list of libraries-extend definitions.
  91. *
  92. * @var array
  93. */
  94. protected $librariesExtend;
  95. /**
  96. * Constructs an ActiveTheme object.
  97. *
  98. * @param array $values
  99. * The properties of the object, keyed by the names.
  100. */
  101. public function __construct(array $values) {
  102. $values += [
  103. 'path' => '',
  104. 'engine' => 'twig',
  105. 'owner' => 'twig',
  106. 'logo' => '',
  107. 'stylesheets_remove' => [],
  108. 'libraries' => [],
  109. 'extension' => 'html.twig',
  110. 'base_theme_extensions' => [],
  111. 'regions' => [],
  112. 'libraries_override' => [],
  113. 'libraries_extend' => [],
  114. ];
  115. $this->name = $values['name'];
  116. $this->logo = $values['logo'];
  117. $this->path = $values['path'];
  118. $this->engine = $values['engine'];
  119. $this->owner = $values['owner'];
  120. $this->styleSheetsRemove = $values['stylesheets_remove'];
  121. $this->libraries = $values['libraries'];
  122. $this->extension = $values['extension'];
  123. $this->baseThemeExtensions = $values['base_theme_extensions'];
  124. if (!empty($values['base_themes']) && empty($this->baseThemeExtensions)) {
  125. @trigger_error("The 'base_themes' key is deprecated in Drupal 8.7.0 and support for it will be removed in Drupal 9.0.0. Use 'base_theme_extensions' instead. See https://www.drupal.org/node/3019948", E_USER_DEPRECATED);
  126. foreach ($values['base_themes'] as $base_theme) {
  127. $this->baseThemeExtensions[$base_theme->getName()] = $base_theme->getExtension();
  128. }
  129. }
  130. $this->regions = $values['regions'];
  131. $this->librariesOverride = $values['libraries_override'];
  132. $this->librariesExtend = $values['libraries_extend'];
  133. }
  134. /**
  135. * Returns the machine name of the theme.
  136. *
  137. * @return string
  138. */
  139. public function getName() {
  140. return $this->name;
  141. }
  142. /**
  143. * Returns the path to the theme directory.
  144. *
  145. * @return string
  146. */
  147. public function getPath() {
  148. return $this->path;
  149. }
  150. /**
  151. * Returns the theme engine.
  152. *
  153. * @return string
  154. */
  155. public function getEngine() {
  156. return $this->engine;
  157. }
  158. /**
  159. * Returns the path to the theme engine for root themes.
  160. *
  161. * @see \Drupal\Core\Extension\ThemeExtensionList::doList()
  162. *
  163. * @return mixed
  164. */
  165. public function getOwner() {
  166. return $this->owner;
  167. }
  168. /**
  169. * Returns the extension object.
  170. *
  171. * @return \Drupal\Core\Extension\Extension
  172. */
  173. public function getExtension() {
  174. return $this->extension;
  175. }
  176. /**
  177. * Returns the libraries provided by the theme.
  178. *
  179. * @return mixed
  180. */
  181. public function getLibraries() {
  182. return $this->libraries;
  183. }
  184. /**
  185. * Returns the removed stylesheets by the theme.
  186. *
  187. * This method is used as a BC layer to access the contents of the deprecated
  188. * stylesheets-remove key in theme info.yml files. It will be removed once it
  189. * is no longer needed in Drupal 10.
  190. *
  191. * @return mixed
  192. * The removed stylesheets.
  193. *
  194. * @see https://www.drupal.org/node/2497313
  195. *
  196. * @todo Remove in Drupal 10.0.x.
  197. *
  198. * @internal
  199. */
  200. public function getStyleSheetsRemove() {
  201. return $this->styleSheetsRemove;
  202. }
  203. /**
  204. * Returns an array of base theme active theme objects keyed by name.
  205. *
  206. * The order starts with the base theme of $this and ends with the root of
  207. * the dependency chain.
  208. *
  209. * @return static[]
  210. *
  211. * @deprecated in drupal:8.7.0 and is removed from drupal:9.0.0. Use
  212. * \Drupal\Core\Theme\ActiveTheme::getBaseThemeExtensions() instead.
  213. *
  214. * @see https://www.drupal.org/node/3019948
  215. */
  216. public function getBaseThemes() {
  217. @trigger_error('\Drupal\Core\Theme\ActiveTheme::getBaseThemes() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Theme\ActiveTheme::getBaseThemeExtensions() instead. See https://www.drupal.org/node/3019948', E_USER_DEPRECATED);
  218. /** @var \Drupal\Core\Theme\ThemeInitialization $theme_initialisation */
  219. $theme_initialisation = \Drupal::service('theme.initialization');
  220. $base_themes = array_combine(array_keys($this->baseThemeExtensions), array_keys($this->baseThemeExtensions));
  221. return array_map([$theme_initialisation, 'getActiveThemeByName'], $base_themes);
  222. }
  223. /**
  224. * Returns an array of base theme extension objects keyed by name.
  225. *
  226. * The order starts with the base theme of $this and ends with the root of
  227. * the dependency chain.
  228. *
  229. * @return \Drupal\Core\Extension\Extension[]
  230. */
  231. public function getBaseThemeExtensions() {
  232. return $this->baseThemeExtensions;
  233. }
  234. /**
  235. * Returns the logo provided by the theme.
  236. *
  237. * @return string
  238. * The logo path.
  239. */
  240. public function getLogo() {
  241. return $this->logo;
  242. }
  243. /**
  244. * The regions used by the theme.
  245. *
  246. * @return string[]
  247. * The list of region machine names supported by the theme.
  248. *
  249. * @see system_region_list()
  250. */
  251. public function getRegions() {
  252. return array_keys($this->regions);
  253. }
  254. /**
  255. * Returns the libraries or library assets overridden by the active theme.
  256. *
  257. * @return array
  258. * The list of libraries overrides.
  259. */
  260. public function getLibrariesOverride() {
  261. return $this->librariesOverride;
  262. }
  263. /**
  264. * Returns the libraries extended by the active theme.
  265. *
  266. * @return array
  267. * The list of libraries-extend definitions.
  268. */
  269. public function getLibrariesExtend() {
  270. return $this->librariesExtend;
  271. }
  272. }