acquia_vwo-1.0.x-dev/modules/acquia_vwo_content/src/Service/ThemeManager.php
modules/acquia_vwo_content/src/Service/ThemeManager.php
<?php
namespace Drupal\acquia_vwo_content\Service;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Theme\ActiveTheme;
use Drupal\Core\Theme\ThemeInitializationInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
/**
* Class ThemeManager.
*
* Handles theme switching logic.
*/
class ThemeManager {
/**
* The theme initialization.
*
* @var \Drupal\Core\Theme\ThemeInitializationInterface
*/
protected $themeInitialization;
/**
* The theme manager.
*
* @var \Drupal\Core\Theme\ThemeManagerInterface
*/
protected $themeManager;
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructor.
*/
public function __construct(
ThemeInitializationInterface $theme_initialization,
ThemeManagerInterface $theme_manager,
ConfigFactoryInterface $config_factory,
) {
$this->themeInitialization = $theme_initialization;
$this->themeManager = $theme_manager;
$this->configFactory = $config_factory;
}
/**
* Get Default Theme.
*
* @return \Drupal\Core\Theme\ActiveTheme
* The default theme.
*
* @throws \Drupal\Core\Theme\MissingThemeDependencyException
* If the default theme is not found.
*/
public function getDefaultTheme(): ActiveTheme {
$activeDefaultThemeName = $this->configFactory->get('system.theme')->get('default');
return $this->themeInitialization->getActiveThemeByName($activeDefaultThemeName);
}
/**
* Get Active Theme.
*
* @return \Drupal\Core\Theme\ActiveTheme
* The active theme.
*/
public function getActiveTheme(): ActiveTheme {
return $this->themeManager->getActiveTheme();
}
/**
* Set Active Theme.
*
* @param \Drupal\Core\Theme\ActiveTheme $theme
* The theme to set as active.
*
* @return void
* No return value.
*/
public function switchTheme(ActiveTheme $theme): void {
$this->themeInitialization->loadActiveTheme($theme);
$this->themeManager->setActiveTheme($theme);
}
}
