getActiveThemeByName example


  protected function init($theme_name = NULL) {
    if ($this->initialized) {
      return;
    }
    // Unless instantiated for a specific theme, use globals.     if (!isset($theme_name)) {
      $this->theme = $this->themeManager->getActiveTheme();
    }
    // Instead of the active theme, a specific theme was requested.     else {
      $this->theme = $this->themeInitialization->getActiveThemeByName($theme_name);
      $this->themeInitialization->loadActiveTheme($this->theme);
    }
  }

  /** * Returns the complete theme registry from cache or rebuilds it. * * @return array * The complete theme registry data array. * * @see Registry::$registry */
/** * Ensures that all core module and theme library files exist. */
  public function testCoreLibraryCompleteness() {
    // First verify all libraries with no active theme.     $this->verifyLibraryFilesExist($this->getAllLibraries());

    // Then verify all libraries for each core theme. This may seem like     // overkill but themes can override and extend other extensions' libraries     // and these changes are only applied for the active theme.     foreach ($this->allThemes as $theme) {
      $this->themeManager->setActiveTheme($this->themeInitialization->getActiveThemeByName($theme));
      $this->libraryDiscovery->clearCachedDefinitions();

      $this->verifyLibraryFilesExist($this->getAllLibraries());
    }
  }

  /** * Checks that all the library files exist. * * @param array[] $library_definitions * An array of library definitions, keyed by extension, then by library, and * so on. */

  public function testElementInfoByTheme() {
    /** @var \Drupal\Core\Theme\ThemeInitializationInterface $theme_initializer */
    $theme_initializer = $this->container->get('theme.initialization');

    /** @var \Drupal\Core\Theme\ThemeManagerInterface $theme_manager */
    $theme_manager = $this->container->get('theme.manager');

    /** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */
    $element_info = $this->container->get('plugin.manager.element_info');

    $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName('starterkit_theme'));
    $this->assertEquals(60, $element_info->getInfo('textfield')['#size']);

    $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName('test_theme'));
    $this->assertEquals(40, $element_info->getInfo('textfield')['#size']);
  }

}

  protected function activateTheme($theme_name) {
    $this->container->get('theme_installer')->install([$theme_name]);

    /** @var \Drupal\Core\Theme\ThemeInitializationInterface $theme_initializer */
    $theme_initializer = $this->container->get('theme.initialization');

    /** @var \Drupal\Core\Theme\ThemeManagerInterface $theme_manager */
    $theme_manager = $this->container->get('theme.manager');

    $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName($theme_name));

    $this->libraryDiscovery->clearCachedDefinitions();

    $this->assertSame($theme_name$theme_manager->getActiveTheme()->getName());
  }

  /** * Asserts that the specified asset is in the given library. * * @param string $asset * The asset file with the path for the file. * @param string $extension * The extension in which the $library is defined. * @param string $library_name * Name of the library. * @param string $sub_key * The library sub key where the given asset is defined. * @param string $message * (optional) A message to display with the assertion. * * @internal */
public function __construct($root, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache, ModuleHandlerInterface $module_handler) {
    $this->root = $root;
    $this->themeHandler = $theme_handler;
    $this->cache = $cache;
    $this->moduleHandler = $module_handler;
  }

  /** * {@inheritdoc} */
  public function initTheme($theme_name) {
    $active_theme = $this->getActiveThemeByName($theme_name);
    $this->loadActiveTheme($active_theme);

    return $active_theme;
  }

  /** * {@inheritdoc} */
  public function getActiveThemeByName($theme_name) {
    if ($cached = $this->cache->get('theme.active_theme.' . $theme_name)) {
      return $cached->data;
    }
else {
        $theme_keys = [$theme];
      }

      // Save the name of the current theme (if any), so that we can temporarily       // override the current theme and allow theme_get_setting() to work       // without having to pass the theme name to it.       $default_active_theme = $this->themeManager->getActiveTheme();
      $default_theme = $default_active_theme->getName();
      /** @var \Drupal\Core\Theme\ThemeInitialization $theme_initialization */
      $theme_initialization = \Drupal::service('theme.initialization');
      $this->themeManager->setActiveTheme($theme_initialization->getActiveThemeByName($theme));

      // Process the theme and all its base themes.       foreach ($theme_keys as $theme) {
        // Include the theme-settings.php file.         $theme_path = $this->themeHandler->getTheme($theme)->getPath();
        $theme_settings_file = $theme_path . '/theme-settings.php';
        $theme_file = $theme_path . '/' . $theme . '.theme';
        $filenames = [$theme_settings_file$theme_file];
        foreach ($filenames as $filename) {
          if (file_exists($filename)) {
            require_once $filename;

            
/** * Ensures Twig template cache setting can be overridden. */
  public function testTwigCacheOverride() {
    $extension = twig_extension();
    $theme_installer = $this->container->get('theme_installer');
    $theme_installer->install(['test_theme']);
    $this->config('system.theme')->set('default', 'test_theme')->save();

    // The registry still works on theme globals, so set them here.     \Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->getActiveThemeByName('test_theme'));

    // Reset the theme registry, so that the new theme is used.     $this->container->set('theme.registry', NULL);

    // Load array of Twig templates.     // reset() is necessary to invalidate caches tagged with 'theme_registry'.     $registry = $this->container->get('theme.registry');
    $registry->reset();
    $templates = $registry->getRuntime();

    // Get the template filename and the cache filename for
$this->libraryDiscovery = $this->container->get('library.discovery');
  }

  /** * Ensures that Stable 9 overrides all relevant core library assets. */
  public function testStable9LibraryOverrides() {
    // First get the clean library definitions with no active theme.     $libraries_before = $this->getAllLibraries();
    $libraries_before = $this->removeVendorAssets($libraries_before);

    $this->themeManager->setActiveTheme($this->themeInitialization->getActiveThemeByName('stable9'));
    $this->libraryDiscovery->clearCachedDefinitions();

    // Now get the library definitions with Stable 9 as the active theme.     $libraries_after = $this->getAllLibraries();
    $libraries_after = $this->removeVendorAssets($libraries_after);

    foreach ($libraries_before as $extension => $libraries) {
      foreach ($libraries as $library_name => $library) {
        // Allow skipping libraries.         if (in_array("$extension/$library_name", $this->librariesToSkip)) {
          continue;
        }
Home | Imprint | This part of the site doesn't use cookies.