blob: 2dd0e05e485c3aca220c274ccf6926b28e54c758 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
<?php
namespace Drupal\Core\Extension;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\Exception\UnknownExtensionException;
/**
* Default theme handler using the config system to store installation statuses.
*/
class ThemeHandler implements ThemeHandlerInterface {
/**
* A list of all currently available themes.
*
* @var array
*/
protected $list;
/**
* The config factory to get the installed themes.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* An extension discovery instance.
*
* @var \Drupal\Core\Extension\ThemeExtensionList
*/
protected $themeList;
/**
* The app root.
*
* @var string
*/
protected $root;
/**
* Constructs a new ThemeHandler.
*
* @param string $root
* The app root.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory to get the installed themes.
* @param \Drupal\Core\Extension\ThemeExtensionList $theme_list
* An extension discovery instance.
*/
public function __construct($root, ConfigFactoryInterface $config_factory, ThemeExtensionList $theme_list) {
$this->root = $root;
$this->configFactory = $config_factory;
$this->themeList = $theme_list;
}
/**
* {@inheritdoc}
*/
public function getDefault() {
return $this->configFactory->get('system.theme')->get('default');
}
/**
* {@inheritdoc}
*/
public function listInfo() {
if (!isset($this->list)) {
$this->list = [];
$installed_themes = array_keys($this->configFactory->get('core.extension')->get('theme'));
if (!empty($installed_themes)) {
$list = $this->themeList->getList();
foreach ($installed_themes as $theme) {
// Do not add installed themes that cannot be found by the
// extension.list.theme service. If a theme does go missing from the
// file system any call to ::getTheme() will result in an exception
// and an error being logged. Ignoring the problem here allows the
// theme system to fix itself while updating.
if (isset($list[$theme])) {
$this->addTheme($list[$theme]);
}
}
}
}
return $this->list;
}
/**
* {@inheritdoc}
*/
public function addTheme(Extension $theme) {
// Register the namespaces of installed themes.
// @todo Implement proper theme registration
// https://www.drupal.org/project/drupal/issues/2941757
\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;
}
/**
* {@inheritdoc}
*/
public function refreshInfo() {
$installed = $this->configFactory->get('core.extension')->get('theme');
// Only refresh the info if a theme has been installed. Modules are
// installed before themes by the installer and this method is called during
// module installation.
if (empty($installed) && empty($this->list)) {
return;
}
$this->reset();
}
/**
* {@inheritdoc}
*/
public function reset() {
$this->themeList->reset();
$this->list = NULL;
}
/**
* {@inheritdoc}
*/
public function rebuildThemeData() {
@trigger_error("\Drupal\Core\Extension\ThemeHandlerInterface::rebuildThemeData() is deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. Use \Drupal::service('extension.list.theme')->reset()->getList() instead. See https://www.drupal.org/node/3413196", E_USER_DEPRECATED);
return $this->themeList->reset()->getList();
}
/**
* {@inheritdoc}
*/
public function getBaseThemes(array $themes, $theme) {
@trigger_error("\Drupal\Core\Extension\ThemeHandlerInterface::getBaseThemes() is deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. There is no direct replacement. See https://www.drupal.org/node/3413187", E_USER_DEPRECATED);
return $this->themeList->getBaseThemes($themes, $theme);
}
/**
* {@inheritdoc}
*/
public function getName($theme) {
return $this->themeList->getName($theme);
}
/**
* {@inheritdoc}
*/
public function getThemeDirectories() {
$dirs = [];
foreach ($this->listInfo() as $name => $theme) {
$dirs[$name] = $this->root . '/' . $theme->getPath();
}
return $dirs;
}
/**
* {@inheritdoc}
*/
public function themeExists($theme) {
$themes = $this->listInfo();
return isset($themes[$theme]);
}
/**
* {@inheritdoc}
*/
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));
}
/**
* {@inheritdoc}
*/
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;
}
}
|