You are here

public function ThemeInitialization::getActiveTheme in Drupal 10

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

File

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

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';
  }

  // 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'] = $theme->engine ?? NULL;
  $values['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);
}