ThemeInitialization.php in Drupal 10
File
core/lib/Drupal/Core/Theme/ThemeInitialization.php
View source
<?php
namespace Drupal\Core\Theme;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
class ThemeInitialization implements ThemeInitializationInterface {
protected $themeHandler;
protected $cache;
protected $root;
protected $extensions;
protected $moduleHandler;
public function __construct($root, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache, ModuleHandlerInterface $module_handler) {
$this->root = $root;
$this->themeHandler = $theme_handler;
$this->cache = $cache;
$this->moduleHandler = $module_handler;
}
public function initTheme($theme_name) {
$active_theme = $this
->getActiveThemeByName($theme_name);
$this
->loadActiveTheme($active_theme);
return $active_theme;
}
public function getActiveThemeByName($theme_name) {
if ($cached = $this->cache
->get('theme.active_theme.' . $theme_name)) {
return $cached->data;
}
$themes = $this->themeHandler
->listInfo();
if (empty($themes) || !$theme_name || !isset($themes[$theme_name])) {
$theme_name = 'core';
$active_theme = $this
->getActiveTheme(new Extension($this->root, 'theme', 'core/core.info.yml'));
return $active_theme;
}
$base_themes = [];
$ancestor = $theme_name;
while ($ancestor && isset($themes[$ancestor]->base_theme)) {
$ancestor = $themes[$ancestor]->base_theme;
if (!$this->themeHandler
->themeExists($ancestor)) {
if ($ancestor == 'stable') {
continue;
}
throw new MissingThemeDependencyException(sprintf('Base theme %s has not been installed.', $ancestor), $ancestor);
}
$base_themes[] = $themes[$ancestor];
}
$active_theme = $this
->getActiveTheme($themes[$theme_name], $base_themes);
$this->cache
->set('theme.active_theme.' . $theme_name, $active_theme);
return $active_theme;
}
public function loadActiveTheme(ActiveTheme $active_theme) {
if ($theme_engine = $active_theme
->getEngine()) {
include_once $this->root . '/' . $active_theme
->getOwner();
foreach ($active_theme
->getBaseThemeExtensions() as $base) {
$base
->load();
}
$active_theme
->getExtension()
->load();
}
else {
foreach ($active_theme
->getBaseThemeExtensions() as $base) {
if ($base->owner) {
include_once $this->root . '/' . $base->owner;
}
}
if ($active_theme
->getOwner()) {
include_once $this->root . '/' . $active_theme
->getOwner();
}
}
include_once $this->root . '/core/themes/engines/twig/twig.engine';
}
public function getActiveTheme(Extension $theme, array $base_themes = []) {
$theme_path = $theme
->getPath();
$values['path'] = $theme_path;
$values['name'] = $theme
->getName();
if (!empty($theme->info['logo'])) {
$values['logo'] = $theme
->getPath() . '/' . $theme->info['logo'];
}
else {
$values['logo'] = $theme
->getPath() . '/logo.svg';
}
$values['libraries_override'] = [];
foreach ($base_themes as $base) {
if (!empty($base->info['libraries-override'])) {
foreach ($base->info['libraries-override'] as $library => $override) {
$values['libraries_override'][$base
->getPath()][$library] = $override;
}
}
}
if (!empty($theme->info['libraries-override'])) {
foreach ($theme->info['libraries-override'] as $library => $override) {
$values['libraries_override'][$theme
->getPath()][$library] = $override;
}
}
foreach ($base_themes as $base) {
if (!empty($base->info['libraries-extend'])) {
foreach ($base->info['libraries-extend'] as $library => $extend) {
if (isset($values['libraries_extend'][$library])) {
$values['libraries_extend'][$library] = array_merge($values['libraries_extend'][$library], $extend);
}
else {
$values['libraries_extend'][$library] = $extend;
}
}
}
}
if (!empty($theme->info['libraries-extend'])) {
foreach ($theme->info['libraries-extend'] as $library => $extend) {
if (isset($values['libraries_extend'][$library])) {
$values['libraries_extend'][$library] = array_merge($values['libraries_extend'][$library], $extend);
}
else {
$values['libraries_extend'][$library] = $extend;
}
}
}
$values['libraries'] = [];
foreach ($base_themes as $base) {
if (!empty($base->libraries)) {
foreach ($base->libraries as $library) {
$values['libraries'][] = $library;
}
}
}
if (!empty($theme->libraries)) {
foreach ($theme->libraries as $library) {
$values['libraries'][] = $library;
}
}
$values['engine'] = $theme->engine ?? NULL;
$values['owner'] = $theme->owner ?? NULL;
$values['extension'] = $theme;
$base_active_themes = [];
foreach ($base_themes as $base_theme) {
$base_active_themes[$base_theme
->getName()] = $base_theme;
}
$values['base_theme_extensions'] = $base_active_themes;
if (!empty($theme->info['regions'])) {
$values['regions'] = $theme->info['regions'];
}
return new ActiveTheme($values);
}
protected function getExtensions() {
if (!isset($this->extensions)) {
$this->extensions = array_merge($this->moduleHandler
->getModuleList(), $this->themeHandler
->listInfo());
}
return $this->extensions;
}
protected function resolveStyleSheetPlaceholders($css_file) {
$token_candidate = explode('/', $css_file)[0];
if (!preg_match('/@[A-z0-9_-]+/', $token_candidate)) {
return $css_file;
}
$token = substr($token_candidate, 1);
$extensions = $this
->getExtensions();
if (isset($extensions[$token])) {
return str_replace($token_candidate, $extensions[$token]
->getPath(), $css_file);
}
}
}