3 namespace Drupal\Core\Extension;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\State\StateInterface;
9 * Default theme handler using the config system to store installation statuses.
11 class ThemeHandler implements ThemeHandlerInterface {
14 * Contains the features enabled for themes by default.
18 * @see _system_default_theme_features()
20 protected $defaultFeatures = [
24 'comment_user_picture',
25 'comment_user_verification',
29 * A list of all currently available themes.
36 * The config factory to get the installed themes.
38 * @var \Drupal\Core\Config\ConfigFactoryInterface
40 protected $configFactory;
43 * The module handler to fire themes_installed/themes_uninstalled hooks.
45 * @var \Drupal\Core\Extension\ModuleHandlerInterface
47 protected $moduleHandler;
52 * @var \Drupal\Core\State\StateInterface
57 * The config installer to install configuration.
59 * @var \Drupal\Core\Config\ConfigInstallerInterface
61 protected $configInstaller;
64 * The info parser to parse the theme.info.yml files.
66 * @var \Drupal\Core\Extension\InfoParserInterface
68 protected $infoParser;
73 * @var \Psr\Log\LoggerInterface
78 * The route builder to rebuild the routes if a theme is installed.
80 * @var \Drupal\Core\Routing\RouteBuilderInterface
82 protected $routeBuilder;
85 * An extension discovery instance.
87 * @var \Drupal\Core\Extension\ExtensionDiscovery
89 protected $extensionDiscovery;
92 * The CSS asset collection optimizer service.
94 * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
96 protected $cssCollectionOptimizer;
99 * The config manager used to uninstall a theme.
101 * @var \Drupal\Core\Config\ConfigManagerInterface
103 protected $configManager;
113 * Constructs a new ThemeHandler.
115 * @param string $root
117 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
118 * The config factory to get the installed themes.
119 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
120 * The module handler to fire themes_installed/themes_uninstalled hooks.
121 * @param \Drupal\Core\State\StateInterface $state
123 * @param \Drupal\Core\Extension\InfoParserInterface $info_parser
124 * The info parser to parse the theme.info.yml files.
125 * @param \Drupal\Core\Extension\ExtensionDiscovery $extension_discovery
126 * (optional) A extension discovery instance (for unit tests).
128 public function __construct($root, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, StateInterface $state, InfoParserInterface $info_parser, ExtensionDiscovery $extension_discovery = NULL) {
130 $this->configFactory = $config_factory;
131 $this->moduleHandler = $module_handler;
132 $this->state = $state;
133 $this->infoParser = $info_parser;
134 $this->extensionDiscovery = $extension_discovery;
140 public function getDefault() {
141 return $this->configFactory->get('system.theme')->get('default');
147 public function setDefault($name) {
148 $list = $this->listInfo();
149 if (!isset($list[$name])) {
150 throw new \InvalidArgumentException("$name theme is not installed.");
152 $this->configFactory->getEditable('system.theme')
153 ->set('default', $name)
161 public function install(array $theme_list, $install_dependencies = TRUE) {
162 // We keep the old install() method as BC layer but redirect directly to the
164 return \Drupal::service('theme_installer')->install($theme_list, $install_dependencies);
170 public function uninstall(array $theme_list) {
171 // We keep the old uninstall() method as BC layer but redirect directly to
172 // the theme installer.
173 \Drupal::service('theme_installer')->uninstall($theme_list);
179 public function listInfo() {
180 if (!isset($this->list)) {
182 $themes = $this->systemThemeList();
183 // @todo Ensure that systemThemeList() does not contain an empty list
184 // during the batch installer, see https://www.drupal.org/node/2322619.
185 if (empty($themes)) {
186 $this->refreshInfo();
187 $this->list = $this->list ?: [];
188 $themes = \Drupal::state()->get('system.theme.data', []);
190 foreach ($themes as $theme) {
191 $this->addTheme($theme);
200 public function addTheme(Extension $theme) {
201 if (!empty($theme->info['libraries'])) {
202 foreach ($theme->info['libraries'] as $library => $name) {
203 $theme->libraries[$library] = $name;
206 if (isset($theme->info['engine'])) {
207 $theme->engine = $theme->info['engine'];
209 if (isset($theme->info['base theme'])) {
210 $theme->base_theme = $theme->info['base theme'];
212 $this->list[$theme->getName()] = $theme;
218 public function refreshInfo() {
220 $extension_config = $this->configFactory->get('core.extension');
221 $installed = $extension_config->get('theme');
223 // @todo Avoid re-scanning all themes by retaining the original (unaltered)
224 // theme info somewhere.
225 $list = $this->rebuildThemeData();
226 foreach ($list as $name => $theme) {
227 if (isset($installed[$name])) {
228 $this->addTheme($theme);
231 $this->state->set('system.theme.data', $this->list);
237 public function reset() {
238 $this->systemListReset();
245 public function rebuildThemeData() {
246 $listing = $this->getExtensionDiscovery();
247 $themes = $listing->scan('theme');
248 $engines = $listing->scan('theme_engine');
249 $extension_config = $this->configFactory->get('core.extension');
250 $installed = $extension_config->get('theme') ?: [];
252 // Set defaults for theme info.
255 'base theme' => 'stable',
257 'sidebar_first' => 'Left sidebar',
258 'sidebar_second' => 'Right sidebar',
259 'content' => 'Content',
260 'header' => 'Header',
261 'primary_menu' => 'Primary menu',
262 'secondary_menu' => 'Secondary menu',
263 'footer' => 'Footer',
264 'highlighted' => 'Highlighted',
266 'page_top' => 'Page top',
267 'page_bottom' => 'Page bottom',
268 'breadcrumb' => 'Breadcrumb',
271 'features' => $this->defaultFeatures,
272 'screenshot' => 'screenshot.png',
273 'php' => DRUPAL_MINIMUM_PHP,
279 $files_theme_engine = [];
280 // Read info files for each theme.
281 foreach ($themes as $key => $theme) {
282 // @todo Remove all code that relies on the $status property.
283 $theme->status = (int) isset($installed[$key]);
285 $theme->info = $this->infoParser->parse($theme->getPathname()) + $defaults;
286 // Remove the default Stable base theme when 'base theme: false' is set in
287 // a theme .info.yml file.
288 if ($theme->info['base theme'] === FALSE) {
289 unset($theme->info['base theme']);
292 // Add the info file modification time, so it becomes available for
293 // contributed modules to use for ordering theme lists.
294 $theme->info['mtime'] = $theme->getMTime();
296 // Invoke hook_system_info_alter() to give installed modules a chance to
297 // modify the data in the .info.yml files if necessary.
298 // @todo Remove $type argument, obsolete with $theme->getType().
300 $this->moduleHandler->alter('system_info', $theme->info, $theme, $type);
302 if (!empty($theme->info['base theme'])) {
303 $sub_themes[] = $key;
304 // Add the base theme as a proper dependency.
305 $themes[$key]->info['dependencies'][] = $themes[$key]->info['base theme'];
308 // Defaults to 'twig' (see $defaults above).
309 $engine = $theme->info['engine'];
310 if (isset($engines[$engine])) {
311 $theme->owner = $engines[$engine]->getExtensionPathname();
312 $theme->prefix = $engines[$engine]->getName();
313 $files_theme_engine[$engine] = $engines[$engine]->getPathname();
316 // Prefix screenshot with theme path.
317 if (!empty($theme->info['screenshot'])) {
318 $theme->info['screenshot'] = $theme->getPath() . '/' . $theme->info['screenshot'];
321 $files_theme[$key] = $theme->getPathname();
323 // Build dependencies.
324 // @todo Move into a generic ExtensionHandler base class.
325 // @see https://www.drupal.org/node/2208429
326 $themes = $this->moduleHandler->buildModuleDependencies($themes);
328 // Store filenames to allow system_list() and drupal_get_filename() to
329 // retrieve them for themes and theme engines without having to scan the
331 $this->state->set('system.theme.files', $files_theme);
332 $this->state->set('system.theme_engine.files', $files_theme_engine);
334 // After establishing the full list of available themes, fill in data for
336 foreach ($sub_themes as $key) {
337 $sub_theme = $themes[$key];
338 // The $base_themes property is optional; only set for sub themes.
339 // @see ThemeHandlerInterface::listInfo()
340 $sub_theme->base_themes = $this->getBaseThemes($themes, $key);
341 // empty() cannot be used here, since ThemeHandler::doGetBaseThemes() adds
342 // the key of a base theme with a value of NULL in case it is not found,
343 // in order to prevent needless iterations.
344 if (!current($sub_theme->base_themes)) {
347 // Determine the root base theme.
348 $root_key = key($sub_theme->base_themes);
349 // Build the list of sub-themes for each of the theme's base themes.
350 foreach (array_keys($sub_theme->base_themes) as $base_theme) {
351 $themes[$base_theme]->sub_themes[$key] = $sub_theme->info['name'];
353 // Add the theme engine info from the root base theme.
354 if (isset($themes[$root_key]->owner)) {
355 $sub_theme->info['engine'] = $themes[$root_key]->info['engine'];
356 $sub_theme->owner = $themes[$root_key]->owner;
357 $sub_theme->prefix = $themes[$root_key]->prefix;
367 public function getBaseThemes(array $themes, $theme) {
368 return $this->doGetBaseThemes($themes, $theme);
372 * Finds the base themes for the specific theme.
374 * @param array $themes
375 * An array of available themes.
376 * @param string $theme
377 * The name of the theme whose base we are looking for.
378 * @param array $used_themes
379 * (optional) A recursion parameter preventing endless loops. Defaults to
383 * An array of base themes.
385 protected function doGetBaseThemes(array $themes, $theme, $used_themes = []) {
386 if (!isset($themes[$theme]->info['base theme'])) {
390 $base_key = $themes[$theme]->info['base theme'];
391 // Does the base theme exist?
392 if (!isset($themes[$base_key])) {
393 return [$base_key => NULL];
396 $current_base_theme = [$base_key => $themes[$base_key]->info['name']];
398 // Is the base theme itself a child of another theme?
399 if (isset($themes[$base_key]->info['base theme'])) {
400 // Do we already know the base themes of this theme?
401 if (isset($themes[$base_key]->base_themes)) {
402 return $themes[$base_key]->base_themes + $current_base_theme;
405 if (!empty($used_themes[$base_key])) {
406 return [$base_key => NULL];
408 $used_themes[$base_key] = TRUE;
409 return $this->doGetBaseThemes($themes, $base_key, $used_themes) + $current_base_theme;
411 // If we get here, then this is our parent theme.
412 return $current_base_theme;
416 * Returns an extension discovery object.
418 * @return \Drupal\Core\Extension\ExtensionDiscovery
419 * The extension discovery object.
421 protected function getExtensionDiscovery() {
422 if (!isset($this->extensionDiscovery)) {
423 $this->extensionDiscovery = new ExtensionDiscovery($this->root);
425 return $this->extensionDiscovery;
431 public function getName($theme) {
432 $themes = $this->listInfo();
433 if (!isset($themes[$theme])) {
434 throw new \InvalidArgumentException("Requested the name of a non-existing theme $theme");
436 return $themes[$theme]->info['name'];
440 * Wraps system_list_reset().
442 protected function systemListReset() {
447 * Wraps system_list().
450 * A list of themes keyed by name.
452 protected function systemThemeList() {
453 return system_list('theme');
459 public function getThemeDirectories() {
461 foreach ($this->listInfo() as $name => $theme) {
462 $dirs[$name] = $this->root . '/' . $theme->getPath();
470 public function themeExists($theme) {
471 $themes = $this->listInfo();
472 return isset($themes[$theme]);
478 public function getTheme($name) {
479 $themes = $this->listInfo();
480 if (isset($themes[$name])) {
481 return $themes[$name];
483 throw new \InvalidArgumentException(sprintf('The theme %s does not exist.', $name));
489 public function hasUi($name) {
490 $themes = $this->listInfo();
491 if (isset($themes[$name])) {
492 if (!empty($themes[$name]->info['hidden'])) {
493 $theme_config = $this->configFactory->get('system.theme');
494 return $name == $theme_config->get('default') || $name == $theme_config->get('admin');