You are here

class WebformThemeManager in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformThemeManager.php \Drupal\webform\WebformThemeManager

Defines a class to manage webform theming.

Hierarchy

Expanded class hierarchy of WebformThemeManager

1 string reference to 'WebformThemeManager'
webform.services.yml in ./webform.services.yml
webform.services.yml
1 service uses WebformThemeManager
webform.theme_manager in ./webform.services.yml
Drupal\webform\WebformThemeManager

File

src/WebformThemeManager.php, line 16

Namespace

Drupal\webform
View 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

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WebformThemeManager::$activeTheme protected property Contains the current active theme.
WebformThemeManager::$configFactory protected property The configuration object factory.
WebformThemeManager::$renderer protected property The renderer.
WebformThemeManager::$routeMatch protected property The current route match.
WebformThemeManager::$themeHandler protected property The theme handler.
WebformThemeManager::$themeInitialization protected property The theme initialization.
WebformThemeManager::$themeManager protected property The theme manager.
WebformThemeManager::getActiveThemeNames public function Get all active theme names. Overrides WebformThemeManagerInterface::getActiveThemeNames
WebformThemeManager::getThemeName public function Get a theme's name. Overrides WebformThemeManagerInterface::getThemeName
WebformThemeManager::getThemeNames public function Get themes as associative array. Overrides WebformThemeManagerInterface::getThemeNames
WebformThemeManager::hasActiveTheme public function Determine if the current request has an active theme. Overrides WebformThemeManagerInterface::hasActiveTheme
WebformThemeManager::isActiveTheme public function Determine if a theme name is being used the active or base theme. Overrides WebformThemeManagerInterface::isActiveTheme
WebformThemeManager::render public function Renders HTML given a structured array tree. Overrides WebformThemeManagerInterface::render
WebformThemeManager::renderPlain public function Renders using the default theme final HTML in situations where no assets are needed. Overrides WebformThemeManagerInterface::renderPlain
WebformThemeManager::setActiveTheme public function Sets the current theme the active theme. Overrides WebformThemeManagerInterface::setActiveTheme
WebformThemeManager::setCurrentTheme public function Sets the current theme the theme. Overrides WebformThemeManagerInterface::setCurrentTheme
WebformThemeManager::__construct public function Constructs a WebformThemeManager object.