You are here

public function ThemeInitialization::getActiveThemeByName in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Theme/ThemeInitialization.php \Drupal\Core\Theme\ThemeInitialization::getActiveThemeByName()

Builds an active theme object.

Parameters

string $theme_name: The machine name of the theme.

Return value

\Drupal\Core\Theme\ActiveTheme An active theme object instance for the given theme.

Throws

\Drupal\Core\Theme\MissingThemeDependencyException Thrown when base theme for installed theme is not installed.

Overrides ThemeInitializationInterface::getActiveThemeByName

1 call to ThemeInitialization::getActiveThemeByName()
ThemeInitialization::initTheme in core/lib/Drupal/Core/Theme/ThemeInitialization.php
Initializes a given theme.

File

core/lib/Drupal/Core/Theme/ThemeInitialization.php, line 82

Class

ThemeInitialization
Provides the theme initialization logic.

Namespace

Drupal\Core\Theme

Code

public function getActiveThemeByName($theme_name) {
  if ($cached = $this->cache
    ->get('theme.active_theme.' . $theme_name)) {
    return $cached->data;
  }
  $themes = $this->themeHandler
    ->listInfo();

  // If no theme could be negotiated, or if the negotiated theme is not within
  // the list of installed themes, fall back to the default theme output of
  // core and modules (like Stark, but without a theme extension at all). This
  // is possible, because loadActiveTheme() always loads the Twig theme
  // engine. This is desired, because missing or malformed theme configuration
  // should not leave the application in a broken state. By falling back to
  // default output, the user is able to reconfigure the theme through the UI.
  // Lastly, tests are expected to operate with no theme by default, so as to
  // only assert the original theme output of modules (unless a test manually
  // installs a specific theme).
  if (empty($themes) || !$theme_name || !isset($themes[$theme_name])) {
    $theme_name = 'core';

    // /core/core.info.yml does not actually exist, but is required because
    // Extension expects a pathname.
    $active_theme = $this
      ->getActiveTheme(new Extension($this->root, 'theme', 'core/core.info.yml'));

    // Early-return and do not set state, because the initialized $theme_name
    // differs from the original $theme_name.
    return $active_theme;
  }

  // Find all our ancestor themes and put them in an array.
  $base_themes = [];
  $ancestor = $theme_name;
  while ($ancestor && isset($themes[$ancestor]->base_theme)) {
    $ancestor = $themes[$ancestor]->base_theme;
    if (!$this->themeHandler
      ->themeExists($ancestor)) {
      if ($ancestor == 'stable') {

        // Themes that depend on Stable will be fixed by system_update_8014().
        // There is no harm in not adding it as an ancestor since at worst
        // some people might experience slight visual regressions on
        // update.php.
        continue;
      }
      throw new MissingThemeDependencyException(sprintf('Base theme %s has not been installed.', $ancestor), $ancestor);
    }
    $base_themes[] = $themes[$ancestor];
  }
  $active_theme = $this
    ->getActiveTheme($themes[$theme_name], $base_themes);
  $this->cache
    ->set('theme.active_theme.' . $theme_name, $active_theme);
  return $active_theme;
}