class WebformThemeManager in Webform 6.x
Same name and namespace in other branches
- 8.5 src/WebformThemeManager.php \Drupal\webform\WebformThemeManager
Defines a class to manage webform theming.
Hierarchy
- class \Drupal\webform\WebformThemeManager implements WebformThemeManagerInterface uses StringTranslationTrait
Expanded class hierarchy of WebformThemeManager
1 string reference to 'WebformThemeManager'
1 service uses WebformThemeManager
File
- src/
WebformThemeManager.php, line 16
Namespace
Drupal\webformView source
class WebformThemeManager implements WebformThemeManagerInterface {
use StringTranslationTrait;
/**
* The configuration object factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The theme manager.
*
* @var \Drupal\Core\Theme\ThemeManagerInterface
*/
protected $themeManager;
/**
* The theme handler.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;
/**
* The theme initialization.
*
* @var \Drupal\Core\Theme\ThemeInitializationInterface
*/
protected $themeInitialization;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Contains the current active theme.
*
* @var \Drupal\Core\Theme\ActiveTheme
*/
protected $activeTheme;
/**
* Constructs a WebformThemeManager object.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration object factory.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
* The theme manager.
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* The theme handler.
* @param \Drupal\Core\Theme\ThemeInitializationInterface $theme_initialization
* The theme initialization.
*/
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;
}
/**
* Get a theme's name.
*
* @return string
* A theme's name
*/
public function getThemeName($name) {
return $this->themeHandler
->getName($name);
}
/**
* Get themes as associative array.
*
* @return array
* An associative array containing theme 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;
}
/**
* {@inheritdoc}
*/
public function getActiveThemeNames() {
$active_theme = $this->themeManager
->getActiveTheme();
// Note: Reversing the order so that base themes are first.
return array_reverse(array_merge([
$active_theme
->getName(),
], array_keys($active_theme
->getBaseThemeExtensions())));
}
/**
* {@inheritdoc}
*/
public function hasActiveTheme() {
// If there is no route match, then there is no active theme.
// If there is no route match the admin theme can't be initialized.
// @see \Drupal\Core\Theme\ThemeManager::initTheme
// @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme
// @see \Drupal\user\Theme\AdminNegotiator::applies
return \Drupal::routeMatch()
->getRouteName() ? TRUE : FALSE;
}
/**
* {@inheritdoc}
*/
public function isActiveTheme($theme_name) {
return in_array($theme_name, $this
->getActiveThemeNames());
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
public function setActiveTheme() {
if ($this->activeTheme) {
$this->themeManager
->setActiveTheme($this->activeTheme);
}
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WebformThemeManager:: |
protected | property | Contains the current active theme. | |
WebformThemeManager:: |
protected | property | The configuration object factory. | |
WebformThemeManager:: |
protected | property | The renderer. | |
WebformThemeManager:: |
protected | property | The current route match. | |
WebformThemeManager:: |
protected | property | The theme handler. | |
WebformThemeManager:: |
protected | property | The theme initialization. | |
WebformThemeManager:: |
protected | property | The theme manager. | |
WebformThemeManager:: |
public | function |
Get all active theme names. Overrides WebformThemeManagerInterface:: |
|
WebformThemeManager:: |
public | function |
Get a theme's name. Overrides WebformThemeManagerInterface:: |
|
WebformThemeManager:: |
public | function |
Get themes as associative array. Overrides WebformThemeManagerInterface:: |
|
WebformThemeManager:: |
public | function |
Determine if the current request has an active theme. Overrides WebformThemeManagerInterface:: |
|
WebformThemeManager:: |
public | function |
Determine if a theme name is being used the active or base theme. Overrides WebformThemeManagerInterface:: |
|
WebformThemeManager:: |
public | function |
Renders HTML given a structured array tree. Overrides WebformThemeManagerInterface:: |
|
WebformThemeManager:: |
public | function |
Renders using the default theme final HTML in situations where no assets are needed. Overrides WebformThemeManagerInterface:: |
|
WebformThemeManager:: |
public | function |
Sets the current theme the active theme. Overrides WebformThemeManagerInterface:: |
|
WebformThemeManager:: |
public | function |
Sets the current theme the theme. Overrides WebformThemeManagerInterface:: |
|
WebformThemeManager:: |
public | function | Constructs a WebformThemeManager object. |