ThemeHandler.php in Drupal 10
File
core/lib/Drupal/Core/Extension/ThemeHandler.php
View source
<?php
namespace Drupal\Core\Extension;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\Exception\UnknownExtensionException;
class ThemeHandler implements ThemeHandlerInterface {
protected $list;
protected $configFactory;
protected $themeList;
protected $root;
public function __construct($root, ConfigFactoryInterface $config_factory, ThemeExtensionList $theme_list) {
$this->root = $root;
$this->configFactory = $config_factory;
$this->themeList = $theme_list;
}
public function getDefault() {
return $this->configFactory
->get('system.theme')
->get('default');
}
public function listInfo() {
if (!isset($this->list)) {
$this->list = [];
$installed_themes = $this->configFactory
->get('core.extension')
->get('theme');
if (!empty($installed_themes)) {
$installed_themes = array_intersect_key($this->themeList
->getList(), $installed_themes);
array_map([
$this,
'addTheme',
], $installed_themes);
}
}
return $this->list;
}
public function addTheme(Extension $theme) {
\Drupal::service('class_loader')
->addPsr4('Drupal\\' . $theme
->getName() . '\\', $this->root . '/' . $theme
->getPath() . '/src');
if (!empty($theme->info['libraries'])) {
foreach ($theme->info['libraries'] as $library => $name) {
$theme->libraries[$library] = $name;
}
}
if (isset($theme->info['engine'])) {
$theme->engine = $theme->info['engine'];
}
if (isset($theme->info['base theme'])) {
$theme->base_theme = $theme->info['base theme'];
}
$this->list[$theme
->getName()] = $theme;
}
public function refreshInfo() {
$installed = $this->configFactory
->get('core.extension')
->get('theme');
if (empty($installed) && empty($this->list)) {
return;
}
$this
->reset();
}
public function reset() {
$this->themeList
->reset();
$this->list = NULL;
}
public function rebuildThemeData() {
return $this->themeList
->reset()
->getList();
}
public function getBaseThemes(array $themes, $theme) {
return $this->themeList
->getBaseThemes($themes, $theme);
}
public function getName($theme) {
return $this->themeList
->getName($theme);
}
public function getThemeDirectories() {
$dirs = [];
foreach ($this
->listInfo() as $name => $theme) {
$dirs[$name] = $this->root . '/' . $theme
->getPath();
}
return $dirs;
}
public function themeExists($theme) {
$themes = $this
->listInfo();
return isset($themes[$theme]);
}
public function getTheme($name) {
$themes = $this
->listInfo();
if (isset($themes[$name])) {
return $themes[$name];
}
throw new UnknownExtensionException(sprintf('The theme %s does not exist.', $name));
}
public function hasUi($name) {
$themes = $this
->listInfo();
if (isset($themes[$name])) {
if (!empty($themes[$name]->info['hidden'])) {
$theme_config = $this->configFactory
->get('system.theme');
return $name == $theme_config
->get('default') || $name == $theme_config
->get('admin');
}
return TRUE;
}
return FALSE;
}
}
Classes
Name |
Description |
ThemeHandler |
Default theme handler using the config system to store installation statuses. |