You are here

public function ThemeInitialization::getActiveTheme in Drupal 8

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

Builds up the active theme object from extensions.

Parameters

\Drupal\Core\Extension\Extension $theme: The theme extension object.

\Drupal\Core\Extension\Extension[] $base_themes: An array of extension objects of base theme and its bases. It is ordered by 'next parent first', meaning the top level of the chain will be first.

Return value

\Drupal\Core\Theme\ActiveTheme The active theme instance for the passed in $theme.

Overrides ThemeInitializationInterface::getActiveTheme

1 call to ThemeInitialization::getActiveTheme()
ThemeInitialization::getActiveThemeByName in core/lib/Drupal/Core/Theme/ThemeInitialization.php
Builds an active theme object.

File

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

Class

ThemeInitialization
Provides the theme initialization logic.

Namespace

Drupal\Core\Theme

Code

public function getActiveTheme(Extension $theme, array $base_themes = []) {
  $theme_path = $theme
    ->getPath();
  $values['path'] = $theme_path;
  $values['name'] = $theme
    ->getName();

  // Use the logo declared in this themes info file, otherwise use logo.svg
  // from the themes root.
  if (!empty($theme->info['logo'])) {
    $values['logo'] = $theme
      ->getPath() . '/' . $theme->info['logo'];
  }
  else {
    $values['logo'] = $theme
      ->getPath() . '/logo.svg';
  }

  // @todo Remove in Drupal 10.0.x.
  $values['stylesheets_remove'] = $this
    ->prepareStylesheetsRemove($theme, $base_themes);

  // Prepare libraries overrides from this theme and ancestor themes. This
  // allows child themes to easily remove CSS files from base themes and
  // modules.
  $values['libraries_override'] = [];

  // Get libraries overrides declared by base themes.
  foreach ($base_themes as $base) {
    if (!empty($base->info['libraries-override'])) {
      foreach ($base->info['libraries-override'] as $library => $override) {
        $values['libraries_override'][$base
          ->getPath()][$library] = $override;
      }
    }
  }

  // Add libraries overrides declared by this theme.
  if (!empty($theme->info['libraries-override'])) {
    foreach ($theme->info['libraries-override'] as $library => $override) {
      $values['libraries_override'][$theme
        ->getPath()][$library] = $override;
    }
  }

  // Get libraries extensions declared by base themes.
  foreach ($base_themes as $base) {
    if (!empty($base->info['libraries-extend'])) {
      foreach ($base->info['libraries-extend'] as $library => $extend) {
        if (isset($values['libraries_extend'][$library])) {

          // Merge if libraries-extend has already been defined for this
          // library.
          $values['libraries_extend'][$library] = array_merge($values['libraries_extend'][$library], $extend);
        }
        else {
          $values['libraries_extend'][$library] = $extend;
        }
      }
    }
  }

  // Add libraries extensions declared by this theme.
  if (!empty($theme->info['libraries-extend'])) {
    foreach ($theme->info['libraries-extend'] as $library => $extend) {
      if (isset($values['libraries_extend'][$library])) {

        // Merge if libraries-extend has already been defined for this
        // library.
        $values['libraries_extend'][$library] = array_merge($values['libraries_extend'][$library], $extend);
      }
      else {
        $values['libraries_extend'][$library] = $extend;
      }
    }
  }

  // Do basically the same as the above for libraries
  $values['libraries'] = [];

  // Grab libraries from base theme
  foreach ($base_themes as $base) {
    if (!empty($base->libraries)) {
      foreach ($base->libraries as $library) {
        $values['libraries'][] = $library;
      }
    }
  }

  // Add libraries used by this theme.
  if (!empty($theme->libraries)) {
    foreach ($theme->libraries as $library) {
      $values['libraries'][] = $library;
    }
  }
  $values['engine'] = isset($theme->engine) ? $theme->engine : NULL;
  $values['owner'] = isset($theme->owner) ? $theme->owner : NULL;
  $values['extension'] = $theme;
  $base_active_themes = [];
  foreach ($base_themes as $base_theme) {
    $base_active_themes[$base_theme
      ->getName()] = $base_theme;
  }
  $values['base_theme_extensions'] = $base_active_themes;
  if (!empty($theme->info['regions'])) {
    $values['regions'] = $theme->info['regions'];
  }
  return new ActiveTheme($values);
}