WebformThemeManager.php in Webform 6.x
File
src/WebformThemeManager.php
View source
<?php
namespace Drupal\webform;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Theme\ThemeInitializationInterface;
class WebformThemeManager implements WebformThemeManagerInterface {
use StringTranslationTrait;
protected $configFactory;
protected $themeManager;
protected $themeHandler;
protected $themeInitialization;
protected $renderer;
protected $routeMatch;
protected $activeTheme;
public function __construct(RouteMatchInterface $route_match, ConfigFactoryInterface $config_factory, RendererInterface $renderer, ThemeManagerInterface $theme_manager, ThemeHandlerInterface $theme_handler, ThemeInitializationInterface $theme_initialization) {
$this->routeMatch = $route_match;
$this->configFactory = $config_factory;
$this->renderer = $renderer;
$this->themeManager = $theme_manager;
$this->themeHandler = $theme_handler;
$this->themeInitialization = $theme_initialization;
}
public function getThemeName($name) {
return $this->themeHandler
->getName($name);
}
public function getThemeNames() {
$themes = [];
foreach ($this->themeHandler
->listInfo() as $name => $theme) {
if ($theme->status === 1) {
$themes[$name] = $theme->info['name'];
}
}
asort($themes);
return [
'' => $this
->t('Default'),
] + $themes;
}
public function getActiveThemeNames() {
$active_theme = $this->themeManager
->getActiveTheme();
return array_reverse(array_merge([
$active_theme
->getName(),
], array_keys($active_theme
->getBaseThemeExtensions())));
}
public function hasActiveTheme() {
return \Drupal::routeMatch()
->getRouteName() ? TRUE : FALSE;
}
public function isActiveTheme($theme_name) {
return in_array($theme_name, $this
->getActiveThemeNames());
}
public function setCurrentTheme($theme_name = NULL) {
if (!isset($this->activeTheme)) {
$this->activeTheme = $this->themeManager
->getActiveTheme();
}
$current_theme_name = $theme_name ?: $this->configFactory
->get('system.theme')
->get('default');
$current_theme = $this->themeInitialization
->getActiveThemeByName($current_theme_name);
$this->themeManager
->setActiveTheme($current_theme);
}
public function setActiveTheme() {
if ($this->activeTheme) {
$this->themeManager
->setActiveTheme($this->activeTheme);
}
}
public function render(array &$elements, $theme_name = NULL) {
if ($theme_name !== NULL) {
$this
->setCurrentTheme($theme_name);
}
$markup = $this->renderer
->render($elements);
if ($theme_name !== NULL) {
$this
->setActiveTheme();
}
return $markup;
}
public function renderPlain(array &$elements, $theme_name = NULL) {
if ($theme_name !== NULL) {
$this
->setCurrentTheme($theme_name);
}
$markup = $this->renderer
->renderPlain($elements);
if ($theme_name !== NULL) {
$this
->setActiveTheme();
}
return $markup;
}
}