ThemeInitialization::getActiveTheme

public ThemeInitialization::getActiveTheme(Extension $theme, array $base_themes = [])

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

File

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

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();

  // @todo Remove in Drupal 9.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'] = array();

  // 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 = array();
  foreach ($base_themes as $base_theme) {
    $base_active_themes[$base_theme->getName()] = $this->getActiveTheme($base_theme, array_slice($base_themes, 1));
  }

  $values['base_themes'] = $base_active_themes;
  if (!empty($theme->info['regions'])) {
    $values['regions'] = $theme->info['regions'];
  }

  return new ActiveTheme($values);
}
doc_Drupal
2016-10-29 09:48:05
Comments
Leave a Comment

Please login to continue.