ThemeInitialization.php in Drupal 8
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();
if (function_exists($theme_engine . '_init')) {
foreach ($active_theme
->getBaseThemeExtensions() as $base) {
call_user_func($theme_engine . '_init', $base);
}
call_user_func($theme_engine . '_init', $active_theme
->getExtension());
}
}
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['stylesheets_remove'] = $this
->prepareStylesheetsRemove($theme, $base_themes);
$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'] = isset($theme->engine) ? $theme->engine : NULL;
$values['owner'] = isset($theme->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);
}
}
protected function prepareStylesheetsRemove(Extension $theme, $base_themes) {
$stylesheets_remove = [];
foreach ($base_themes as $base) {
if (!empty($base->info['stylesheets-remove'])) {
@trigger_error('The theme info key stylesheets-remove implemented by theme ' . $base
->getName() . ' is deprecated in drupal:8.0.0 and is removed from drupal:10.0.0. See https://www.drupal.org/node/2497313', E_USER_DEPRECATED);
foreach ($base->info['stylesheets-remove'] as $css_file) {
$css_file = $this
->resolveStyleSheetPlaceholders($css_file);
$stylesheets_remove[$css_file] = $css_file;
}
}
}
if (!empty($theme->info['stylesheets-remove'])) {
@trigger_error('The theme info key stylesheets-remove implemented by theme ' . $theme
->getName() . ' is deprecated in drupal:8.0.0 and is removed from drupal:10.0.0. See https://www.drupal.org/node/2497313', E_USER_DEPRECATED);
foreach ($theme->info['stylesheets-remove'] as $css_file) {
$css_file = $this
->resolveStyleSheetPlaceholders($css_file);
$stylesheets_remove[$css_file] = $css_file;
}
}
return $stylesheets_remove;
}
}