class ThemeSettingsProvider

Default implementation of the theme settings provider service.

Hierarchy

Expanded class hierarchy of ThemeSettingsProvider

16 files declare their use of ThemeSettingsProvider
BareHtmlPageRenderer.php in core/lib/Drupal/Core/Render/BareHtmlPageRenderer.php
CommentThemeHooks.php in core/modules/comment/src/Hook/CommentThemeHooks.php
NodeThemeHooks.php in core/modules/node/src/Hook/NodeThemeHooks.php
olivero.theme in core/themes/olivero/olivero.theme
Functions to support theming in the Olivero theme.
OliveroPagePreprocessHooks.php in core/themes/olivero/src/Hook/OliveroPagePreprocessHooks.php

... See full list

File

core/lib/Drupal/Core/Extension/ThemeSettingsProvider.php, line 19

Namespace

Drupal\Core\Extension
View source
class ThemeSettingsProvider {
  
  /**
   * An array of default theme features.
   *
   * @see \Drupal\Core\Extension\ThemeExtensionList::$defaults
   */
  public const DEFAULT_THEME_FEATURES = [
    'favicon',
    'logo',
    'node_user_picture',
    'comment_user_picture',
    'comment_user_verification',
  ];
  
  /**
   * Builds a new service instance.
   */
  public function __construct(protected readonly ThemeManagerInterface $themeManager, protected readonly ThemeInitializationInterface $themeInitialization, protected readonly ThemeHandlerInterface $themeHandler, protected readonly ConfigFactoryInterface $configFactory, protected readonly FileUrlGeneratorInterface $fileUrlGenerator, #[Autowire(service: 'cache.memory')] protected readonly CacheBackendInterface $memoryCache) {
  }
  
  /**
   * {@inheritdoc}
   */
  public function getSetting(string $setting_name, ?string $theme = NULL) : mixed {
    // If no key is given, use the current theme if we can determine it.
    if (!isset($theme)) {
      $theme = $this->themeManager
        ->getActiveTheme()
        ->getName();
    }
    $cid = 'theme_settings:' . $theme;
    $cacheItem = $this->memoryCache
      ->get($cid);
    if ($cacheItem) {
      /** @var \Drupal\Core\Theme\ThemeSettings $themeSettings */
      $themeSettings = $cacheItem->data;
    }
    else {
      $themeSettings = $this->buildThemeSettings($theme);
      $this->memoryCache
        ->set($cid, $themeSettings, tags: [
        'config:core.extension',
        'config:system.theme.global',
        sprintf('config:%s.settings', $theme),
      ]);
    }
    return $themeSettings->get($setting_name);
  }
  
  /**
   * Build a ThemeSettings object for a given theme.
   */
  protected function buildThemeSettings(string $theme) : ThemeSettings {
    // Create a theme settings object.
    $themeSettings = new ThemeSettings($theme);
    // Get the global settings from configuration.
    $themeSettings->setData($this->configFactory
      ->get('system.theme.global')
      ->get());
    // Get the values for the theme-specific settings from the .info.yml files
    // of the theme and all its base themes.
    $themes = $this->themeHandler
      ->listInfo();
    if (isset($themes[$theme])) {
      $themeObject = $themes[$theme];
      // Retrieve configured theme-specific settings, if any.
      try {
        if ($themeConfigSettings = $this->configFactory
          ->get($theme . '.settings')
          ->get()) {
          $themeSettings->merge($themeConfigSettings);
        }
      } catch (StorageException) {
      }
      // If the theme does not support a particular feature, override the
      // global setting and set the value to NULL.
      if (!empty($themeObject->info['features'])) {
        foreach (self::DEFAULT_THEME_FEATURES as $feature) {
          if (!in_array($feature, $themeObject->info['features'])) {
            $themeSettings->set('features.' . $feature, NULL);
          }
        }
      }
      // Generate the path to the logo image.
      if ($themeSettings->get('logo.use_default')) {
        $logo = $this->themeInitialization
          ->getActiveThemeByName($theme)
          ->getLogo();
        $themeSettings->set('logo.url', $this->fileUrlGenerator
          ->generateString($logo));
      }
      elseif ($logo_path = $themeSettings->get('logo.path')) {
        $themeSettings->set('logo.url', $this->fileUrlGenerator
          ->generateString($logo_path));
      }
      // Generate the path to the favicon.
      if ($themeSettings->get('features.favicon')) {
        $faviconPath = $themeSettings->get('favicon.path');
        if ($themeSettings->get('favicon.use_default')) {
          if (file_exists($favicon = $themeObject->getPath() . '/favicon.ico')) {
            $themeSettings->set('favicon.url', $this->fileUrlGenerator
              ->generateString($favicon));
          }
          else {
            $themeSettings->set('favicon.url', $this->fileUrlGenerator
              ->generateString('core/misc/favicon.ico'));
          }
        }
        elseif ($faviconPath) {
          $themeSettings->set('favicon.url', $this->fileUrlGenerator
            ->generateString($faviconPath));
        }
        else {
          $themeSettings->set('features.favicon', FALSE);
        }
      }
    }
    return $themeSettings;
  }

}

Members

Title Sort descending Modifiers Object type Summary
ThemeSettingsProvider::buildThemeSettings protected function Build a ThemeSettings object for a given theme.
ThemeSettingsProvider::DEFAULT_THEME_FEATURES public constant An array of default theme features.
ThemeSettingsProvider::getSetting public function
ThemeSettingsProvider::__construct public function Builds a new service instance.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.