getActiveTheme example

/** * {@inheritdoc} */
  public static function getLabel() {
    return t('Theme');
  }

  /** * {@inheritdoc} */
  public function getContext() {
    return $this->themeManager->getActiveTheme()->getName() ?: 'stark';
  }

  /** * {@inheritdoc} */
  public function getCacheableMetadata() {
    return new CacheableMetadata();
  }

}


  /** * Tests that changes to the info file are picked up. */
  public function testChanges() {
    $this->themeInstaller->install(['test_theme']);
    $this->config('system.theme')->set('default', 'test_theme')->save();
    $this->themeManager->resetActiveTheme();

    $active_theme = $this->themeManager->getActiveTheme();
    // Make sure we are not testing the wrong theme.     $this->assertEquals('test_theme', $active_theme->getName());
    $this->assertEquals(['starterkit_theme/base', 'starterkit_theme/messages', 'core/normalize', 'test_theme/global-styling']$active_theme->getLibraries());

    // @see theme_test_system_info_alter()     $this->state->set('theme_test.modify_info_files', TRUE);
    $this->resetAll();
    $active_theme = $this->themeManager->getActiveTheme();
    $this->assertEquals(['starterkit_theme/base', 'starterkit_theme/messages', 'core/normalize', 'test_theme/global-styling', 'core/once']$active_theme->getLibraries());
  }

}
$this->themeInstaller = $this->container->get('theme_installer');
    $this->themeManager = $this->container->get('theme.manager');
  }

  /** * Tests opting out of Stable 9 by setting the base theme to false. */
  public function testWildWest() {
    $this->themeInstaller->install(['test_wild_west']);
    $this->config('system.theme')->set('default', 'test_wild_west')->save();
    $theme = $this->themeManager->getActiveTheme();
    /** @var \Drupal\Core\Theme\ActiveTheme $base_theme */
    $base_themes = $theme->getBaseThemeExtensions();
    $this->assertEmpty($base_themes, 'No base theme is set when a theme has opted out of using Stable 9.');
  }

}
$this->assertNotSame($element_info$this->container->get('element_info'));
    $this->assertNotEmpty($this->container->get('element_info')->getInfo($type));

    $build = [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#value' => 'Inner',
    ];
    $expected = "<h3>Inner</h3>\n";

    $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
    $output = \Drupal::service('renderer')->renderRoot($build);
    $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());

    $this->assertSame($expected(string) $build['#markup']);
    $this->assertSame($expected(string) $output);
  }

  /** * @covers ::render */
  public function testRenderWithTheme() {
    
$this->themeManager = $theme_manager;
    parent::__construct(NULL, $cache$lock['library_info']);

    $this->discoveryParser = $discovery_parser;
  }

  /** * {@inheritdoc} */
  protected function getCid() {
    if (!isset($this->cid)) {
      $this->cid = 'library_info:' . $this->themeManager->getActiveTheme()->getName();
    }

    return $this->cid;
  }

  /** * {@inheritdoc} */
  protected function resolveCacheMiss($key) {
    $this->storage[$key] = $this->getLibraryDefinitions($key);
    $this->persist($key);

    
$this->libraryDependencyResolver->getLibrariesWithDependencies($assets->getAlreadyLoadedLibraries())
    );
  }

  /** * {@inheritdoc} */
  public function getCssAssets(AttachedAssetsInterface $assets$optimize, LanguageInterface $language = NULL) {
    if (!isset($language)) {
      $language = $this->languageManager->getCurrentLanguage();
    }
    $theme_info = $this->themeManager->getActiveTheme();
    // Add the theme name to the cache key since themes may implement     // hook_library_info_alter().     $libraries_to_load = $this->getLibrariesToLoad($assets);
    $cid = 'css:' . $theme_info->getName() . ':' . $language->getId() . Crypt::hashBase64(serialize($libraries_to_load)) . (int) $optimize;
    if ($cached = $this->cache->get($cid)) {
      return $cached->data;
    }

    $css = [];
    $default_options = [
      'type' => 'file',
      
parent::submitConfigurationForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function evaluate() {
    if (!$this->configuration['theme']) {
      return TRUE;
    }

    return $this->themeManager->getActiveTheme()->getName() == $this->configuration['theme'];
  }

  /** * {@inheritdoc} */
  public function summary() {
    if ($this->isNegated()) {
      return $this->t('The current theme is not @theme', ['@theme' => $this->configuration['theme']]);
    }

    return $this->t('The current theme is @theme', ['@theme' => $this->configuration['theme']]);
  }
// We don't do any aggregation and hence also no caching for external         // CSS assets.         $uri = $css_group['items'][0]['data'];
        $css_assets[$order]['data'] = $uri;
      }
    }

    // All asset group URLs will have exactly the same query arguments, except     // for the delta, so prepare them in advance.     $query_args = [
      'language' => $this->languageManager->getCurrentLanguage()->getId(),
      'theme' => $this->themeManager->getActiveTheme()->getName(),
      'include' => UrlHelper::compressQueryParameter(implode(',', $this->dependencyResolver->getMinimalRepresentativeSubset($libraries))),
    ];
    $ajax_page_state = $this->requestStack->getCurrentRequest()->get('ajax_page_state');
    $already_loaded = isset($ajax_page_state) ? explode(',', $ajax_page_state['libraries']) : [];
    if ($already_loaded) {
      $query_args['exclude'] = UrlHelper::compressQueryParameter(implode(',', $this->dependencyResolver->getMinimalRepresentativeSubset($already_loaded)));
    }

    // Generate a URL for each group of assets, but do not process them inline,     // this is done using optimizeGroup() when the asset path is requested.     foreach ($css_assets as $order => $css_asset) {
      

  public function themePage($inherited) {
    $theme_key = $this->themeManager->getActiveTheme()->getName();
    // Now we check what the theme negotiator service returns.     $active_theme = $this->themeNegotiator
      ->determineActiveTheme($this->routeMatch);
    $output = "Active theme: $active_theme. Actual theme: $theme_key.";
    if ($inherited) {
      $output .= ' Theme negotiation inheritance is being tested.';
    }
    return ['#markup' => $output];
  }

  /** * A title callback for XSS breadcrumb check. * * @return string */
'ajax' => 'test',
        ],
      ],
    ];
  }

  /** * Returns the used theme. */
  public function theme() {
    return [
      '#markup' => 'Current theme: ' . \Drupal::theme()->getActiveTheme()->getName(),
    ];
  }

  /** * Returns an AjaxResponse; settings command set last. * * Helps verifying AjaxResponse reorders commands to ensure correct execution. */
  public function order() {
    $response = new AjaxResponse();
    // HTML insertion command.
/** * Tests that the maintenance theme initializes the theme and its base themes. */
  public function testMaintenanceTheme() {
    $this->setSetting('maintenance_theme', 'test_subtheme');
    // Get the maintenance theme loaded.     drupal_maintenance_theme();

    // Do we have an active theme?     $this->assertTrue(\Drupal::theme()->hasActiveTheme());

    $active_theme = \Drupal::theme()->getActiveTheme();
    $this->assertEquals('test_subtheme', $active_theme->getName());

    $base_themes = $active_theme->getBaseThemeExtensions();
    $base_theme_names = array_keys($base_themes);
    $this->assertSame(['test_basetheme']$base_theme_names);
  }

}
$element['#default_value'] = $friendly_path;
        }

        // Prepare local file path for description.         if ($original_path && isset($friendly_path)) {
          $local_file = strtr($original_path['public:/' => PublicStream::basePath()]);
        }
        elseif ($theme) {
          $local_file = $this->themeHandler->getTheme($theme)->getPath() . '/' . $default;
        }
        else {
          $local_file = $this->themeManager->getActiveTheme()->getPath() . '/' . $default;
        }

        $element['#description'] = $this->t('Examples: <code>@implicit-public-file</code> (for a file in the public filesystem), <code>@explicit-file</code>, or <code>@local-file</code>.', [
          '@implicit-public-file' => $friendly_path ?? $default,
          '@explicit-file' => StreamWrapperManager::getScheme($original_path) !== FALSE ? $original_path : 'public://' . $default,
          '@local-file' => $local_file,
        ]);
      }
    }

    if ($theme) {
      
    // 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;
      
/** @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 */
return;
    }

    /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form */
    $exposed_form = $this->display_handler->getPlugin('exposed_form');
    $exposed_form->preRender($this->result);

    $module_handler = \Drupal::moduleHandler();

    // @todo In the long run, it would be great to execute a view without     // the theme system at all. See https://www.drupal.org/node/2322623.     $active_theme = \Drupal::theme()->getActiveTheme();
    $themes = array_keys($active_theme->getBaseThemeExtensions());
    $themes[] = $active_theme->getName();

    // Check for already-cached output.     /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */
    if (!empty($this->live_preview)) {
      $cache = Views::pluginManager('cache')->createInstance('none');
    }
    else {
      $cache = $this->display_handler->getPlugin('cache');
    }

    
Home | Imprint | This part of the site doesn't use cookies.